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 <stdarg.h> // va_list | 10 #include <stdarg.h> // va_list |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 // setting |*output| to the result of the conversion. Returns true for | 401 // setting |*output| to the result of the conversion. Returns true for |
402 // "perfect" conversions; returns false in the following cases: | 402 // "perfect" conversions; returns false in the following cases: |
403 // - Overflow/underflow. |*output| will be set to the maximum value supported | 403 // - Overflow/underflow. |*output| will be set to the maximum value supported |
404 // by the data type. | 404 // by the data type. |
405 // - Trailing characters in the string after parsing the number. |*output| | 405 // - Trailing characters in the string after parsing the number. |*output| |
406 // will be set to the value of the number that was parsed. | 406 // will be set to the value of the number that was parsed. |
407 // - No characters parseable as a number at the beginning of the string. | 407 // - No characters parseable as a number at the beginning of the string. |
408 // |*output| will be set to 0. | 408 // |*output| will be set to 0. |
409 // - Empty string. |*output| will be set to 0. | 409 // - Empty string. |*output| will be set to 0. |
410 bool StringToInt(const std::string& input, int* output); | 410 bool StringToInt(const std::string& input, int* output); |
411 bool StringToInt(const std::wstring& input, int* output); | 411 bool StringToInt(const string16& input, int* output); |
412 bool StringToInt64(const std::string& input, int64* output); | 412 bool StringToInt64(const std::string& input, int64* output); |
413 bool StringToInt64(const std::wstring& input, int64* output); | 413 bool StringToInt64(const string16& input, int64* output); |
414 bool HexStringToInt(const std::string& input, int* output); | 414 bool HexStringToInt(const std::string& input, int* output); |
415 bool HexStringToInt(const std::wstring& input, int* output); | 415 bool HexStringToInt(const string16& input, int* output); |
416 | 416 |
417 // Similar to the previous functions, except that output is a vector of bytes. | 417 // Similar to the previous functions, except that output is a vector of bytes. |
418 // |*output| will contain as many bytes as were successfully parsed prior to the | 418 // |*output| will contain as many bytes as were successfully parsed prior to the |
419 // error. There is no overflow, but input.size() must be evenly divisible by 2. | 419 // error. There is no overflow, but input.size() must be evenly divisible by 2. |
420 // Leading 0x or +/- are not allowed. | 420 // Leading 0x or +/- are not allowed. |
421 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output); | 421 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output); |
422 bool HexStringToBytes(const std::wstring& input, std::vector<uint8>* output); | 422 bool HexStringToBytes(const string16& input, std::vector<uint8>* output); |
423 | 423 |
424 // For floating-point conversions, only conversions of input strings in decimal | 424 // For floating-point conversions, only conversions of input strings in decimal |
425 // form are defined to work. Behavior with strings representing floating-point | 425 // form are defined to work. Behavior with strings representing floating-point |
426 // numbers in hexadecimal, and strings representing non-fininte values (such as | 426 // numbers in hexadecimal, and strings representing non-fininte values (such as |
427 // NaN and inf) is undefined. Otherwise, these behave the same as the integral | 427 // NaN and inf) is undefined. Otherwise, these behave the same as the integral |
428 // variants. This expects the input string to NOT be specific to the locale. | 428 // variants. This expects the input string to NOT be specific to the locale. |
429 // If your input is locale specific, use ICU to read the number. | 429 // If your input is locale specific, use ICU to read the number. |
430 bool StringToDouble(const std::string& input, double* output); | 430 bool StringToDouble(const std::string& input, double* output); |
431 bool StringToDouble(const std::wstring& input, double* output); | 431 bool StringToDouble(const string16& input, double* output); |
432 | 432 |
433 // Convenience forms of the above, when the caller is uninterested in the | 433 // Convenience forms of the above, when the caller is uninterested in the |
434 // boolean return value. These return only the |*output| value from the | 434 // boolean return value. These return only the |*output| value from the |
435 // above conversions: a best-effort conversion when possible, otherwise, 0. | 435 // above conversions: a best-effort conversion when possible, otherwise, 0. |
436 int StringToInt(const std::string& value); | 436 int StringToInt(const std::string& value); |
437 int StringToInt(const std::wstring& value); | 437 int StringToInt(const string16& value); |
438 int64 StringToInt64(const std::string& value); | 438 int64 StringToInt64(const std::string& value); |
439 int64 StringToInt64(const std::wstring& value); | 439 int64 StringToInt64(const string16& value); |
440 int HexStringToInt(const std::string& value); | 440 int HexStringToInt(const std::string& value); |
441 int HexStringToInt(const std::wstring& value); | 441 int HexStringToInt(const string16& value); |
442 double StringToDouble(const std::string& value); | 442 double StringToDouble(const std::string& value); |
443 double StringToDouble(const std::wstring& value); | 443 double StringToDouble(const string16& value); |
444 | 444 |
445 // Return a C++ string given printf-like input. | 445 // Return a C++ string given printf-like input. |
446 std::string StringPrintf(const char* format, ...); | 446 std::string StringPrintf(const char* format, ...); |
447 std::wstring StringPrintf(const wchar_t* format, ...); | 447 std::wstring StringPrintf(const wchar_t* format, ...); |
448 | 448 |
449 // Store result into a supplied string and return it | 449 // Store result into a supplied string and return it |
450 const std::string& SStringPrintf(std::string* dst, const char* format, ...); | 450 const std::string& SStringPrintf(std::string* dst, const char* format, ...); |
451 const std::wstring& SStringPrintf(std::wstring* dst, | 451 const std::wstring& SStringPrintf(std::wstring* dst, |
452 const wchar_t* format, ...); | 452 const wchar_t* format, ...); |
453 | 453 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 // Returns a hex string representation of a binary buffer. | 590 // Returns a hex string representation of a binary buffer. |
591 // The returned hex string will be in upper case. | 591 // The returned hex string will be in upper case. |
592 // This function does not check if |size| is within reasonable limits since | 592 // This function does not check if |size| is within reasonable limits since |
593 // it's written with trusted data in mind. | 593 // it's written with trusted data in mind. |
594 // If you suspect that the data you want to format might be large, | 594 // If you suspect that the data you want to format might be large, |
595 // the absolute max size for |size| should be is | 595 // the absolute max size for |size| should be is |
596 // std::numeric_limits<size_t>::max() / 2 | 596 // std::numeric_limits<size_t>::max() / 2 |
597 std::string HexEncode(const void* bytes, size_t size); | 597 std::string HexEncode(const void* bytes, size_t size); |
598 | 598 |
599 #endif // BASE_STRING_UTIL_H_ | 599 #endif // BASE_STRING_UTIL_H_ |
OLD | NEW |