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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 BASE_EXPORT extern const char16 kWhitespaceUTF16[]; | 143 BASE_EXPORT extern const char16 kWhitespaceUTF16[]; |
144 BASE_EXPORT extern const char kWhitespaceASCII[]; | 144 BASE_EXPORT extern const char kWhitespaceASCII[]; |
145 | 145 |
146 // Null-terminated string representing the UTF-8 byte order mark. | 146 // Null-terminated string representing the UTF-8 byte order mark. |
147 BASE_EXPORT extern const char kUtf8ByteOrderMark[]; | 147 BASE_EXPORT extern const char kUtf8ByteOrderMark[]; |
148 | 148 |
149 // Removes characters in |remove_chars| from anywhere in |input|. Returns true | 149 // Removes characters in |remove_chars| from anywhere in |input|. Returns true |
150 // if any characters were removed. |remove_chars| must be null-terminated. | 150 // if any characters were removed. |remove_chars| must be null-terminated. |
151 // NOTE: Safe to use the same variable for both |input| and |output|. | 151 // NOTE: Safe to use the same variable for both |input| and |output|. |
152 BASE_EXPORT bool RemoveChars(const string16& input, | 152 BASE_EXPORT bool RemoveChars(const string16& input, |
153 const char16 remove_chars[], | 153 const base::StringPiece16& remove_chars, |
154 string16* output); | 154 string16* output); |
155 BASE_EXPORT bool RemoveChars(const std::string& input, | 155 BASE_EXPORT bool RemoveChars(const std::string& input, |
156 const char remove_chars[], | 156 const base::StringPiece& remove_chars, |
157 std::string* output); | 157 std::string* output); |
158 | 158 |
159 // Replaces characters in |replace_chars| from anywhere in |input| with | 159 // Replaces characters in |replace_chars| from anywhere in |input| with |
160 // |replace_with|. Each character in |replace_chars| will be replaced with | 160 // |replace_with|. Each character in |replace_chars| will be replaced with |
161 // the |replace_with| string. Returns true if any characters were replaced. | 161 // the |replace_with| string. Returns true if any characters were replaced. |
162 // |replace_chars| must be null-terminated. | 162 // |replace_chars| must be null-terminated. |
163 // NOTE: Safe to use the same variable for both |input| and |output|. | 163 // NOTE: Safe to use the same variable for both |input| and |output|. |
164 BASE_EXPORT bool ReplaceChars(const string16& input, | 164 BASE_EXPORT bool ReplaceChars(const string16& input, |
165 const char16 replace_chars[], | 165 const base::StringPiece16& replace_chars, |
166 const string16& replace_with, | 166 const string16& replace_with, |
167 string16* output); | 167 string16* output); |
168 BASE_EXPORT bool ReplaceChars(const std::string& input, | 168 BASE_EXPORT bool ReplaceChars(const std::string& input, |
169 const char replace_chars[], | 169 const base::StringPiece& replace_chars, |
170 const std::string& replace_with, | 170 const std::string& replace_with, |
171 std::string* output); | 171 std::string* output); |
172 | 172 |
173 // Removes characters in |trim_chars| from the beginning and end of |input|. | 173 // Removes characters in |trim_chars| from the beginning and end of |input|. |
174 // |trim_chars| must be null-terminated. | 174 // |trim_chars| must be null-terminated. |
175 // NOTE: Safe to use the same variable for both |input| and |output|. | 175 // NOTE: Safe to use the same variable for both |input| and |output|. |
176 BASE_EXPORT bool TrimString(const string16& input, | 176 BASE_EXPORT bool TrimString(const string16& input, |
177 const char16 trim_chars[], | 177 const base::StringPiece16& trim_chars, |
178 string16* output); | 178 string16* output); |
179 BASE_EXPORT bool TrimString(const std::string& input, | 179 BASE_EXPORT bool TrimString(const std::string& input, |
180 const char trim_chars[], | 180 const base::StringPiece& trim_chars, |
181 std::string* output); | 181 std::string* output); |
182 | 182 |
183 // Truncates a string to the nearest UTF-8 character that will leave | 183 // Truncates a string to the nearest UTF-8 character that will leave |
184 // the string less than or equal to the specified byte size. | 184 // the string less than or equal to the specified byte size. |
185 BASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input, | 185 BASE_EXPORT void TruncateUTF8ToByteSize(const std::string& input, |
186 const size_t byte_size, | 186 const size_t byte_size, |
187 std::string* output); | 187 std::string* output); |
188 | 188 |
189 // Trims any whitespace from either end of the input string. Returns where | 189 // Trims any whitespace from either end of the input string. Returns where |
190 // whitespace was found. | 190 // whitespace was found. |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 // Return a byte string in human-readable format with a unit suffix. Not | 371 // Return a byte string in human-readable format with a unit suffix. Not |
372 // appropriate for use in any UI; use of FormatBytes and friends in ui/base is | 372 // appropriate for use in any UI; use of FormatBytes and friends in ui/base is |
373 // highly recommended instead. TODO(avi): Figure out how to get callers to use | 373 // highly recommended instead. TODO(avi): Figure out how to get callers to use |
374 // FormatBytes instead; remove this. | 374 // FormatBytes instead; remove this. |
375 BASE_EXPORT base::string16 FormatBytesUnlocalized(int64 bytes); | 375 BASE_EXPORT base::string16 FormatBytesUnlocalized(int64 bytes); |
376 | 376 |
377 // Starting at |start_offset| (usually 0), replace the first instance of | 377 // Starting at |start_offset| (usually 0), replace the first instance of |
378 // |find_this| with |replace_with|. | 378 // |find_this| with |replace_with|. |
379 BASE_EXPORT void ReplaceFirstSubstringAfterOffset( | 379 BASE_EXPORT void ReplaceFirstSubstringAfterOffset( |
380 base::string16* str, | 380 base::string16* str, |
381 base::string16::size_type start_offset, | 381 size_t start_offset, |
382 const base::string16& find_this, | 382 const base::string16& find_this, |
383 const base::string16& replace_with); | 383 const base::string16& replace_with); |
384 BASE_EXPORT void ReplaceFirstSubstringAfterOffset( | 384 BASE_EXPORT void ReplaceFirstSubstringAfterOffset( |
385 std::string* str, | 385 std::string* str, |
386 std::string::size_type start_offset, | 386 size_t start_offset, |
387 const std::string& find_this, | 387 const std::string& find_this, |
388 const std::string& replace_with); | 388 const std::string& replace_with); |
389 | 389 |
390 // Starting at |start_offset| (usually 0), look through |str| and replace all | 390 // Starting at |start_offset| (usually 0), look through |str| and replace all |
391 // instances of |find_this| with |replace_with|. | 391 // instances of |find_this| with |replace_with|. |
392 // | 392 // |
393 // This does entire substrings; use std::replace in <algorithm> for single | 393 // This does entire substrings; use std::replace in <algorithm> for single |
394 // characters, for example: | 394 // characters, for example: |
395 // std::replace(str.begin(), str.end(), 'a', 'b'); | 395 // std::replace(str.begin(), str.end(), 'a', 'b'); |
396 BASE_EXPORT void ReplaceSubstringsAfterOffset( | 396 BASE_EXPORT void ReplaceSubstringsAfterOffset( |
397 base::string16* str, | 397 base::string16* str, |
398 base::string16::size_type start_offset, | 398 size_t start_offset, |
399 const base::string16& find_this, | 399 const base::string16& find_this, |
400 const base::string16& replace_with); | 400 const base::string16& replace_with); |
401 BASE_EXPORT void ReplaceSubstringsAfterOffset( | 401 BASE_EXPORT void ReplaceSubstringsAfterOffset(std::string* str, |
402 std::string* str, | 402 size_t start_offset, |
403 std::string::size_type start_offset, | 403 const std::string& find_this, |
404 const std::string& find_this, | 404 const std::string& replace_with); |
405 const std::string& replace_with); | |
406 | 405 |
407 // Reserves enough memory in |str| to accommodate |length_with_null| characters, | 406 // Reserves enough memory in |str| to accommodate |length_with_null| characters, |
408 // sets the size of |str| to |length_with_null - 1| characters, and returns a | 407 // sets the size of |str| to |length_with_null - 1| characters, and returns a |
409 // pointer to the underlying contiguous array of characters. This is typically | 408 // pointer to the underlying contiguous array of characters. This is typically |
410 // used when calling a function that writes results into a character array, but | 409 // used when calling a function that writes results into a character array, but |
411 // the caller wants the data to be managed by a string-like object. It is | 410 // the caller wants the data to be managed by a string-like object. It is |
412 // convenient in that is can be used inline in the call, and fast in that it | 411 // convenient in that is can be used inline in the call, and fast in that it |
413 // avoids copying the results of the call from a char* into a string. | 412 // avoids copying the results of the call from a char* into a string. |
414 // | 413 // |
415 // |length_with_null| must be at least 2, since otherwise the underlying string | 414 // |length_with_null| must be at least 2, since otherwise the underlying string |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 #elif defined(WCHAR_T_IS_UTF32) | 514 #elif defined(WCHAR_T_IS_UTF32) |
516 typedef uint32 Unsigned; | 515 typedef uint32 Unsigned; |
517 #endif | 516 #endif |
518 }; | 517 }; |
519 template<> | 518 template<> |
520 struct ToUnsigned<short> { | 519 struct ToUnsigned<short> { |
521 typedef unsigned short Unsigned; | 520 typedef unsigned short Unsigned; |
522 }; | 521 }; |
523 | 522 |
524 #endif // BASE_STRINGS_STRING_UTIL_H_ | 523 #endif // BASE_STRINGS_STRING_UTIL_H_ |
OLD | NEW |