1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/strings/string_number_conversions.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,122 @@ 1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_ 1.9 +#define BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_ 1.10 + 1.11 +#include <string> 1.12 +#include <vector> 1.13 + 1.14 +#include "base/base_export.h" 1.15 +#include "base/basictypes.h" 1.16 +#include "base/strings/string16.h" 1.17 +#include "base/strings/string_piece.h" 1.18 + 1.19 +// ---------------------------------------------------------------------------- 1.20 +// IMPORTANT MESSAGE FROM YOUR SPONSOR 1.21 +// 1.22 +// This file contains no "wstring" variants. New code should use string16. If 1.23 +// you need to make old code work, use the UTF8 version and convert. Please do 1.24 +// not add wstring variants. 1.25 +// 1.26 +// Please do not add "convenience" functions for converting strings to integers 1.27 +// that return the value and ignore success/failure. That encourages people to 1.28 +// write code that doesn't properly handle the error conditions. 1.29 +// ---------------------------------------------------------------------------- 1.30 + 1.31 +namespace base { 1.32 + 1.33 +// Number -> string conversions ------------------------------------------------ 1.34 + 1.35 +BASE_EXPORT std::string IntToString(int value); 1.36 +BASE_EXPORT string16 IntToString16(int value); 1.37 + 1.38 +BASE_EXPORT std::string UintToString(unsigned value); 1.39 +BASE_EXPORT string16 UintToString16(unsigned value); 1.40 + 1.41 +BASE_EXPORT std::string Int64ToString(int64 value); 1.42 +BASE_EXPORT string16 Int64ToString16(int64 value); 1.43 + 1.44 +BASE_EXPORT std::string Uint64ToString(uint64 value); 1.45 +BASE_EXPORT string16 Uint64ToString16(uint64 value); 1.46 + 1.47 +// DoubleToString converts the double to a string format that ignores the 1.48 +// locale. If you want to use locale specific formatting, use ICU. 1.49 +BASE_EXPORT std::string DoubleToString(double value); 1.50 + 1.51 +// String -> number conversions ------------------------------------------------ 1.52 + 1.53 +// Perform a best-effort conversion of the input string to a numeric type, 1.54 +// setting |*output| to the result of the conversion. Returns true for 1.55 +// "perfect" conversions; returns false in the following cases: 1.56 +// - Overflow. |*output| will be set to the maximum value supported 1.57 +// by the data type. 1.58 +// - Underflow. |*output| will be set to the minimum value supported 1.59 +// by the data type. 1.60 +// - Trailing characters in the string after parsing the number. |*output| 1.61 +// will be set to the value of the number that was parsed. 1.62 +// - Leading whitespace in the string before parsing the number. |*output| will 1.63 +// be set to the value of the number that was parsed. 1.64 +// - No characters parseable as a number at the beginning of the string. 1.65 +// |*output| will be set to 0. 1.66 +// - Empty string. |*output| will be set to 0. 1.67 +BASE_EXPORT bool StringToInt(const StringPiece& input, int* output); 1.68 +BASE_EXPORT bool StringToInt(const StringPiece16& input, int* output); 1.69 + 1.70 +BASE_EXPORT bool StringToUint(const StringPiece& input, unsigned* output); 1.71 +BASE_EXPORT bool StringToUint(const StringPiece16& input, unsigned* output); 1.72 + 1.73 +BASE_EXPORT bool StringToInt64(const StringPiece& input, int64* output); 1.74 +BASE_EXPORT bool StringToInt64(const StringPiece16& input, int64* output); 1.75 + 1.76 +BASE_EXPORT bool StringToUint64(const StringPiece& input, uint64* output); 1.77 +BASE_EXPORT bool StringToUint64(const StringPiece16& input, uint64* output); 1.78 + 1.79 +BASE_EXPORT bool StringToSizeT(const StringPiece& input, size_t* output); 1.80 +BASE_EXPORT bool StringToSizeT(const StringPiece16& input, size_t* output); 1.81 + 1.82 +// For floating-point conversions, only conversions of input strings in decimal 1.83 +// form are defined to work. Behavior with strings representing floating-point 1.84 +// numbers in hexadecimal, and strings representing non-fininte values (such as 1.85 +// NaN and inf) is undefined. Otherwise, these behave the same as the integral 1.86 +// variants. This expects the input string to NOT be specific to the locale. 1.87 +// If your input is locale specific, use ICU to read the number. 1.88 +BASE_EXPORT bool StringToDouble(const std::string& input, double* output); 1.89 + 1.90 +// Hex encoding ---------------------------------------------------------------- 1.91 + 1.92 +// Returns a hex string representation of a binary buffer. The returned hex 1.93 +// string will be in upper case. This function does not check if |size| is 1.94 +// within reasonable limits since it's written with trusted data in mind. If 1.95 +// you suspect that the data you want to format might be large, the absolute 1.96 +// max size for |size| should be is 1.97 +// std::numeric_limits<size_t>::max() / 2 1.98 +BASE_EXPORT std::string HexEncode(const void* bytes, size_t size); 1.99 + 1.100 +// Best effort conversion, see StringToInt above for restrictions. 1.101 +// Will only successful parse hex values that will fit into |output|, i.e. 1.102 +// -0x80000000 < |input| < 0x7FFFFFFF. 1.103 +BASE_EXPORT bool HexStringToInt(const StringPiece& input, int* output); 1.104 + 1.105 +// Best effort conversion, see StringToInt above for restrictions. 1.106 +// Will only successful parse hex values that will fit into |output|, i.e. 1.107 +// -0x8000000000000000 < |input| < 0x7FFFFFFFFFFFFFFF. 1.108 +BASE_EXPORT bool HexStringToInt64(const StringPiece& input, int64* output); 1.109 + 1.110 +// Best effort conversion, see StringToInt above for restrictions. 1.111 +// Will only successful parse hex values that will fit into |output|, i.e. 1.112 +// 0x0000000000000000 < |input| < 0xFFFFFFFFFFFFFFFF. 1.113 +// The string is not required to start with 0x. 1.114 +BASE_EXPORT bool HexStringToUInt64(const StringPiece& input, uint64* output); 1.115 + 1.116 +// Similar to the previous functions, except that output is a vector of bytes. 1.117 +// |*output| will contain as many bytes as were successfully parsed prior to the 1.118 +// error. There is no overflow, but input.size() must be evenly divisible by 2. 1.119 +// Leading 0x or +/- are not allowed. 1.120 +BASE_EXPORT bool HexStringToBytes(const std::string& input, 1.121 + std::vector<uint8>* output); 1.122 + 1.123 +} // namespace base 1.124 + 1.125 +#endif // BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_