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
8#include <stddef.h>
9#include <stdint.h>
10
11#include <string>
12#include <string_view>
13#include <vector>
14
15#include "util/build_config.h"
16
17namespace base {
18
19// Number -> string conversions ------------------------------------------------
20
21// Ignores locale! see warning above.
22std::string NumberToString(int value);
23std::u16string NumberToString16(int value);
24std::string NumberToString(unsigned int value);
25std::u16string NumberToString16(unsigned int value);
26std::string NumberToString(long value);
27std::u16string NumberToString16(long value);
28std::string NumberToString(unsigned long value);
29std::u16string NumberToString16(unsigned long value);
30std::string NumberToString(long long value);
31std::u16string NumberToString16(long long value);
32std::string NumberToString(unsigned long long value);
33std::u16string NumberToString16(unsigned long long value);
34
35// Type-specific naming for backwards compatibility.
36//
37// TODO(brettw) these should be removed and callers converted to the overloaded
38// "NumberToString" variant.
39inline std::string IntToString(int value) {
40  return NumberToString(value);
41}
42inline std::u16string IntToString16(int value) {
43  return NumberToString16(value);
44}
45inline std::string UintToString(unsigned value) {
46  return NumberToString(value);
47}
48inline std::u16string UintToString16(unsigned value) {
49  return NumberToString16(value);
50}
51inline std::string Int64ToString(int64_t value) {
52  return NumberToString(value);
53}
54inline std::u16string Int64ToString16(int64_t value) {
55  return NumberToString16(value);
56}
57
58// String -> number conversions ------------------------------------------------
59
60// Perform a best-effort conversion of the input string to a numeric type,
61// setting |*output| to the result of the conversion.  Returns true for
62// "perfect" conversions; returns false in the following cases:
63//  - Overflow. |*output| will be set to the maximum value supported
64//    by the data type.
65//  - Underflow. |*output| will be set to the minimum value supported
66//    by the data type.
67//  - Trailing characters in the string after parsing the number.  |*output|
68//    will be set to the value of the number that was parsed.
69//  - Leading whitespace in the string before parsing the number. |*output| will
70//    be set to the value of the number that was parsed.
71//  - No characters parseable as a number at the beginning of the string.
72//    |*output| will be set to 0.
73//  - Empty string.  |*output| will be set to 0.
74// WARNING: Will write to |output| even when returning false.
75//          Read the comments above carefully.
76bool StringToInt(std::string_view input, int* output);
77bool StringToInt(std::u16string_view input, int* output);
78
79bool StringToUint(std::string_view input, unsigned* output);
80bool StringToUint(std::u16string_view input, unsigned* output);
81
82bool StringToInt64(std::string_view input, int64_t* output);
83bool StringToInt64(std::u16string_view input, int64_t* output);
84
85bool StringToUint64(std::string_view input, uint64_t* output);
86bool StringToUint64(std::u16string_view input, uint64_t* output);
87
88bool StringToSizeT(std::string_view input, size_t* output);
89bool StringToSizeT(std::u16string_view input, size_t* output);
90
91// Hex encoding ----------------------------------------------------------------
92
93// Returns a hex string representation of a binary buffer. The returned hex
94// string will be in upper case. This function does not check if |size| is
95// within reasonable limits since it's written with trusted data in mind.  If
96// you suspect that the data you want to format might be large, the absolute
97// max size for |size| should be is
98//   std::numeric_limits<size_t>::max() / 2
99std::string HexEncode(const void* bytes, size_t size);
100
101// Best effort conversion, see StringToInt above for restrictions.
102// Will only successful parse hex values that will fit into |output|, i.e.
103// -0x80000000 < |input| < 0x7FFFFFFF.
104bool HexStringToInt(std::string_view input, int* output);
105
106// Best effort conversion, see StringToInt above for restrictions.
107// Will only successful parse hex values that will fit into |output|, i.e.
108// 0x00000000 < |input| < 0xFFFFFFFF.
109// The string is not required to start with 0x.
110bool HexStringToUInt(std::string_view input, uint32_t* output);
111
112// Best effort conversion, see StringToInt above for restrictions.
113// Will only successful parse hex values that will fit into |output|, i.e.
114// -0x8000000000000000 < |input| < 0x7FFFFFFFFFFFFFFF.
115bool HexStringToInt64(std::string_view input, int64_t* output);
116
117// Best effort conversion, see StringToInt above for restrictions.
118// Will only successful parse hex values that will fit into |output|, i.e.
119// 0x0000000000000000 < |input| < 0xFFFFFFFFFFFFFFFF.
120// The string is not required to start with 0x.
121bool HexStringToUInt64(std::string_view input, uint64_t* output);
122
123// Similar to the previous functions, except that output is a vector of bytes.
124// |*output| will contain as many bytes as were successfully parsed prior to the
125// error.  There is no overflow, but input.size() must be evenly divisible by 2.
126// Leading 0x or +/- are not allowed.
127bool HexStringToBytes(std::string_view input, std::vector<uint8_t>* output);
128
129}  // namespace base
130
131#endif  // BASE_STRINGS_STRING_NUMBER_CONVERSIONS_H_
132