OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_STRING_UTIL_H_ | 7 #ifndef BASE_STRING_UTIL_H_ |
8 #define BASE_STRING_UTIL_H_ | 8 #define BASE_STRING_UTIL_H_ |
9 | 9 |
10 #include <string> | 10 #include <string> |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 // These functions are threadsafe. | 120 // These functions are threadsafe. |
121 const std::string& EmptyString(); | 121 const std::string& EmptyString(); |
122 const std::wstring& EmptyWString(); | 122 const std::wstring& EmptyWString(); |
123 | 123 |
124 extern const wchar_t kWhitespaceWide[]; | 124 extern const wchar_t kWhitespaceWide[]; |
125 extern const char kWhitespaceASCII[]; | 125 extern const char kWhitespaceASCII[]; |
126 | 126 |
127 // Names of codepages (charsets) understood by icu. | 127 // Names of codepages (charsets) understood by icu. |
128 extern const char* const kCodepageUTF8; | 128 extern const char* const kCodepageUTF8; |
129 | 129 |
| 130 // When some function takes an optional parameter to control locale |
| 131 // independence, you should use values from here to make function call |
| 132 // look more descriptive. |
| 133 enum LocaleDependence { |
| 134 LOCALE_DEPENDENT = true, |
| 135 LOCALE_INDEPENDENT = false |
| 136 }; |
| 137 |
130 // Removes characters in trim_chars from the beginning and end of input. | 138 // Removes characters in trim_chars from the beginning and end of input. |
131 // NOTE: Safe to use the same variable for both input and output. | 139 // NOTE: Safe to use the same variable for both input and output. |
132 bool TrimString(const std::wstring& input, | 140 bool TrimString(const std::wstring& input, |
133 const wchar_t trim_chars[], | 141 const wchar_t trim_chars[], |
134 std::wstring* output); | 142 std::wstring* output); |
135 bool TrimString(const std::string& input, | 143 bool TrimString(const std::string& input, |
136 const char trim_chars[], | 144 const char trim_chars[], |
137 std::string* output); | 145 std::string* output); |
138 | 146 |
139 // Trims any whitespace from either end of the input string. Returns where | 147 // Trims any whitespace from either end of the input string. Returns where |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 // Specialized string-conversion functions. | 355 // Specialized string-conversion functions. |
348 std::string IntToString(int value); | 356 std::string IntToString(int value); |
349 std::wstring IntToWString(int value); | 357 std::wstring IntToWString(int value); |
350 std::string UintToString(unsigned int value); | 358 std::string UintToString(unsigned int value); |
351 std::wstring UintToWString(unsigned int value); | 359 std::wstring UintToWString(unsigned int value); |
352 std::string Int64ToString(int64 value); | 360 std::string Int64ToString(int64 value); |
353 std::wstring Int64ToWString(int64 value); | 361 std::wstring Int64ToWString(int64 value); |
354 std::string Uint64ToString(uint64 value); | 362 std::string Uint64ToString(uint64 value); |
355 std::wstring Uint64ToWString(uint64 value); | 363 std::wstring Uint64ToWString(uint64 value); |
356 | 364 |
| 365 std::string DoubleToString(double value, LocaleDependence locale_dependent); |
| 366 std::wstring DoubleToWString(double value, LocaleDependence locale_dependent); |
| 367 |
357 // Perform a best-effort conversion of the input string to a numeric type, | 368 // Perform a best-effort conversion of the input string to a numeric type, |
358 // setting |*output| to the result of the conversion. Returns true for | 369 // setting |*output| to the result of the conversion. Returns true for |
359 // "perfect" conversions; returns false in the following cases: | 370 // "perfect" conversions; returns false in the following cases: |
360 // - Overflow/underflow. |*output| will be set to the maximum value supported | 371 // - Overflow/underflow. |*output| will be set to the maximum value supported |
361 // by the data type. | 372 // by the data type. |
362 // - Trailing characters in the string after parsing the number. |*output| | 373 // - Trailing characters in the string after parsing the number. |*output| |
363 // will be set to the value of the number that was parsed. | 374 // will be set to the value of the number that was parsed. |
364 // - No characters parseable as a number at the beginning of the string. | 375 // - No characters parseable as a number at the beginning of the string. |
365 // |*output| will be set to 0. | 376 // |*output| will be set to 0. |
366 // - Empty string. |*output| will be set to 0. | 377 // - Empty string. |*output| will be set to 0. |
367 bool StringToInt(const std::string& input, int* output); | 378 bool StringToInt(const std::string& input, int* output); |
368 bool StringToInt(const std::wstring& input, int* output); | 379 bool StringToInt(const std::wstring& input, int* output); |
369 bool StringToInt64(const std::string& input, int64* output); | 380 bool StringToInt64(const std::string& input, int64* output); |
370 bool StringToInt64(const std::wstring& input, int64* output); | 381 bool StringToInt64(const std::wstring& input, int64* output); |
371 bool HexStringToInt(const std::string& input, int* output); | 382 bool HexStringToInt(const std::string& input, int* output); |
372 bool HexStringToInt(const std::wstring& input, int* output); | 383 bool HexStringToInt(const std::wstring& input, int* output); |
373 | 384 |
374 // For floating-point conversions, only conversions of input strings in decimal | 385 // For floating-point conversions, only conversions of input strings in decimal |
375 // form are defined to work. Behavior with strings representing floating-point | 386 // form are defined to work. Behavior with strings representing floating-point |
376 // numbers in hexadecimal, and strings representing non-fininte values (such | 387 // numbers in hexadecimal, and strings representing non-fininte values (such |
377 // as NaN and inf) is undefined. Otherwise, these behave the same as the | 388 // as NaN and inf) is undefined. Otherwise, these behave the same as the |
378 // integral variants above. | 389 // integral variants above. |
| 390 // |
| 391 // By default, locale-dependent variant is used. |
379 bool StringToDouble(const std::string& input, double* output); | 392 bool StringToDouble(const std::string& input, double* output); |
| 393 bool StringToDouble(const std::string& input, double* output, |
| 394 LocaleDependence locale_dependent); |
380 bool StringToDouble(const std::wstring& input, double* output); | 395 bool StringToDouble(const std::wstring& input, double* output); |
| 396 bool StringToDouble(const std::wstring& input, double* output, |
| 397 LocaleDependence locale_dependent); |
381 | 398 |
382 // Convenience forms of the above, when the caller is uninterested in the | 399 // Convenience forms of the above, when the caller is uninterested in the |
383 // boolean return value. These return only the |*output| value from the | 400 // boolean return value. These return only the |*output| value from the |
384 // above conversions: a best-effort conversion when possible, otherwise, 0. | 401 // above conversions: a best-effort conversion when possible, otherwise, 0. |
385 int StringToInt(const std::string& value); | 402 int StringToInt(const std::string& value); |
386 int StringToInt(const std::wstring& value); | 403 int StringToInt(const std::wstring& value); |
387 int64 StringToInt64(const std::string& value); | 404 int64 StringToInt64(const std::string& value); |
388 int64 StringToInt64(const std::wstring& value); | 405 int64 StringToInt64(const std::wstring& value); |
389 int HexStringToInt(const std::string& value); | 406 int HexStringToInt(const std::string& value); |
390 int HexStringToInt(const std::wstring& value); | 407 int HexStringToInt(const std::wstring& value); |
| 408 // By default, locale-dependent variant is used. |
391 double StringToDouble(const std::string& value); | 409 double StringToDouble(const std::string& value); |
| 410 double StringToDouble(const std::string& value, |
| 411 LocaleDependence locale_dependent); |
392 double StringToDouble(const std::wstring& value); | 412 double StringToDouble(const std::wstring& value); |
| 413 double StringToDouble(const std::wstring& value, |
| 414 LocaleDependence locale_dependent); |
393 | 415 |
394 // Return a C++ string given printf-like input. | 416 // Return a C++ string given printf-like input. |
395 std::string StringPrintf(const char* format, ...); | 417 std::string StringPrintf(const char* format, ...); |
396 std::wstring StringPrintf(const wchar_t* format, ...); | 418 std::wstring StringPrintf(const wchar_t* format, ...); |
397 | 419 |
398 // Store result into a supplied string and return it | 420 // Store result into a supplied string and return it |
399 const std::string& SStringPrintf(std::string* dst, const char* format, ...); | 421 const std::string& SStringPrintf(std::string* dst, const char* format, ...); |
400 const std::wstring& SStringPrintf(std::wstring* dst, | 422 const std::wstring& SStringPrintf(std::wstring* dst, |
401 const wchar_t* format, ...); | 423 const wchar_t* format, ...); |
402 | 424 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 // Returns true if the string passed in matches the pattern. The pattern | 543 // Returns true if the string passed in matches the pattern. The pattern |
522 // string can contain wildcards like * and ? | 544 // string can contain wildcards like * and ? |
523 // TODO(iyengar) This function may not work correctly for CJK strings as | 545 // TODO(iyengar) This function may not work correctly for CJK strings as |
524 // it does individual character matches. | 546 // it does individual character matches. |
525 // The backslash character (\) is an escape character for * and ? | 547 // The backslash character (\) is an escape character for * and ? |
526 bool MatchPattern(const std::wstring& string, const std::wstring& pattern); | 548 bool MatchPattern(const std::wstring& string, const std::wstring& pattern); |
527 bool MatchPattern(const std::string& string, const std::string& pattern); | 549 bool MatchPattern(const std::string& string, const std::string& pattern); |
528 | 550 |
529 #endif // BASE_STRING_UTIL_H_ | 551 #endif // BASE_STRING_UTIL_H_ |
530 | 552 |
OLD | NEW |