[email protected] | d2d0b6b | 2012-01-26 00:27:29 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_STRING_NUMBER_CONVERSIONS_H_ |
| 6 | #define BASE_STRING_NUMBER_CONVERSIONS_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 11 | #include "base/base_export.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 12 | #include "base/basictypes.h" |
[email protected] | eb72b27 | 2011-12-19 16:10:55 | [diff] [blame] | 13 | #include "base/string_piece.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 14 | #include "base/string16.h" |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // IMPORTANT MESSAGE FROM YOUR SPONSOR |
| 18 | // |
| 19 | // This file contains no "wstring" variants. New code should use string16. If |
| 20 | // you need to make old code work, use the UTF8 version and convert. Please do |
| 21 | // not add wstring variants. |
| 22 | // |
| 23 | // Please do not add "convenience" functions for converting strings to integers |
| 24 | // that return the value and ignore success/failure. That encourages people to |
| 25 | // write code that doesn't properly handle the error conditions. |
| 26 | // ---------------------------------------------------------------------------- |
| 27 | |
| 28 | namespace base { |
| 29 | |
| 30 | // Number -> string conversions ------------------------------------------------ |
| 31 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 32 | BASE_EXPORT std::string IntToString(int value); |
| 33 | BASE_EXPORT string16 IntToString16(int value); |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 34 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 35 | BASE_EXPORT std::string UintToString(unsigned value); |
| 36 | BASE_EXPORT string16 UintToString16(unsigned value); |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 37 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 38 | BASE_EXPORT std::string Int64ToString(int64 value); |
| 39 | BASE_EXPORT string16 Int64ToString16(int64 value); |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 40 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 41 | BASE_EXPORT std::string Uint64ToString(uint64 value); |
| 42 | BASE_EXPORT string16 Uint64ToString16(uint64 value); |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 43 | |
| 44 | // DoubleToString converts the double to a string format that ignores the |
| 45 | // locale. If you want to use locale specific formatting, use ICU. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 46 | BASE_EXPORT std::string DoubleToString(double value); |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 47 | |
| 48 | // String -> number conversions ------------------------------------------------ |
| 49 | |
| 50 | // Perform a best-effort conversion of the input string to a numeric type, |
| 51 | // setting |*output| to the result of the conversion. Returns true for |
| 52 | // "perfect" conversions; returns false in the following cases: |
| 53 | // - Overflow/underflow. |*output| will be set to the maximum value supported |
| 54 | // by the data type. |
| 55 | // - Trailing characters in the string after parsing the number. |*output| |
| 56 | // will be set to the value of the number that was parsed. |
[email protected] | cfcf2da | 2010-10-21 14:05:38 | [diff] [blame] | 57 | // - Leading whitespace in the string before parsing the number. |*output| will |
| 58 | // be set to the value of the number that was parsed. |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 59 | // - No characters parseable as a number at the beginning of the string. |
| 60 | // |*output| will be set to 0. |
| 61 | // - Empty string. |*output| will be set to 0. |
[email protected] | eb72b27 | 2011-12-19 16:10:55 | [diff] [blame] | 62 | BASE_EXPORT bool StringToInt(const StringPiece& input, int* output); |
| 63 | BASE_EXPORT bool StringToInt(const StringPiece16& input, int* output); |
[email protected] | d2d0b6b | 2012-01-26 00:27:29 | [diff] [blame] | 64 | |
| 65 | BASE_EXPORT bool StringToUint(const StringPiece& input, unsigned* output); |
| 66 | BASE_EXPORT bool StringToUint(const StringPiece16& input, unsigned* output); |
| 67 | |
[email protected] | eb72b27 | 2011-12-19 16:10:55 | [diff] [blame] | 68 | BASE_EXPORT bool StringToInt64(const StringPiece& input, int64* output); |
| 69 | BASE_EXPORT bool StringToInt64(const StringPiece16& input, int64* output); |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 70 | |
[email protected] | d2d0b6b | 2012-01-26 00:27:29 | [diff] [blame] | 71 | BASE_EXPORT bool StringToUint64(const StringPiece& input, uint64* output); |
| 72 | BASE_EXPORT bool StringToUint64(const StringPiece16& input, uint64* output); |
| 73 | |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 74 | // For floating-point conversions, only conversions of input strings in decimal |
| 75 | // form are defined to work. Behavior with strings representing floating-point |
| 76 | // numbers in hexadecimal, and strings representing non-fininte values (such as |
| 77 | // NaN and inf) is undefined. Otherwise, these behave the same as the integral |
| 78 | // variants. This expects the input string to NOT be specific to the locale. |
| 79 | // If your input is locale specific, use ICU to read the number. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 80 | BASE_EXPORT bool StringToDouble(const std::string& input, double* output); |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 81 | |
| 82 | // Hex encoding ---------------------------------------------------------------- |
| 83 | |
| 84 | // Returns a hex string representation of a binary buffer. The returned hex |
| 85 | // string will be in upper case. This function does not check if |size| is |
| 86 | // within reasonable limits since it's written with trusted data in mind. If |
| 87 | // you suspect that the data you want to format might be large, the absolute |
| 88 | // max size for |size| should be is |
| 89 | // std::numeric_limits<size_t>::max() / 2 |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 90 | BASE_EXPORT std::string HexEncode(const void* bytes, size_t size); |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 91 | |
| 92 | // Best effort conversion, see StringToInt above for restrictions. |
[email protected] | eb72b27 | 2011-12-19 16:10:55 | [diff] [blame] | 93 | BASE_EXPORT bool HexStringToInt(const StringPiece& input, int* output); |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 94 | |
| 95 | // Similar to the previous functions, except that output is a vector of bytes. |
| 96 | // |*output| will contain as many bytes as were successfully parsed prior to the |
| 97 | // error. There is no overflow, but input.size() must be evenly divisible by 2. |
| 98 | // Leading 0x or +/- are not allowed. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 99 | BASE_EXPORT bool HexStringToBytes(const std::string& input, |
| 100 | std::vector<uint8>* output); |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 101 | |
| 102 | } // namespace base |
| 103 | |
| 104 | #endif // BASE_STRING_NUMBER_CONVERSIONS_H_ |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 105 | |