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