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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 // - 'F', which is not identified by Windows wprintf documentation. | 88 // - 'F', which is not identified by Windows wprintf documentation. |
89 // - 'D', 'O', and 'U', which are deprecated and not available on all systems. | 89 // - 'D', 'O', and 'U', which are deprecated and not available on all systems. |
90 // Use %ld, %lo, and %lu instead. | 90 // Use %ld, %lo, and %lu instead. |
91 // | 91 // |
92 // Note that there is no portable conversion specifier for char data when | 92 // Note that there is no portable conversion specifier for char data when |
93 // working with wprintf. | 93 // working with wprintf. |
94 // | 94 // |
95 // This function is intended to be called from base::vswprintf. | 95 // This function is intended to be called from base::vswprintf. |
96 bool IsWprintfFormatPortable(const wchar_t* format); | 96 bool IsWprintfFormatPortable(const wchar_t* format); |
97 | 97 |
98 enum LocaleDependence { | |
99 LOCALE_DEPENDENT, | |
100 LOCALE_INDEPENDENT | |
101 }; | |
102 | |
103 std::string DoubleToString(double value, LocaleDependence locale_dependent); | |
104 std::wstring DoubleToWString(double value, LocaleDependence locale_dependent); | |
105 | |
106 bool StringToDouble(const std::string& input, double* output, | |
107 LocaleDependence locale_dependent); | |
108 bool StringToDouble(const std::wstring& input, double* output, | |
109 LocaleDependence locale_dependent); | |
110 double StringToDouble(const std::string& value, | |
111 LocaleDependence locale_dependent); | |
112 double StringToDouble(const std::wstring& value, | |
113 LocaleDependence locale_dependent); | |
114 | |
115 } // namespace base | 98 } // namespace base |
116 | 99 |
117 #if defined(OS_WIN) | 100 #if defined(OS_WIN) |
118 #include "base/string_util_win.h" | 101 #include "base/string_util_win.h" |
119 #elif defined(OS_POSIX) | 102 #elif defined(OS_POSIX) |
120 #include "base/string_util_posix.h" | 103 #include "base/string_util_posix.h" |
121 #else | 104 #else |
122 #error Define string operations appropriately for your platform | 105 #error Define string operations appropriately for your platform |
123 #endif | 106 #endif |
124 | 107 |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 | 346 |
364 // Specialized string-conversion functions. | 347 // Specialized string-conversion functions. |
365 std::string IntToString(int value); | 348 std::string IntToString(int value); |
366 std::wstring IntToWString(int value); | 349 std::wstring IntToWString(int value); |
367 std::string UintToString(unsigned int value); | 350 std::string UintToString(unsigned int value); |
368 std::wstring UintToWString(unsigned int value); | 351 std::wstring UintToWString(unsigned int value); |
369 std::string Int64ToString(int64 value); | 352 std::string Int64ToString(int64 value); |
370 std::wstring Int64ToWString(int64 value); | 353 std::wstring Int64ToWString(int64 value); |
371 std::string Uint64ToString(uint64 value); | 354 std::string Uint64ToString(uint64 value); |
372 std::wstring Uint64ToWString(uint64 value); | 355 std::wstring Uint64ToWString(uint64 value); |
373 | 356 // The DoubleToString methods convert the double to a string format that |
| 357 // ignores the locale. If you want to use locale specific formatting, use ICU. |
| 358 std::string DoubleToString(double value); |
| 359 std::wstring DoubleToWString(double value); |
374 | 360 |
375 // Perform a best-effort conversion of the input string to a numeric type, | 361 // Perform a best-effort conversion of the input string to a numeric type, |
376 // setting |*output| to the result of the conversion. Returns true for | 362 // setting |*output| to the result of the conversion. Returns true for |
377 // "perfect" conversions; returns false in the following cases: | 363 // "perfect" conversions; returns false in the following cases: |
378 // - Overflow/underflow. |*output| will be set to the maximum value supported | 364 // - Overflow/underflow. |*output| will be set to the maximum value supported |
379 // by the data type. | 365 // by the data type. |
380 // - Trailing characters in the string after parsing the number. |*output| | 366 // - Trailing characters in the string after parsing the number. |*output| |
381 // will be set to the value of the number that was parsed. | 367 // will be set to the value of the number that was parsed. |
382 // - No characters parseable as a number at the beginning of the string. | 368 // - No characters parseable as a number at the beginning of the string. |
383 // |*output| will be set to 0. | 369 // |*output| will be set to 0. |
384 // - Empty string. |*output| will be set to 0. | 370 // - Empty string. |*output| will be set to 0. |
385 bool StringToInt(const std::string& input, int* output); | 371 bool StringToInt(const std::string& input, int* output); |
386 bool StringToInt(const std::wstring& input, int* output); | 372 bool StringToInt(const std::wstring& input, int* output); |
387 bool StringToInt64(const std::string& input, int64* output); | 373 bool StringToInt64(const std::string& input, int64* output); |
388 bool StringToInt64(const std::wstring& input, int64* output); | 374 bool StringToInt64(const std::wstring& input, int64* output); |
389 bool HexStringToInt(const std::string& input, int* output); | 375 bool HexStringToInt(const std::string& input, int* output); |
390 bool HexStringToInt(const std::wstring& input, int* output); | 376 bool HexStringToInt(const std::wstring& input, int* output); |
391 | 377 |
392 // For floating-point conversions, only conversions of input strings in decimal | 378 // For floating-point conversions, only conversions of input strings in decimal |
393 // form are defined to work. Behavior with strings representing floating-point | 379 // form are defined to work. Behavior with strings representing floating-point |
394 // numbers in hexadecimal, and strings representing non-fininte values (such | 380 // numbers in hexadecimal, and strings representing non-fininte values (such as |
395 // as NaN and inf) is undefined. Otherwise, these behave the same as the | 381 // NaN and inf) is undefined. Otherwise, these behave the same as the integral |
396 // integral variants above. By default, locale-dependent variant is used. | 382 // variants. This expects the input string to NOT be specific to the locale. |
| 383 // If your input is locale specific, use ICU to read the number. |
397 bool StringToDouble(const std::string& input, double* output); | 384 bool StringToDouble(const std::string& input, double* output); |
398 bool StringToDouble(const std::wstring& input, double* output); | 385 bool StringToDouble(const std::wstring& input, double* output); |
399 | 386 |
400 // Convenience forms of the above, when the caller is uninterested in the | 387 // Convenience forms of the above, when the caller is uninterested in the |
401 // boolean return value. These return only the |*output| value from the | 388 // boolean return value. These return only the |*output| value from the |
402 // above conversions: a best-effort conversion when possible, otherwise, 0. | 389 // above conversions: a best-effort conversion when possible, otherwise, 0. |
403 int StringToInt(const std::string& value); | 390 int StringToInt(const std::string& value); |
404 int StringToInt(const std::wstring& value); | 391 int StringToInt(const std::wstring& value); |
405 int64 StringToInt64(const std::string& value); | 392 int64 StringToInt64(const std::string& value); |
406 int64 StringToInt64(const std::wstring& value); | 393 int64 StringToInt64(const std::wstring& value); |
407 int HexStringToInt(const std::string& value); | 394 int HexStringToInt(const std::string& value); |
408 int HexStringToInt(const std::wstring& value); | 395 int HexStringToInt(const std::wstring& value); |
409 // By default, locale-dependent variant is used. | |
410 double StringToDouble(const std::string& value); | 396 double StringToDouble(const std::string& value); |
411 double StringToDouble(const std::wstring& value); | 397 double StringToDouble(const std::wstring& value); |
412 | 398 |
413 // Return a C++ string given printf-like input. | 399 // Return a C++ string given printf-like input. |
414 std::string StringPrintf(const char* format, ...); | 400 std::string StringPrintf(const char* format, ...); |
415 std::wstring StringPrintf(const wchar_t* format, ...); | 401 std::wstring StringPrintf(const wchar_t* format, ...); |
416 | 402 |
417 // Store result into a supplied string and return it | 403 // Store result into a supplied string and return it |
418 const std::string& SStringPrintf(std::string* dst, const char* format, ...); | 404 const std::string& SStringPrintf(std::string* dst, const char* format, ...); |
419 const std::wstring& SStringPrintf(std::wstring* dst, | 405 const std::wstring& SStringPrintf(std::wstring* dst, |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 // Returns true if the string passed in matches the pattern. The pattern | 526 // Returns true if the string passed in matches the pattern. The pattern |
541 // string can contain wildcards like * and ? | 527 // string can contain wildcards like * and ? |
542 // TODO(iyengar) This function may not work correctly for CJK strings as | 528 // TODO(iyengar) This function may not work correctly for CJK strings as |
543 // it does individual character matches. | 529 // it does individual character matches. |
544 // The backslash character (\) is an escape character for * and ? | 530 // The backslash character (\) is an escape character for * and ? |
545 bool MatchPattern(const std::wstring& string, const std::wstring& pattern); | 531 bool MatchPattern(const std::wstring& string, const std::wstring& pattern); |
546 bool MatchPattern(const std::string& string, const std::string& pattern); | 532 bool MatchPattern(const std::string& string, const std::string& pattern); |
547 | 533 |
548 #endif // BASE_STRING_UTIL_H_ | 534 #endif // BASE_STRING_UTIL_H_ |
549 | 535 |
OLD | NEW |