| 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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 BASE_EXPORT extern const char kWhitespaceASCII[]; | 158 BASE_EXPORT extern const char kWhitespaceASCII[]; |
| 159 BASE_EXPORT extern const char16 kWhitespaceASCIIAs16[]; // No unicode. | 159 BASE_EXPORT extern const char16 kWhitespaceASCIIAs16[]; // No unicode. |
| 160 | 160 |
| 161 // Null-terminated string representing the UTF-8 byte order mark. | 161 // Null-terminated string representing the UTF-8 byte order mark. |
| 162 BASE_EXPORT extern const char kUtf8ByteOrderMark[]; | 162 BASE_EXPORT extern const char kUtf8ByteOrderMark[]; |
| 163 | 163 |
| 164 // Removes characters in |remove_chars| from anywhere in |input|. Returns true | 164 // Removes characters in |remove_chars| from anywhere in |input|. Returns true |
| 165 // if any characters were removed. |remove_chars| must be null-terminated. | 165 // if any characters were removed. |remove_chars| must be null-terminated. |
| 166 // NOTE: Safe to use the same variable for both |input| and |output|. | 166 // NOTE: Safe to use the same variable for both |input| and |output|. |
| 167 BASE_EXPORT bool RemoveChars(const string16& input, | 167 BASE_EXPORT bool RemoveChars(const string16& input, |
| 168 const base::StringPiece16& remove_chars, | 168 const StringPiece16& remove_chars, |
| 169 string16* output); | 169 string16* output); |
| 170 BASE_EXPORT bool RemoveChars(const std::string& input, | 170 BASE_EXPORT bool RemoveChars(const std::string& input, |
| 171 const base::StringPiece& remove_chars, | 171 const StringPiece& remove_chars, |
| 172 std::string* output); | 172 std::string* output); |
| 173 | 173 |
| 174 // Replaces characters in |replace_chars| from anywhere in |input| with | 174 // Replaces characters in |replace_chars| from anywhere in |input| with |
| 175 // |replace_with|. Each character in |replace_chars| will be replaced with | 175 // |replace_with|. Each character in |replace_chars| will be replaced with |
| 176 // the |replace_with| string. Returns true if any characters were replaced. | 176 // the |replace_with| string. Returns true if any characters were replaced. |
| 177 // |replace_chars| must be null-terminated. | 177 // |replace_chars| must be null-terminated. |
| 178 // NOTE: Safe to use the same variable for both |input| and |output|. | 178 // NOTE: Safe to use the same variable for both |input| and |output|. |
| 179 BASE_EXPORT bool ReplaceChars(const string16& input, | 179 BASE_EXPORT bool ReplaceChars(const string16& input, |
| 180 const base::StringPiece16& replace_chars, | 180 const StringPiece16& replace_chars, |
| 181 const string16& replace_with, | 181 const string16& replace_with, |
| 182 string16* output); | 182 string16* output); |
| 183 BASE_EXPORT bool ReplaceChars(const std::string& input, | 183 BASE_EXPORT bool ReplaceChars(const std::string& input, |
| 184 const base::StringPiece& replace_chars, | 184 const StringPiece& replace_chars, |
| 185 const std::string& replace_with, | 185 const std::string& replace_with, |
| 186 std::string* output); | 186 std::string* output); |
| 187 | 187 |
| 188 enum TrimPositions { | 188 enum TrimPositions { |
| 189 TRIM_NONE = 0, | 189 TRIM_NONE = 0, |
| 190 TRIM_LEADING = 1 << 0, | 190 TRIM_LEADING = 1 << 0, |
| 191 TRIM_TRAILING = 1 << 1, | 191 TRIM_TRAILING = 1 << 1, |
| 192 TRIM_ALL = TRIM_LEADING | TRIM_TRAILING, | 192 TRIM_ALL = TRIM_LEADING | TRIM_TRAILING, |
| 193 }; | 193 }; |
| 194 | 194 |
| 195 // Removes characters in |trim_chars| from the beginning and end of |input|. | 195 // Removes characters in |trim_chars| from the beginning and end of |input|. |
| 196 // The 8-bit version only works on 8-bit characters, not UTF-8. | 196 // The 8-bit version only works on 8-bit characters, not UTF-8. |
| 197 // | 197 // |
| 198 // It is safe to use the same variable for both |input| and |output| (this is | 198 // It is safe to use the same variable for both |input| and |output| (this is |
| 199 // the normal usage to trim in-place). | 199 // the normal usage to trim in-place). |
| 200 BASE_EXPORT bool TrimString(const string16& input, | 200 BASE_EXPORT bool TrimString(const string16& input, |
| 201 base::StringPiece16 trim_chars, | 201 StringPiece16 trim_chars, |
| 202 string16* output); | 202 string16* output); |
| 203 BASE_EXPORT bool TrimString(const std::string& input, | 203 BASE_EXPORT bool TrimString(const std::string& input, |
| 204 base::StringPiece trim_chars, | 204 StringPiece trim_chars, |
| 205 std::string* output); | 205 std::string* output); |
| 206 | 206 |
| 207 // StringPiece versions of the above. The returned pieces refer to the original | 207 // StringPiece versions of the above. The returned pieces refer to the original |
| 208 // buffer. | 208 // buffer. |
| 209 BASE_EXPORT StringPiece16 TrimString(StringPiece16 input, | 209 BASE_EXPORT StringPiece16 TrimString(StringPiece16 input, |
| 210 const base::StringPiece16& trim_chars, | 210 const StringPiece16& trim_chars, |
| 211 TrimPositions positions); | 211 TrimPositions positions); |
| 212 BASE_EXPORT StringPiece TrimString(StringPiece input, | 212 BASE_EXPORT StringPiece TrimString(StringPiece input, |
| 213 const base::StringPiece& trim_chars, | 213 const StringPiece& trim_chars, |
| 214 TrimPositions positions); | 214 TrimPositions positions); |
| 215 | 215 |
| 216 // Truncates a string to the nearest UTF-8 character that will leave | 216 // Truncates a string to the nearest UTF-8 character that will leave |
| 217 // the string less than or equal to the specified byte size. | 217 // the string less than or equal to the specified byte size. |
| 218 BASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input, | 218 BASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input, |
| 219 const size_t byte_size, | 219 const size_t byte_size, |
| 220 std::string* output); | 220 std::string* output); |
| 221 | 221 |
| 222 // Trims any whitespace from either end of the input string. | 222 // Trims any whitespace from either end of the input string. |
| 223 // | 223 // |
| 224 // The StringPiece versions return a substring referencing the input buffer. | 224 // The StringPiece versions return a substring referencing the input buffer. |
| 225 // The ASCII versions look only for ASCII whitespace. | 225 // The ASCII versions look only for ASCII whitespace. |
| 226 // | 226 // |
| 227 // The std::string versions return where whitespace was found. | 227 // The std::string versions return where whitespace was found. |
| 228 // NOTE: Safe to use the same variable for both input and output. | 228 // NOTE: Safe to use the same variable for both input and output. |
| 229 BASE_EXPORT TrimPositions TrimWhitespace(const string16& input, | 229 BASE_EXPORT TrimPositions TrimWhitespace(const string16& input, |
| 230 TrimPositions positions, | 230 TrimPositions positions, |
| 231 base::string16* output); | 231 string16* output); |
| 232 BASE_EXPORT StringPiece16 TrimWhitespace(StringPiece16 input, | 232 BASE_EXPORT StringPiece16 TrimWhitespace(StringPiece16 input, |
| 233 TrimPositions positions); | 233 TrimPositions positions); |
| 234 BASE_EXPORT TrimPositions TrimWhitespaceASCII(const std::string& input, | 234 BASE_EXPORT TrimPositions TrimWhitespaceASCII(const std::string& input, |
| 235 TrimPositions positions, | 235 TrimPositions positions, |
| 236 std::string* output); | 236 std::string* output); |
| 237 BASE_EXPORT StringPiece TrimWhitespaceASCII(StringPiece input, | 237 BASE_EXPORT StringPiece TrimWhitespaceASCII(StringPiece input, |
| 238 TrimPositions positions); | 238 TrimPositions positions); |
| 239 | 239 |
| 240 // Deprecated. This function is only for backward compatibility and calls | 240 // Deprecated. This function is only for backward compatibility and calls |
| 241 // TrimWhitespaceASCII(). | 241 // TrimWhitespaceASCII(). |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 StringPiece find_this, | 448 StringPiece find_this, |
| 449 StringPiece replace_with); | 449 StringPiece replace_with); |
| 450 | 450 |
| 451 // Starting at |start_offset| (usually 0), look through |str| and replace all | 451 // Starting at |start_offset| (usually 0), look through |str| and replace all |
| 452 // instances of |find_this| with |replace_with|. | 452 // instances of |find_this| with |replace_with|. |
| 453 // | 453 // |
| 454 // This does entire substrings; use std::replace in <algorithm> for single | 454 // This does entire substrings; use std::replace in <algorithm> for single |
| 455 // characters, for example: | 455 // characters, for example: |
| 456 // std::replace(str.begin(), str.end(), 'a', 'b'); | 456 // std::replace(str.begin(), str.end(), 'a', 'b'); |
| 457 BASE_EXPORT void ReplaceSubstringsAfterOffset( | 457 BASE_EXPORT void ReplaceSubstringsAfterOffset( |
| 458 base::string16* str, | 458 string16* str, |
| 459 size_t start_offset, | 459 size_t start_offset, |
| 460 StringPiece16 find_this, | 460 StringPiece16 find_this, |
| 461 StringPiece16 replace_with); | 461 StringPiece16 replace_with); |
| 462 BASE_EXPORT void ReplaceSubstringsAfterOffset( | 462 BASE_EXPORT void ReplaceSubstringsAfterOffset( |
| 463 std::string* str, | 463 std::string* str, |
| 464 size_t start_offset, | 464 size_t start_offset, |
| 465 StringPiece find_this, | 465 StringPiece find_this, |
| 466 StringPiece replace_with); | 466 StringPiece replace_with); |
| 467 | 467 |
| 468 // Reserves enough memory in |str| to accommodate |length_with_null| characters, | 468 // Reserves enough memory in |str| to accommodate |length_with_null| characters, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 479 // | 479 // |
| 480 // Internally, this takes linear time because the resize() call 0-fills the | 480 // Internally, this takes linear time because the resize() call 0-fills the |
| 481 // underlying array for potentially all | 481 // underlying array for potentially all |
| 482 // (|length_with_null - 1| * sizeof(string_type::value_type)) bytes. Ideally we | 482 // (|length_with_null - 1| * sizeof(string_type::value_type)) bytes. Ideally we |
| 483 // could avoid this aspect of the resize() call, as we expect the caller to | 483 // could avoid this aspect of the resize() call, as we expect the caller to |
| 484 // immediately write over this memory, but there is no other way to set the size | 484 // immediately write over this memory, but there is no other way to set the size |
| 485 // of the string, and not doing that will mean people who access |str| rather | 485 // of the string, and not doing that will mean people who access |str| rather |
| 486 // than str.c_str() will get back a string of whatever size |str| had on entry | 486 // than str.c_str() will get back a string of whatever size |str| had on entry |
| 487 // to this function (probably 0). | 487 // to this function (probably 0). |
| 488 BASE_EXPORT char* WriteInto(std::string* str, size_t length_with_null); | 488 BASE_EXPORT char* WriteInto(std::string* str, size_t length_with_null); |
| 489 BASE_EXPORT char16* WriteInto(base::string16* str, size_t length_with_null); | 489 BASE_EXPORT char16* WriteInto(string16* str, size_t length_with_null); |
| 490 #ifndef OS_WIN | 490 #ifndef OS_WIN |
| 491 BASE_EXPORT wchar_t* WriteInto(std::wstring* str, size_t length_with_null); | 491 BASE_EXPORT wchar_t* WriteInto(std::wstring* str, size_t length_with_null); |
| 492 #endif | 492 #endif |
| 493 | 493 |
| 494 // Does the opposite of SplitString(). | 494 // Does the opposite of SplitString(). |
| 495 BASE_EXPORT std::string JoinString(const std::vector<std::string>& parts, | 495 BASE_EXPORT std::string JoinString(const std::vector<std::string>& parts, |
| 496 StringPiece separator); | 496 StringPiece separator); |
| 497 BASE_EXPORT string16 JoinString(const std::vector<string16>& parts, | 497 BASE_EXPORT string16 JoinString(const std::vector<string16>& parts, |
| 498 StringPiece16 separator); | 498 StringPiece16 separator); |
| 499 | 499 |
| 500 // Replace $1-$2-$3..$9 in the format string with |a|-|b|-|c|..|i| respectively. |
| 501 // Additionally, any number of consecutive '$' characters is replaced by that |
| 502 // number less one. Eg $$->$, $$$->$$, etc. The offsets parameter here can be |
| 503 // NULL. This only allows you to use up to nine replacements. |
| 504 BASE_EXPORT string16 ReplaceStringPlaceholders( |
| 505 const string16& format_string, |
| 506 const std::vector<string16>& subst, |
| 507 std::vector<size_t>* offsets); |
| 508 |
| 509 BASE_EXPORT std::string ReplaceStringPlaceholders( |
| 510 const StringPiece& format_string, |
| 511 const std::vector<std::string>& subst, |
| 512 std::vector<size_t>* offsets); |
| 513 |
| 514 // Single-string shortcut for ReplaceStringHolders. |offset| may be NULL. |
| 515 BASE_EXPORT string16 ReplaceStringPlaceholders(const string16& format_string, |
| 516 const string16& a, |
| 517 size_t* offset); |
| 518 |
| 500 } // namespace base | 519 } // namespace base |
| 501 | 520 |
| 502 #if defined(OS_WIN) | 521 #if defined(OS_WIN) |
| 503 #include "base/strings/string_util_win.h" | 522 #include "base/strings/string_util_win.h" |
| 504 #elif defined(OS_POSIX) | 523 #elif defined(OS_POSIX) |
| 505 #include "base/strings/string_util_posix.h" | 524 #include "base/strings/string_util_posix.h" |
| 506 #else | 525 #else |
| 507 #error Define string operations appropriately for your platform | 526 #error Define string operations appropriately for your platform |
| 508 #endif | 527 #endif |
| 509 | 528 |
| 510 // Replace $1-$2-$3..$9 in the format string with |a|-|b|-|c|..|i| respectively. | |
| 511 // Additionally, any number of consecutive '$' characters is replaced by that | |
| 512 // number less one. Eg $$->$, $$$->$$, etc. The offsets parameter here can be | |
| 513 // NULL. This only allows you to use up to nine replacements. | |
| 514 BASE_EXPORT base::string16 ReplaceStringPlaceholders( | |
| 515 const base::string16& format_string, | |
| 516 const std::vector<base::string16>& subst, | |
| 517 std::vector<size_t>* offsets); | |
| 518 | |
| 519 BASE_EXPORT std::string ReplaceStringPlaceholders( | |
| 520 const base::StringPiece& format_string, | |
| 521 const std::vector<std::string>& subst, | |
| 522 std::vector<size_t>* offsets); | |
| 523 | |
| 524 // Single-string shortcut for ReplaceStringHolders. |offset| may be NULL. | |
| 525 BASE_EXPORT base::string16 ReplaceStringPlaceholders( | |
| 526 const base::string16& format_string, | |
| 527 const base::string16& a, | |
| 528 size_t* offset); | |
| 529 | |
| 530 #endif // BASE_STRINGS_STRING_UTIL_H_ | 529 #endif // BASE_STRINGS_STRING_UTIL_H_ |
| OLD | NEW |