16d528ed9Sopenharmony_ci// Copyright (c) 2012 The Chromium Authors. All rights reserved. 26d528ed9Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 36d528ed9Sopenharmony_ci// found in the LICENSE file. 46d528ed9Sopenharmony_ci 56d528ed9Sopenharmony_ci#ifndef BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_ 66d528ed9Sopenharmony_ci#define BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_ 76d528ed9Sopenharmony_ci 86d528ed9Sopenharmony_ci#include <stddef.h> 96d528ed9Sopenharmony_ci#include <stdint.h> 106d528ed9Sopenharmony_ci 116d528ed9Sopenharmony_ci#include <string> 126d528ed9Sopenharmony_ci#include <string_view> 136d528ed9Sopenharmony_ci#include <vector> 146d528ed9Sopenharmony_ci 156d528ed9Sopenharmony_ci#include "util/build_config.h" 166d528ed9Sopenharmony_ci 176d528ed9Sopenharmony_cinamespace base { 186d528ed9Sopenharmony_ci 196d528ed9Sopenharmony_ci// Number -> string conversions ------------------------------------------------ 206d528ed9Sopenharmony_ci 216d528ed9Sopenharmony_ci// Ignores locale! see warning above. 226d528ed9Sopenharmony_cistd::string NumberToString(int value); 236d528ed9Sopenharmony_cistd::u16string NumberToString16(int value); 246d528ed9Sopenharmony_cistd::string NumberToString(unsigned int value); 256d528ed9Sopenharmony_cistd::u16string NumberToString16(unsigned int value); 266d528ed9Sopenharmony_cistd::string NumberToString(long value); 276d528ed9Sopenharmony_cistd::u16string NumberToString16(long value); 286d528ed9Sopenharmony_cistd::string NumberToString(unsigned long value); 296d528ed9Sopenharmony_cistd::u16string NumberToString16(unsigned long value); 306d528ed9Sopenharmony_cistd::string NumberToString(long long value); 316d528ed9Sopenharmony_cistd::u16string NumberToString16(long long value); 326d528ed9Sopenharmony_cistd::string NumberToString(unsigned long long value); 336d528ed9Sopenharmony_cistd::u16string NumberToString16(unsigned long long value); 346d528ed9Sopenharmony_ci 356d528ed9Sopenharmony_ci// Type-specific naming for backwards compatibility. 366d528ed9Sopenharmony_ci// 376d528ed9Sopenharmony_ci// TODO(brettw) these should be removed and callers converted to the overloaded 386d528ed9Sopenharmony_ci// "NumberToString" variant. 396d528ed9Sopenharmony_ciinline std::string IntToString(int value) { 406d528ed9Sopenharmony_ci return NumberToString(value); 416d528ed9Sopenharmony_ci} 426d528ed9Sopenharmony_ciinline std::u16string IntToString16(int value) { 436d528ed9Sopenharmony_ci return NumberToString16(value); 446d528ed9Sopenharmony_ci} 456d528ed9Sopenharmony_ciinline std::string UintToString(unsigned value) { 466d528ed9Sopenharmony_ci return NumberToString(value); 476d528ed9Sopenharmony_ci} 486d528ed9Sopenharmony_ciinline std::u16string UintToString16(unsigned value) { 496d528ed9Sopenharmony_ci return NumberToString16(value); 506d528ed9Sopenharmony_ci} 516d528ed9Sopenharmony_ciinline std::string Int64ToString(int64_t value) { 526d528ed9Sopenharmony_ci return NumberToString(value); 536d528ed9Sopenharmony_ci} 546d528ed9Sopenharmony_ciinline std::u16string Int64ToString16(int64_t value) { 556d528ed9Sopenharmony_ci return NumberToString16(value); 566d528ed9Sopenharmony_ci} 576d528ed9Sopenharmony_ci 586d528ed9Sopenharmony_ci// String -> number conversions ------------------------------------------------ 596d528ed9Sopenharmony_ci 606d528ed9Sopenharmony_ci// Perform a best-effort conversion of the input string to a numeric type, 616d528ed9Sopenharmony_ci// setting |*output| to the result of the conversion. Returns true for 626d528ed9Sopenharmony_ci// "perfect" conversions; returns false in the following cases: 636d528ed9Sopenharmony_ci// - Overflow. |*output| will be set to the maximum value supported 646d528ed9Sopenharmony_ci// by the data type. 656d528ed9Sopenharmony_ci// - Underflow. |*output| will be set to the minimum value supported 666d528ed9Sopenharmony_ci// by the data type. 676d528ed9Sopenharmony_ci// - Trailing characters in the string after parsing the number. |*output| 686d528ed9Sopenharmony_ci// will be set to the value of the number that was parsed. 696d528ed9Sopenharmony_ci// - Leading whitespace in the string before parsing the number. |*output| will 706d528ed9Sopenharmony_ci// be set to the value of the number that was parsed. 716d528ed9Sopenharmony_ci// - No characters parseable as a number at the beginning of the string. 726d528ed9Sopenharmony_ci// |*output| will be set to 0. 736d528ed9Sopenharmony_ci// - Empty string. |*output| will be set to 0. 746d528ed9Sopenharmony_ci// WARNING: Will write to |output| even when returning false. 756d528ed9Sopenharmony_ci// Read the comments above carefully. 766d528ed9Sopenharmony_cibool StringToInt(std::string_view input, int* output); 776d528ed9Sopenharmony_cibool StringToInt(std::u16string_view input, int* output); 786d528ed9Sopenharmony_ci 796d528ed9Sopenharmony_cibool StringToUint(std::string_view input, unsigned* output); 806d528ed9Sopenharmony_cibool StringToUint(std::u16string_view input, unsigned* output); 816d528ed9Sopenharmony_ci 826d528ed9Sopenharmony_cibool StringToInt64(std::string_view input, int64_t* output); 836d528ed9Sopenharmony_cibool StringToInt64(std::u16string_view input, int64_t* output); 846d528ed9Sopenharmony_ci 856d528ed9Sopenharmony_cibool StringToUint64(std::string_view input, uint64_t* output); 866d528ed9Sopenharmony_cibool StringToUint64(std::u16string_view input, uint64_t* output); 876d528ed9Sopenharmony_ci 886d528ed9Sopenharmony_cibool StringToSizeT(std::string_view input, size_t* output); 896d528ed9Sopenharmony_cibool StringToSizeT(std::u16string_view input, size_t* output); 906d528ed9Sopenharmony_ci 916d528ed9Sopenharmony_ci// Hex encoding ---------------------------------------------------------------- 926d528ed9Sopenharmony_ci 936d528ed9Sopenharmony_ci// Returns a hex string representation of a binary buffer. The returned hex 946d528ed9Sopenharmony_ci// string will be in upper case. This function does not check if |size| is 956d528ed9Sopenharmony_ci// within reasonable limits since it's written with trusted data in mind. If 966d528ed9Sopenharmony_ci// you suspect that the data you want to format might be large, the absolute 976d528ed9Sopenharmony_ci// max size for |size| should be is 986d528ed9Sopenharmony_ci// std::numeric_limits<size_t>::max() / 2 996d528ed9Sopenharmony_cistd::string HexEncode(const void* bytes, size_t size); 1006d528ed9Sopenharmony_ci 1016d528ed9Sopenharmony_ci// Best effort conversion, see StringToInt above for restrictions. 1026d528ed9Sopenharmony_ci// Will only successful parse hex values that will fit into |output|, i.e. 1036d528ed9Sopenharmony_ci// -0x80000000 < |input| < 0x7FFFFFFF. 1046d528ed9Sopenharmony_cibool HexStringToInt(std::string_view input, int* output); 1056d528ed9Sopenharmony_ci 1066d528ed9Sopenharmony_ci// Best effort conversion, see StringToInt above for restrictions. 1076d528ed9Sopenharmony_ci// Will only successful parse hex values that will fit into |output|, i.e. 1086d528ed9Sopenharmony_ci// 0x00000000 < |input| < 0xFFFFFFFF. 1096d528ed9Sopenharmony_ci// The string is not required to start with 0x. 1106d528ed9Sopenharmony_cibool HexStringToUInt(std::string_view input, uint32_t* output); 1116d528ed9Sopenharmony_ci 1126d528ed9Sopenharmony_ci// Best effort conversion, see StringToInt above for restrictions. 1136d528ed9Sopenharmony_ci// Will only successful parse hex values that will fit into |output|, i.e. 1146d528ed9Sopenharmony_ci// -0x8000000000000000 < |input| < 0x7FFFFFFFFFFFFFFF. 1156d528ed9Sopenharmony_cibool HexStringToInt64(std::string_view input, int64_t* output); 1166d528ed9Sopenharmony_ci 1176d528ed9Sopenharmony_ci// Best effort conversion, see StringToInt above for restrictions. 1186d528ed9Sopenharmony_ci// Will only successful parse hex values that will fit into |output|, i.e. 1196d528ed9Sopenharmony_ci// 0x0000000000000000 < |input| < 0xFFFFFFFFFFFFFFFF. 1206d528ed9Sopenharmony_ci// The string is not required to start with 0x. 1216d528ed9Sopenharmony_cibool HexStringToUInt64(std::string_view input, uint64_t* output); 1226d528ed9Sopenharmony_ci 1236d528ed9Sopenharmony_ci// Similar to the previous functions, except that output is a vector of bytes. 1246d528ed9Sopenharmony_ci// |*output| will contain as many bytes as were successfully parsed prior to the 1256d528ed9Sopenharmony_ci// error. There is no overflow, but input.size() must be evenly divisible by 2. 1266d528ed9Sopenharmony_ci// Leading 0x or +/- are not allowed. 1276d528ed9Sopenharmony_cibool HexStringToBytes(std::string_view input, std::vector<uint8_t>* output); 1286d528ed9Sopenharmony_ci 1296d528ed9Sopenharmony_ci} // namespace base 1306d528ed9Sopenharmony_ci 1316d528ed9Sopenharmony_ci#endif // BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_ 132