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