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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsTStringRepr.h"
#include "double-conversion/string-to-double.h"
#include "mozilla/FloatingPoint.h"
#include "nsError.h"
#include "nsString.h"
namespace mozilla::detail {
template <typename T>
typename nsTStringRepr<T>::char_type nsTStringRepr<T>::First() const {
MOZ_RELEASE_ASSERT(this->mLength > 0, "|First()| called on an empty string");
return this->mData[0];
}
template <typename T>
typename nsTStringRepr<T>::char_type nsTStringRepr<T>::Last() const {
MOZ_RELEASE_ASSERT(this->mLength > 0, "|Last()| called on an empty string");
return this->mData[this->mLength - 1];
}
template <typename T>
bool nsTStringRepr<T>::Equals(const self_type& aStr) const {
return this->mLength == aStr.mLength &&
char_traits::compare(this->mData, aStr.mData, this->mLength) == 0;
}
template <typename T>
bool nsTStringRepr<T>::Equals(const self_type& aStr,
comparator_type aComp) const {
return this->mLength == aStr.mLength &&
aComp(this->mData, aStr.mData, this->mLength, aStr.mLength) == 0;
}
template <typename T>
bool nsTStringRepr<T>::Equals(const substring_tuple_type& aTuple) const {
return Equals(substring_type(aTuple));
}
template <typename T>
bool nsTStringRepr<T>::Equals(const substring_tuple_type& aTuple,
comparator_type aComp) const {
return Equals(substring_type(aTuple), aComp);
}
template <typename T>
bool nsTStringRepr<T>::Equals(const char_type* aData) const {
// unfortunately, some callers pass null :-(
if (!aData) {
MOZ_ASSERT_UNREACHABLE("null data pointer");
return this->mLength == 0;
}
// XXX avoid length calculation?
size_type length = char_traits::length(aData);
return this->mLength == length &&
char_traits::compare(this->mData, aData, this->mLength) == 0;
}
template <typename T>
bool nsTStringRepr<T>::Equals(const char_type* aData,
comparator_type aComp) const {
// unfortunately, some callers pass null :-(
if (!aData) {
MOZ_ASSERT_UNREACHABLE("null data pointer");
return this->mLength == 0;
}
// XXX avoid length calculation?
size_type length = char_traits::length(aData);
return this->mLength == length &&
aComp(this->mData, aData, this->mLength, length) == 0;
}
template <typename T>
bool nsTStringRepr<T>::EqualsASCII(const char* aData, size_type aLen) const {
return this->mLength == aLen &&
char_traits::compareASCII(this->mData, aData, aLen) == 0;
}
template <typename T>
bool nsTStringRepr<T>::EqualsASCII(const char* aData) const {
return char_traits::compareASCIINullTerminated(this->mData, this->mLength,
aData) == 0;
}
template <typename T>
bool nsTStringRepr<T>::EqualsLatin1(const char* aData,
const size_type aLength) const {
return (this->mLength == aLength) &&
char_traits::equalsLatin1(this->mData, aData, aLength);
}
template <typename T>
bool nsTStringRepr<T>::LowerCaseEqualsASCII(const char* aData,
size_type aLen) const {
return this->mLength == aLen &&
char_traits::compareLowerCaseToASCII(this->mData, aData, aLen) == 0;
}
template <typename T>
bool nsTStringRepr<T>::LowerCaseEqualsASCII(const char* aData) const {
return char_traits::compareLowerCaseToASCIINullTerminated(
this->mData, this->mLength, aData) == 0;
}
template <typename T>
int32_t nsTStringRepr<T>::Find(const string_view& aString,
index_type aOffset) const {
auto idx = View().find(aString, aOffset);
return idx == string_view::npos ? kNotFound : idx;
}
template <typename T>
int32_t nsTStringRepr<T>::LowerCaseFindASCII(const std::string_view& aString,
index_type aOffset) const {
if (aOffset > Length()) {
return kNotFound;
}
auto begin = BeginReading();
auto end = EndReading();
auto it =
std::search(begin + aOffset, end, aString.begin(), aString.end(),
[](char_type l, char r) {
MOZ_ASSERT(!(r & ~0x7F), "Unexpected non-ASCII character");
MOZ_ASSERT(char_traits::ASCIIToLower(r) == char_type(r),
"Search string must be ASCII lowercase");
return char_traits::ASCIIToLower(l) == char_type(r);
});
return it == end ? kNotFound : std::distance(begin, it);
}
template <typename T>
int32_t nsTStringRepr<T>::RFind(const string_view& aString) const {
auto idx = View().rfind(aString);
return idx == string_view::npos ? kNotFound : idx;
}
template <typename T>
typename nsTStringRepr<T>::size_type nsTStringRepr<T>::CountChar(
char_type aChar) const {
return std::count(BeginReading(), EndReading(), aChar);
}
template <typename T>
int32_t nsTStringRepr<T>::FindChar(char_type aChar, index_type aOffset) const {
auto idx = View().find(aChar, aOffset);
return idx == string_view::npos ? kNotFound : idx;
}
template <typename T>
int32_t nsTStringRepr<T>::RFindChar(char_type aChar, int32_t aOffset) const {
auto idx = View().rfind(aChar, aOffset != -1 ? aOffset : string_view::npos);
return idx == string_view::npos ? kNotFound : idx;
}
template <typename T>
int32_t nsTStringRepr<T>::FindCharInSet(const string_view& aSet,
index_type aOffset) const {
auto idx = View().find_first_of(aSet, aOffset);
return idx == string_view::npos ? kNotFound : idx;
}
template <typename T>
int32_t nsTStringRepr<T>::RFindCharInSet(const string_view& aSet,
int32_t aOffset) const {
auto idx =
View().find_last_of(aSet, aOffset != -1 ? aOffset : string_view::npos);
return idx == string_view::npos ? kNotFound : idx;
}
template <typename T>
bool nsTStringRepr<T>::EqualsIgnoreCase(const std::string_view& aString) const {
return std::equal(BeginReading(), EndReading(), aString.begin(),
aString.end(), [](char_type l, char r) {
return char_traits::ASCIIToLower(l) ==
char_traits::ASCIIToLower(char_type(r));
});
}
// We can't use the method `StringToDoubleConverter::ToDouble` due to linking
// issues on Windows as it's in mozglue. Instead, implement the selection logic
// using an overload set.
//
// StringToFloat is used instead of StringToDouble for floats due to differences
// in rounding behaviour.
static void StringToFP(
const double_conversion::StringToDoubleConverter& aConverter,
const char* aData, size_t aLength, int* aProcessed, float* aResult) {
*aResult = aConverter.StringToFloat(aData, aLength, aProcessed);
}
static void StringToFP(
const double_conversion::StringToDoubleConverter& aConverter,
const char* aData, size_t aLength, int* aProcessed, double* aResult) {
*aResult = aConverter.StringToDouble(aData, aLength, aProcessed);
}
static void StringToFP(
const double_conversion::StringToDoubleConverter& aConverter,
const char16_t* aData, size_t aLength, int* aProcessed, float* aResult) {
*aResult = aConverter.StringToFloat(reinterpret_cast<const uc16*>(aData),
aLength, aProcessed);
}
static void StringToFP(
const double_conversion::StringToDoubleConverter& aConverter,
const char16_t* aData, size_t aLength, int* aProcessed, double* aResult) {
*aResult = aConverter.StringToDouble(reinterpret_cast<const uc16*>(aData),
aLength, aProcessed);
}
template <typename FloatT, typename CharT>
static FloatT ParseFloatingPoint(const nsTStringRepr<CharT>& aString,
bool aAllowTrailingChars,
nsresult* aErrorCode) {
// Performs conversion to double following the "rules for parsing
// floating-point number values" from the HTML standard.
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-floating-point-number-values
//
// This behaviour allows for leading spaces, and will not generate infinity or
// NaN values except in error conditions.
int flags = double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES;
if (aAllowTrailingChars) {
flags |= double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK;
}
double_conversion::StringToDoubleConverter converter(
flags, mozilla::UnspecifiedNaN<double>(),
mozilla::UnspecifiedNaN<double>(), nullptr, nullptr);
FloatT result;
int processed;
StringToFP(converter, aString.Data(), aString.Length(), &processed, &result);
*aErrorCode = mozilla::IsFinite(result) ? NS_OK : NS_ERROR_ILLEGAL_VALUE;
return result;
}
template <typename T>
double nsTStringRepr<T>::ToDouble(nsresult* aErrorCode) const {
return ParseFloatingPoint<double, T>(*this, /* aAllowTrailingChars */ false,
aErrorCode);
}
template <typename T>
double nsTStringRepr<T>::ToDoubleAllowTrailingChars(
nsresult* aErrorCode) const {
return ParseFloatingPoint<double, T>(*this, /* aAllowTrailingChars */ true,
aErrorCode);
}
template <typename T>
float nsTStringRepr<T>::ToFloat(nsresult* aErrorCode) const {
return ParseFloatingPoint<float, T>(*this, /* aAllowTrailingChars */ false,
aErrorCode);
}
template <typename T>
float nsTStringRepr<T>::ToFloatAllowTrailingChars(nsresult* aErrorCode) const {
return ParseFloatingPoint<float, T>(*this, /* aAllowTrailingChars */ true,
aErrorCode);
}
} // namespace mozilla::detail
template class mozilla::detail::nsTStringRepr<char>;
template class mozilla::detail::nsTStringRepr<char16_t>;
|