OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // This file defines utility functions for working with strings. | 5 // This file defines utility functions for working with strings. |
6 | 6 |
7 #ifndef BASE_STRINGS_STRING_UTIL_H_ | 7 #ifndef BASE_STRINGS_STRING_UTIL_H_ |
8 #define BASE_STRINGS_STRING_UTIL_H_ | 8 #define BASE_STRINGS_STRING_UTIL_H_ |
9 | 9 |
10 #include <ctype.h> | 10 #include <ctype.h> |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 // Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT | 131 // Therefore, DO NOT USE THESE AS A GENERAL-PURPOSE SUBSTITUTE FOR DEFAULT |
132 // CONSTRUCTORS. There is only one case where you should use these: functions | 132 // CONSTRUCTORS. There is only one case where you should use these: functions |
133 // which need to return a string by reference (e.g. as a class member | 133 // which need to return a string by reference (e.g. as a class member |
134 // accessor), and don't have an empty string to use (e.g. in an error case). | 134 // accessor), and don't have an empty string to use (e.g. in an error case). |
135 // These should not be used as initializers, function arguments, or return | 135 // These should not be used as initializers, function arguments, or return |
136 // values for functions which return by value or outparam. | 136 // values for functions which return by value or outparam. |
137 BASE_EXPORT const std::string& EmptyString(); | 137 BASE_EXPORT const std::string& EmptyString(); |
138 BASE_EXPORT const string16& EmptyString16(); | 138 BASE_EXPORT const string16& EmptyString16(); |
139 | 139 |
140 // Contains the set of characters representing whitespace in the corresponding | 140 // Contains the set of characters representing whitespace in the corresponding |
141 // encoding. Null-terminated. | 141 // encoding. Null-terminated. The ASCII versions are the whitespaces as defined |
142 BASE_EXPORT extern const wchar_t kWhitespaceWide[]; | 142 // by HTML5, and don't include control characters. |
143 BASE_EXPORT extern const char16 kWhitespaceUTF16[]; | 143 BASE_EXPORT extern const wchar_t kWhitespaceWide[]; // Includes Unicode. |
| 144 BASE_EXPORT extern const char16 kWhitespaceUTF16[]; // Includes Unicode. |
144 BASE_EXPORT extern const char kWhitespaceASCII[]; | 145 BASE_EXPORT extern const char kWhitespaceASCII[]; |
| 146 BASE_EXPORT extern const char16 kWhitespaceASCIIAs16[]; // No unicode. |
145 | 147 |
146 // Null-terminated string representing the UTF-8 byte order mark. | 148 // Null-terminated string representing the UTF-8 byte order mark. |
147 BASE_EXPORT extern const char kUtf8ByteOrderMark[]; | 149 BASE_EXPORT extern const char kUtf8ByteOrderMark[]; |
148 | 150 |
149 // Removes characters in |remove_chars| from anywhere in |input|. Returns true | 151 // Removes characters in |remove_chars| from anywhere in |input|. Returns true |
150 // if any characters were removed. |remove_chars| must be null-terminated. | 152 // if any characters were removed. |remove_chars| must be null-terminated. |
151 // NOTE: Safe to use the same variable for both |input| and |output|. | 153 // NOTE: Safe to use the same variable for both |input| and |output|. |
152 BASE_EXPORT bool RemoveChars(const string16& input, | 154 BASE_EXPORT bool RemoveChars(const string16& input, |
153 const base::StringPiece16& remove_chars, | 155 const base::StringPiece16& remove_chars, |
154 string16* output); | 156 string16* output); |
155 BASE_EXPORT bool RemoveChars(const std::string& input, | 157 BASE_EXPORT bool RemoveChars(const std::string& input, |
156 const base::StringPiece& remove_chars, | 158 const base::StringPiece& remove_chars, |
157 std::string* output); | 159 std::string* output); |
158 | 160 |
159 // Replaces characters in |replace_chars| from anywhere in |input| with | 161 // Replaces characters in |replace_chars| from anywhere in |input| with |
160 // |replace_with|. Each character in |replace_chars| will be replaced with | 162 // |replace_with|. Each character in |replace_chars| will be replaced with |
161 // the |replace_with| string. Returns true if any characters were replaced. | 163 // the |replace_with| string. Returns true if any characters were replaced. |
162 // |replace_chars| must be null-terminated. | 164 // |replace_chars| must be null-terminated. |
163 // NOTE: Safe to use the same variable for both |input| and |output|. | 165 // NOTE: Safe to use the same variable for both |input| and |output|. |
164 BASE_EXPORT bool ReplaceChars(const string16& input, | 166 BASE_EXPORT bool ReplaceChars(const string16& input, |
165 const base::StringPiece16& replace_chars, | 167 const base::StringPiece16& replace_chars, |
166 const string16& replace_with, | 168 const string16& replace_with, |
167 string16* output); | 169 string16* output); |
168 BASE_EXPORT bool ReplaceChars(const std::string& input, | 170 BASE_EXPORT bool ReplaceChars(const std::string& input, |
169 const base::StringPiece& replace_chars, | 171 const base::StringPiece& replace_chars, |
170 const std::string& replace_with, | 172 const std::string& replace_with, |
171 std::string* output); | 173 std::string* output); |
172 | 174 |
| 175 enum TrimPositions { |
| 176 TRIM_NONE = 0, |
| 177 TRIM_LEADING = 1 << 0, |
| 178 TRIM_TRAILING = 1 << 1, |
| 179 TRIM_ALL = TRIM_LEADING | TRIM_TRAILING, |
| 180 }; |
| 181 |
173 // Removes characters in |trim_chars| from the beginning and end of |input|. | 182 // Removes characters in |trim_chars| from the beginning and end of |input|. |
174 // |trim_chars| must be null-terminated. | 183 // The 8-bit version only works on 8-bit characters, not UTF-8. |
175 // NOTE: Safe to use the same variable for both |input| and |output|. | 184 // |
| 185 // It is safe to use the same variable for both |input| and |output| (this is |
| 186 // the normal usage to trim in-place). |
176 BASE_EXPORT bool TrimString(const string16& input, | 187 BASE_EXPORT bool TrimString(const string16& input, |
177 const base::StringPiece16& trim_chars, | 188 base::StringPiece16 trim_chars, |
178 string16* output); | 189 string16* output); |
179 BASE_EXPORT bool TrimString(const std::string& input, | 190 BASE_EXPORT bool TrimString(const std::string& input, |
180 const base::StringPiece& trim_chars, | 191 base::StringPiece trim_chars, |
181 std::string* output); | 192 std::string* output); |
182 | 193 |
| 194 // StringPiece versions of the above. The returned pieces refer to the original |
| 195 // buffer. |
| 196 BASE_EXPORT StringPiece16 TrimString(StringPiece16 input, |
| 197 const base::StringPiece16& trim_chars, |
| 198 TrimPositions positions); |
| 199 BASE_EXPORT StringPiece TrimString(StringPiece input, |
| 200 const base::StringPiece& trim_chars, |
| 201 TrimPositions positions); |
| 202 |
183 // Truncates a string to the nearest UTF-8 character that will leave | 203 // Truncates a string to the nearest UTF-8 character that will leave |
184 // the string less than or equal to the specified byte size. | 204 // the string less than or equal to the specified byte size. |
185 BASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input, | 205 BASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input, |
186 const size_t byte_size, | 206 const size_t byte_size, |
187 std::string* output); | 207 std::string* output); |
188 | 208 |
189 // Trims any whitespace from either end of the input string. Returns where | 209 // Trims any whitespace from either end of the input string. Returns where |
190 // whitespace was found. | 210 // whitespace was found. |
191 // The non-wide version has two functions: | 211 // The non-wide version has two functions: |
192 // * TrimWhitespaceASCII() | 212 // * TrimWhitespaceASCII() |
193 // This function is for ASCII strings and only looks for ASCII whitespace; | 213 // This function is for ASCII strings and only looks for ASCII whitespace; |
194 // Please choose the best one according to your usage. | 214 // Please choose the best one according to your usage. |
195 // NOTE: Safe to use the same variable for both input and output. | 215 // NOTE: Safe to use the same variable for both input and output. |
196 enum TrimPositions { | |
197 TRIM_NONE = 0, | |
198 TRIM_LEADING = 1 << 0, | |
199 TRIM_TRAILING = 1 << 1, | |
200 TRIM_ALL = TRIM_LEADING | TRIM_TRAILING, | |
201 }; | |
202 BASE_EXPORT TrimPositions TrimWhitespace(const string16& input, | 216 BASE_EXPORT TrimPositions TrimWhitespace(const string16& input, |
203 TrimPositions positions, | 217 TrimPositions positions, |
204 base::string16* output); | 218 base::string16* output); |
205 BASE_EXPORT TrimPositions TrimWhitespaceASCII(const std::string& input, | 219 BASE_EXPORT TrimPositions TrimWhitespaceASCII(const std::string& input, |
206 TrimPositions positions, | 220 TrimPositions positions, |
207 std::string* output); | 221 std::string* output); |
208 | 222 |
209 // Deprecated. This function is only for backward compatibility and calls | 223 // Deprecated. This function is only for backward compatibility and calls |
210 // TrimWhitespaceASCII(). | 224 // TrimWhitespaceASCII(). |
211 BASE_EXPORT TrimPositions TrimWhitespace(const std::string& input, | 225 BASE_EXPORT TrimPositions TrimWhitespace(const std::string& input, |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 str->reserve(length_with_null); | 457 str->reserve(length_with_null); |
444 str->resize(length_with_null - 1); | 458 str->resize(length_with_null - 1); |
445 return &((*str)[0]); | 459 return &((*str)[0]); |
446 } | 460 } |
447 | 461 |
448 //----------------------------------------------------------------------------- | 462 //----------------------------------------------------------------------------- |
449 | 463 |
450 // Splits a string into its fields delimited by any of the characters in | 464 // Splits a string into its fields delimited by any of the characters in |
451 // |delimiters|. Each field is added to the |tokens| vector. Returns the | 465 // |delimiters|. Each field is added to the |tokens| vector. Returns the |
452 // number of tokens found. | 466 // number of tokens found. |
| 467 // |
| 468 // DEPRECATED. Use SplitStringUsingSet for new code (these just forward). |
| 469 // TODO(brettw) convert callers and delete these forwarders. |
453 BASE_EXPORT size_t Tokenize(const base::string16& str, | 470 BASE_EXPORT size_t Tokenize(const base::string16& str, |
454 const base::string16& delimiters, | 471 const base::string16& delimiters, |
455 std::vector<base::string16>* tokens); | 472 std::vector<base::string16>* tokens); |
456 BASE_EXPORT size_t Tokenize(const std::string& str, | 473 BASE_EXPORT size_t Tokenize(const std::string& str, |
457 const std::string& delimiters, | 474 const std::string& delimiters, |
458 std::vector<std::string>* tokens); | 475 std::vector<std::string>* tokens); |
459 BASE_EXPORT size_t Tokenize(const base::StringPiece& str, | 476 BASE_EXPORT size_t Tokenize(const base::StringPiece& str, |
460 const base::StringPiece& delimiters, | 477 const base::StringPiece& delimiters, |
461 std::vector<base::StringPiece>* tokens); | 478 std::vector<base::StringPiece>* tokens); |
462 | 479 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 // string can contain wildcards like * and ? | 515 // string can contain wildcards like * and ? |
499 // The backslash character (\) is an escape character for * and ? | 516 // The backslash character (\) is an escape character for * and ? |
500 // We limit the patterns to having a max of 16 * or ? characters. | 517 // We limit the patterns to having a max of 16 * or ? characters. |
501 // ? matches 0 or 1 character, while * matches 0 or more characters. | 518 // ? matches 0 or 1 character, while * matches 0 or more characters. |
502 BASE_EXPORT bool MatchPattern(const base::StringPiece& string, | 519 BASE_EXPORT bool MatchPattern(const base::StringPiece& string, |
503 const base::StringPiece& pattern); | 520 const base::StringPiece& pattern); |
504 BASE_EXPORT bool MatchPattern(const base::string16& string, | 521 BASE_EXPORT bool MatchPattern(const base::string16& string, |
505 const base::string16& pattern); | 522 const base::string16& pattern); |
506 | 523 |
507 #endif // BASE_STRINGS_STRING_UTIL_H_ | 524 #endif // BASE_STRINGS_STRING_UTIL_H_ |
OLD | NEW |