forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_number_conversions.cc
173 lines (130 loc) · 4.8 KB
/
string_number_conversions.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/strings/string_number_conversions.h"
#include <iterator>
#include <string>
#include "base/containers/span.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions_internal.h"
#include "base/strings/string_piece.h"
namespace base {
std::string NumberToString(int value) {
return internal::IntToStringT<std::string>(value);
}
std::u16string NumberToString16(int value) {
return internal::IntToStringT<std::u16string>(value);
}
std::string NumberToString(unsigned value) {
return internal::IntToStringT<std::string>(value);
}
std::u16string NumberToString16(unsigned value) {
return internal::IntToStringT<std::u16string>(value);
}
std::string NumberToString(long value) {
return internal::IntToStringT<std::string>(value);
}
std::u16string NumberToString16(long value) {
return internal::IntToStringT<std::u16string>(value);
}
std::string NumberToString(unsigned long value) {
return internal::IntToStringT<std::string>(value);
}
std::u16string NumberToString16(unsigned long value) {
return internal::IntToStringT<std::u16string>(value);
}
std::string NumberToString(long long value) {
return internal::IntToStringT<std::string>(value);
}
std::u16string NumberToString16(long long value) {
return internal::IntToStringT<std::u16string>(value);
}
std::string NumberToString(unsigned long long value) {
return internal::IntToStringT<std::string>(value);
}
std::u16string NumberToString16(unsigned long long value) {
return internal::IntToStringT<std::u16string>(value);
}
std::string NumberToString(double value) {
return internal::DoubleToStringT<std::string>(value);
}
std::u16string NumberToString16(double value) {
return internal::DoubleToStringT<std::u16string>(value);
}
bool StringToInt(StringPiece input, int* output) {
return internal::StringToIntImpl(input, *output);
}
bool StringToInt(StringPiece16 input, int* output) {
return internal::StringToIntImpl(input, *output);
}
bool StringToUint(StringPiece input, unsigned* output) {
return internal::StringToIntImpl(input, *output);
}
bool StringToUint(StringPiece16 input, unsigned* output) {
return internal::StringToIntImpl(input, *output);
}
bool StringToInt64(StringPiece input, int64_t* output) {
return internal::StringToIntImpl(input, *output);
}
bool StringToInt64(StringPiece16 input, int64_t* output) {
return internal::StringToIntImpl(input, *output);
}
bool StringToUint64(StringPiece input, uint64_t* output) {
return internal::StringToIntImpl(input, *output);
}
bool StringToUint64(StringPiece16 input, uint64_t* output) {
return internal::StringToIntImpl(input, *output);
}
bool StringToSizeT(StringPiece input, size_t* output) {
return internal::StringToIntImpl(input, *output);
}
bool StringToSizeT(StringPiece16 input, size_t* output) {
return internal::StringToIntImpl(input, *output);
}
bool StringToDouble(StringPiece input, double* output) {
return internal::StringToDoubleImpl(input, input.data(), *output);
}
bool StringToDouble(StringPiece16 input, double* output) {
return internal::StringToDoubleImpl(
input, reinterpret_cast<const uint16_t*>(input.data()), *output);
}
std::string HexEncode(const void* bytes, size_t size) {
static const char kHexChars[] = "0123456789ABCDEF";
// Each input byte creates two output hex characters.
std::string ret(size * 2, '\0');
for (size_t i = 0; i < size; ++i) {
char b = reinterpret_cast<const char*>(bytes)[i];
ret[(i * 2)] = kHexChars[(b >> 4) & 0xf];
ret[(i * 2) + 1] = kHexChars[b & 0xf];
}
return ret;
}
std::string HexEncode(base::span<const uint8_t> bytes) {
return HexEncode(bytes.data(), bytes.size());
}
bool HexStringToInt(StringPiece input, int* output) {
return internal::HexStringToIntImpl(input, *output);
}
bool HexStringToUInt(StringPiece input, uint32_t* output) {
return internal::HexStringToIntImpl(input, *output);
}
bool HexStringToInt64(StringPiece input, int64_t* output) {
return internal::HexStringToIntImpl(input, *output);
}
bool HexStringToUInt64(StringPiece input, uint64_t* output) {
return internal::HexStringToIntImpl(input, *output);
}
bool HexStringToBytes(StringPiece input, std::vector<uint8_t>* output) {
DCHECK(output->empty());
return internal::HexStringToByteContainer(input, std::back_inserter(*output));
}
bool HexStringToString(StringPiece input, std::string* output) {
DCHECK(output->empty());
return internal::HexStringToByteContainer(input, std::back_inserter(*output));
}
bool HexStringToSpan(StringPiece input, base::span<uint8_t> output) {
if (input.size() / 2 != output.size())
return false;
return internal::HexStringToByteContainer(input, output.begin());
}
} // namespace base