| 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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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. |
| 142 BASE_EXPORT extern const wchar_t kWhitespaceWide[]; | 142 BASE_EXPORT extern const wchar_t kWhitespaceWide[]; // Includes Unicode. |
| 143 BASE_EXPORT extern const char16 kWhitespaceUTF16[]; | 143 BASE_EXPORT extern const char16 kWhitespaceUTF16[]; // Includes Unicode. |
| 144 BASE_EXPORT extern const char kWhitespaceASCII[]; | 144 BASE_EXPORT extern const char kWhitespaceASCII[]; |
| 145 BASE_EXPORT extern const char16 kWhitespaceASCIIAs16[]; // No unicode. |
| 145 | 146 |
| 146 // Null-terminated string representing the UTF-8 byte order mark. | 147 // Null-terminated string representing the UTF-8 byte order mark. |
| 147 BASE_EXPORT extern const char kUtf8ByteOrderMark[]; | 148 BASE_EXPORT extern const char kUtf8ByteOrderMark[]; |
| 148 | 149 |
| 149 // Removes characters in |remove_chars| from anywhere in |input|. Returns true | 150 // Removes characters in |remove_chars| from anywhere in |input|. Returns true |
| 150 // if any characters were removed. |remove_chars| must be null-terminated. | 151 // if any characters were removed. |remove_chars| must be null-terminated. |
| 151 // NOTE: Safe to use the same variable for both |input| and |output|. | 152 // NOTE: Safe to use the same variable for both |input| and |output|. |
| 152 BASE_EXPORT bool RemoveChars(const string16& input, | 153 BASE_EXPORT bool RemoveChars(const string16& input, |
| 153 const base::StringPiece16& remove_chars, | 154 const base::StringPiece16& remove_chars, |
| 154 string16* output); | 155 string16* output); |
| 155 BASE_EXPORT bool RemoveChars(const std::string& input, | 156 BASE_EXPORT bool RemoveChars(const std::string& input, |
| 156 const base::StringPiece& remove_chars, | 157 const base::StringPiece& remove_chars, |
| 157 std::string* output); | 158 std::string* output); |
| 158 | 159 |
| 159 // Replaces characters in |replace_chars| from anywhere in |input| with | 160 // Replaces characters in |replace_chars| from anywhere in |input| with |
| 160 // |replace_with|. Each character in |replace_chars| will be replaced with | 161 // |replace_with|. Each character in |replace_chars| will be replaced with |
| 161 // the |replace_with| string. Returns true if any characters were replaced. | 162 // the |replace_with| string. Returns true if any characters were replaced. |
| 162 // |replace_chars| must be null-terminated. | 163 // |replace_chars| must be null-terminated. |
| 163 // NOTE: Safe to use the same variable for both |input| and |output|. | 164 // NOTE: Safe to use the same variable for both |input| and |output|. |
| 164 BASE_EXPORT bool ReplaceChars(const string16& input, | 165 BASE_EXPORT bool ReplaceChars(const string16& input, |
| 165 const base::StringPiece16& replace_chars, | 166 const base::StringPiece16& replace_chars, |
| 166 const string16& replace_with, | 167 const string16& replace_with, |
| 167 string16* output); | 168 string16* output); |
| 168 BASE_EXPORT bool ReplaceChars(const std::string& input, | 169 BASE_EXPORT bool ReplaceChars(const std::string& input, |
| 169 const base::StringPiece& replace_chars, | 170 const base::StringPiece& replace_chars, |
| 170 const std::string& replace_with, | 171 const std::string& replace_with, |
| 171 std::string* output); | 172 std::string* output); |
| 172 | 173 |
| 174 enum TrimPositions { |
| 175 TRIM_NONE = 0, |
| 176 TRIM_LEADING = 1 << 0, |
| 177 TRIM_TRAILING = 1 << 1, |
| 178 TRIM_ALL = TRIM_LEADING | TRIM_TRAILING, |
| 179 }; |
| 180 |
| 173 // Removes characters in |trim_chars| from the beginning and end of |input|. | 181 // Removes characters in |trim_chars| from the beginning and end of |input|. |
| 174 // |trim_chars| must be null-terminated. | 182 // 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|. | 183 // |
| 184 // It is safe to use the same variable for both |input| and |output| (this is |
| 185 // the normal usage to trim in-place). |
| 176 BASE_EXPORT bool TrimString(const string16& input, | 186 BASE_EXPORT bool TrimString(const string16& input, |
| 177 const base::StringPiece16& trim_chars, | 187 base::StringPiece16 trim_chars, |
| 178 string16* output); | 188 string16* output); |
| 179 BASE_EXPORT bool TrimString(const std::string& input, | 189 BASE_EXPORT bool TrimString(const std::string& input, |
| 180 const base::StringPiece& trim_chars, | 190 base::StringPiece trim_chars, |
| 181 std::string* output); | 191 std::string* output); |
| 182 | 192 |
| 193 // StringPiece versions of the above. The returned pieces refer to the original |
| 194 // buffer. |
| 195 BASE_EXPORT StringPiece16 TrimString(StringPiece16 input, |
| 196 const base::StringPiece16& trim_chars, |
| 197 TrimPositions positions); |
| 198 BASE_EXPORT StringPiece TrimString(StringPiece input, |
| 199 const base::StringPiece& trim_chars, |
| 200 TrimPositions positions); |
| 201 |
| 183 // Truncates a string to the nearest UTF-8 character that will leave | 202 // Truncates a string to the nearest UTF-8 character that will leave |
| 184 // the string less than or equal to the specified byte size. | 203 // the string less than or equal to the specified byte size. |
| 185 BASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input, | 204 BASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input, |
| 186 const size_t byte_size, | 205 const size_t byte_size, |
| 187 std::string* output); | 206 std::string* output); |
| 188 | 207 |
| 189 // Trims any whitespace from either end of the input string. Returns where | 208 // Trims any whitespace from either end of the input string. Returns where |
| 190 // whitespace was found. | 209 // whitespace was found. |
| 191 // The non-wide version has two functions: | 210 // The non-wide version has two functions: |
| 192 // * TrimWhitespaceASCII() | 211 // * TrimWhitespaceASCII() |
| 193 // This function is for ASCII strings and only looks for ASCII whitespace; | 212 // This function is for ASCII strings and only looks for ASCII whitespace; |
| 194 // Please choose the best one according to your usage. | 213 // Please choose the best one according to your usage. |
| 195 // NOTE: Safe to use the same variable for both input and output. | 214 // 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, | 215 BASE_EXPORT TrimPositions TrimWhitespace(const string16& input, |
| 203 TrimPositions positions, | 216 TrimPositions positions, |
| 204 base::string16* output); | 217 base::string16* output); |
| 205 BASE_EXPORT TrimPositions TrimWhitespaceASCII(const std::string& input, | 218 BASE_EXPORT TrimPositions TrimWhitespaceASCII(const std::string& input, |
| 206 TrimPositions positions, | 219 TrimPositions positions, |
| 207 std::string* output); | 220 std::string* output); |
| 208 | 221 |
| 209 // Deprecated. This function is only for backward compatibility and calls | 222 // Deprecated. This function is only for backward compatibility and calls |
| 210 // TrimWhitespaceASCII(). | 223 // TrimWhitespaceASCII(). |
| 211 BASE_EXPORT TrimPositions TrimWhitespace(const std::string& input, | 224 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); | 456 str->reserve(length_with_null); |
| 444 str->resize(length_with_null - 1); | 457 str->resize(length_with_null - 1); |
| 445 return &((*str)[0]); | 458 return &((*str)[0]); |
| 446 } | 459 } |
| 447 | 460 |
| 448 //----------------------------------------------------------------------------- | 461 //----------------------------------------------------------------------------- |
| 449 | 462 |
| 450 // Splits a string into its fields delimited by any of the characters in | 463 // 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 | 464 // |delimiters|. Each field is added to the |tokens| vector. Returns the |
| 452 // number of tokens found. | 465 // number of tokens found. |
| 466 // |
| 467 // DEPRECATED. Use SplitStringUsingSet for new code (these just forward). |
| 468 // TODO(brettw) convert callers and delete these forwarders. |
| 453 BASE_EXPORT size_t Tokenize(const base::string16& str, | 469 BASE_EXPORT size_t Tokenize(const base::string16& str, |
| 454 const base::string16& delimiters, | 470 const base::string16& delimiters, |
| 455 std::vector<base::string16>* tokens); | 471 std::vector<base::string16>* tokens); |
| 456 BASE_EXPORT size_t Tokenize(const std::string& str, | 472 BASE_EXPORT size_t Tokenize(const std::string& str, |
| 457 const std::string& delimiters, | 473 const std::string& delimiters, |
| 458 std::vector<std::string>* tokens); | 474 std::vector<std::string>* tokens); |
| 459 BASE_EXPORT size_t Tokenize(const base::StringPiece& str, | 475 BASE_EXPORT size_t Tokenize(const base::StringPiece& str, |
| 460 const base::StringPiece& delimiters, | 476 const base::StringPiece& delimiters, |
| 461 std::vector<base::StringPiece>* tokens); | 477 std::vector<base::StringPiece>* tokens); |
| 462 | 478 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 // string can contain wildcards like * and ? | 514 // string can contain wildcards like * and ? |
| 499 // The backslash character (\) is an escape character for * and ? | 515 // The backslash character (\) is an escape character for * and ? |
| 500 // We limit the patterns to having a max of 16 * or ? characters. | 516 // We limit the patterns to having a max of 16 * or ? characters. |
| 501 // ? matches 0 or 1 character, while * matches 0 or more characters. | 517 // ? matches 0 or 1 character, while * matches 0 or more characters. |
| 502 BASE_EXPORT bool MatchPattern(const base::StringPiece& string, | 518 BASE_EXPORT bool MatchPattern(const base::StringPiece& string, |
| 503 const base::StringPiece& pattern); | 519 const base::StringPiece& pattern); |
| 504 BASE_EXPORT bool MatchPattern(const base::string16& string, | 520 BASE_EXPORT bool MatchPattern(const base::string16& string, |
| 505 const base::string16& pattern); | 521 const base::string16& pattern); |
| 506 | 522 |
| 507 #endif // BASE_STRINGS_STRING_UTIL_H_ | 523 #endif // BASE_STRINGS_STRING_UTIL_H_ |
| OLD | NEW |