Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 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_STRINGS_STRING_NUMBER_CONVERSIONS_H_ |
| 6 | #define BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_ |
| 7 | |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "base/base_export.h" |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 15 | #include "base/strings/string_piece.h" |
| 16 | |
| 17 | // ---------------------------------------------------------------------------- |
| 18 | // IMPORTANT MESSAGE FROM YOUR SPONSOR |
| 19 | // |
| 20 | // This file contains no "wstring" variants. New code should use string16. If |
| 21 | // you need to make old code work, use the UTF8 version and convert. Please do |
| 22 | // not add wstring variants. |
| 23 | // |
| 24 | // Please do not add "convenience" functions for converting strings to integers |
| 25 | // that return the value and ignore success/failure. That encourages people to |
| 26 | // write code that doesn't properly handle the error conditions. |
| 27 | // ---------------------------------------------------------------------------- |
| 28 | |
| 29 | namespace base { |
| 30 | |
| 31 | // Number -> string conversions ------------------------------------------------ |
| 32 | |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 33 | std::string IntToString(int value); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 34 | |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 35 | std::string UintToString(unsigned value); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 36 | |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 37 | std::string Int64ToString(int64_t value); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 38 | |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 39 | std::string Uint64ToString(uint64_t value); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 40 | |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 41 | std::string SizeTToString(size_t value); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 42 | |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 43 | // Deprecated: prefer std::to_string(double) instead. |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 44 | // DoubleToString converts the double to a string format that ignores the |
| 45 | // locale. If you want to use locale specific formatting, use ICU. |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 46 | std::string DoubleToString(double value); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [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. |*output| will be set to the maximum value supported |
| 54 | // by the data type. |
| 55 | // - Underflow. |*output| will be set to the minimum value supported |
| 56 | // by the data type. |
| 57 | // - Trailing characters in the string after parsing the number. |*output| |
| 58 | // will be set to the value of the number that was parsed. |
| 59 | // - Leading whitespace in the string before parsing the number. |*output| will |
| 60 | // be set to the value of the number that was parsed. |
| 61 | // - No characters parseable as a number at the beginning of the string. |
| 62 | // |*output| will be set to 0. |
| 63 | // - Empty string. |*output| will be set to 0. |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 64 | // WARNING: Will write to |output| even when returning false. |
| 65 | // Read the comments above carefully. |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 66 | bool StringToInt(const StringPiece& input, int* output); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 67 | |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 68 | bool StringToUint(const StringPiece& input, unsigned* output); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 69 | |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 70 | bool StringToInt64(const StringPiece& input, int64_t* output); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 71 | |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 72 | bool StringToUint64(const StringPiece& input, uint64_t* output); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 73 | |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 74 | bool StringToSizeT(const StringPiece& input, size_t* output); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 75 | |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 76 | // Deprecated: prefer std::stod() instead. |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 77 | // For floating-point conversions, only conversions of input strings in decimal |
| 78 | // form are defined to work. Behavior with strings representing floating-point |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 79 | // numbers in hexadecimal, and strings representing non-finite values (such as |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 80 | // NaN and inf) is undefined. Otherwise, these behave the same as the integral |
| 81 | // variants. This expects the input string to NOT be specific to the locale. |
| 82 | // If your input is locale specific, use ICU to read the number. |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 83 | // WARNING: Will write to |output| even when returning false. |
| 84 | // Read the comments here and above StringToInt() carefully. |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 85 | bool StringToDouble(const std::string& input, double* output); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 86 | |
| 87 | // Hex encoding ---------------------------------------------------------------- |
| 88 | |
| 89 | // Returns a hex string representation of a binary buffer. The returned hex |
| 90 | // string will be in upper case. This function does not check if |size| is |
| 91 | // within reasonable limits since it's written with trusted data in mind. If |
| 92 | // you suspect that the data you want to format might be large, the absolute |
| 93 | // max size for |size| should be is |
| 94 | // std::numeric_limits<size_t>::max() / 2 |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 95 | std::string HexEncode(const void* bytes, size_t size); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 96 | |
| 97 | // Best effort conversion, see StringToInt above for restrictions. |
| 98 | // Will only successful parse hex values that will fit into |output|, i.e. |
| 99 | // -0x80000000 < |input| < 0x7FFFFFFF. |
Vitaly Buka | 60b8f00 | 2015-08-20 13:47:48 -0700 | [diff] [blame] | 100 | bool HexStringToInt(const StringPiece& input, int* output); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 101 | |
| 102 | // Best effort conversion, see StringToInt above for restrictions. |
| 103 | // Will only successful parse hex values that will fit into |output|, i.e. |
| 104 | // 0x00000000 < |input| < 0xFFFFFFFF. |
| 105 | // The string is not required to start with 0x. |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 106 | bool HexStringToUInt(const StringPiece& input, uint32_t* output); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 107 | |
| 108 | // Best effort conversion, see StringToInt above for restrictions. |
| 109 | // Will only successful parse hex values that will fit into |output|, i.e. |
| 110 | // -0x8000000000000000 < |input| < 0x7FFFFFFFFFFFFFFF. |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 111 | bool HexStringToInt64(const StringPiece& input, int64_t* output); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 112 | |
| 113 | // Best effort conversion, see StringToInt above for restrictions. |
| 114 | // Will only successful parse hex values that will fit into |output|, i.e. |
| 115 | // 0x0000000000000000 < |input| < 0xFFFFFFFFFFFFFFFF. |
| 116 | // The string is not required to start with 0x. |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 117 | bool HexStringToUInt64(const StringPiece& input, uint64_t* output); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 118 | |
| 119 | // Similar to the previous functions, except that output is a vector of bytes. |
| 120 | // |*output| will contain as many bytes as were successfully parsed prior to the |
| 121 | // error. There is no overflow, but input.size() must be evenly divisible by 2. |
| 122 | // Leading 0x or +/- are not allowed. |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 123 | bool HexStringToBytes(const std::string& input, |
| 124 | std::vector<uint8_t>* output); |
Vitaly Buka | cbed206 | 2015-08-17 12:54:05 -0700 | [diff] [blame] | 125 | |
| 126 | } // namespace base |
| 127 | |
| 128 | #endif // BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_ |