Commit 11327983 authored by John Delaney's avatar John Delaney Committed by Commit Bot

Add HexToUint64 parsing for wtf_string

This adds a util to parse a hex string to uint64. This is necessary for
the ConversionMeasurement API, as the impression data attribute is a
64-bit identifier provided as a hex string.

Link to explainer:
https://github.com/WICG/conversion-measurement-api#impression-declaration

Usage of this is included on https://crrev.com/c/2012883 in
html_anchor_element.cc

Change-Id: I18aeef488477a83dbcb31351fd9179f4957f5f5a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2123811Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Commit-Queue: John Delaney <johnidel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#754515}
parent aa91d8cd
...@@ -701,6 +701,15 @@ wtf_size_t StringImpl::HexToUIntStrict(bool* ok) { ...@@ -701,6 +701,15 @@ wtf_size_t StringImpl::HexToUIntStrict(bool* ok) {
NumberParsingOptions::kStrict, ok); NumberParsingOptions::kStrict, ok);
} }
uint64_t StringImpl::HexToUInt64Strict(bool* ok) {
if (Is8Bit()) {
return HexCharactersToUInt64(Characters8(), length_,
NumberParsingOptions::kStrict, ok);
}
return HexCharactersToUInt64(Characters16(), length_,
NumberParsingOptions::kStrict, ok);
}
int64_t StringImpl::ToInt64(NumberParsingOptions options, bool* ok) const { int64_t StringImpl::ToInt64(NumberParsingOptions options, bool* ok) const {
if (Is8Bit()) if (Is8Bit())
return CharactersToInt64(Characters8(), length_, options, ok); return CharactersToInt64(Characters8(), length_, options, ok);
......
...@@ -345,6 +345,7 @@ class WTF_EXPORT StringImpl { ...@@ -345,6 +345,7 @@ class WTF_EXPORT StringImpl {
uint64_t ToUInt64(NumberParsingOptions, bool* ok) const; uint64_t ToUInt64(NumberParsingOptions, bool* ok) const;
wtf_size_t HexToUIntStrict(bool* ok); wtf_size_t HexToUIntStrict(bool* ok);
uint64_t HexToUInt64Strict(bool* ok);
// FIXME: Like NumberParsingOptions::kStrict, these give false for "ok" when // FIXME: Like NumberParsingOptions::kStrict, these give false for "ok" when
// there is trailing garbage. Like NumberParsingOptions::kLoose, these return // there is trailing garbage. Like NumberParsingOptions::kLoose, these return
......
...@@ -175,6 +175,20 @@ unsigned HexCharactersToUInt(const UChar* data, ...@@ -175,6 +175,20 @@ unsigned HexCharactersToUInt(const UChar* data,
return ToIntegralType<unsigned, UChar, 16>(data, length, options, ok); return ToIntegralType<unsigned, UChar, 16>(data, length, options, ok);
} }
uint64_t HexCharactersToUInt64(const LChar* data,
size_t length,
NumberParsingOptions options,
bool* ok) {
return ToIntegralType<uint64_t, LChar, 16>(data, length, options, ok);
}
uint64_t HexCharactersToUInt64(const UChar* data,
size_t length,
NumberParsingOptions options,
bool* ok) {
return ToIntegralType<uint64_t, UChar, 16>(data, length, options, ok);
}
int CharactersToInt(const LChar* data, int CharactersToInt(const LChar* data,
size_t length, size_t length,
NumberParsingOptions options, NumberParsingOptions options,
......
...@@ -40,6 +40,14 @@ WTF_EXPORT unsigned HexCharactersToUInt(const UChar*, ...@@ -40,6 +40,14 @@ WTF_EXPORT unsigned HexCharactersToUInt(const UChar*,
size_t, size_t,
NumberParsingOptions, NumberParsingOptions,
bool* ok); bool* ok);
WTF_EXPORT uint64_t HexCharactersToUInt64(const UChar*,
size_t,
NumberParsingOptions,
bool* ok);
WTF_EXPORT uint64_t HexCharactersToUInt64(const LChar*,
size_t,
NumberParsingOptions,
bool* ok);
WTF_EXPORT unsigned CharactersToUInt(const LChar*, WTF_EXPORT unsigned CharactersToUInt(const LChar*,
size_t, size_t,
NumberParsingOptions, NumberParsingOptions,
......
...@@ -295,6 +295,15 @@ unsigned String::HexToUIntStrict(bool* ok) const { ...@@ -295,6 +295,15 @@ unsigned String::HexToUIntStrict(bool* ok) const {
return impl_->HexToUIntStrict(ok); return impl_->HexToUIntStrict(ok);
} }
uint64_t String::HexToUInt64Strict(bool* ok) const {
if (!impl_) {
if (ok)
*ok = false;
return 0;
}
return impl_->HexToUInt64Strict(ok);
}
int64_t String::ToInt64Strict(bool* ok) const { int64_t String::ToInt64Strict(bool* ok) const {
if (!impl_) { if (!impl_) {
if (ok) if (ok)
......
...@@ -422,6 +422,7 @@ class WTF_EXPORT String { ...@@ -422,6 +422,7 @@ class WTF_EXPORT String {
int ToIntStrict(bool* ok = nullptr) const; int ToIntStrict(bool* ok = nullptr) const;
unsigned ToUIntStrict(bool* ok = nullptr) const; unsigned ToUIntStrict(bool* ok = nullptr) const;
unsigned HexToUIntStrict(bool* ok) const; unsigned HexToUIntStrict(bool* ok) const;
uint64_t HexToUInt64Strict(bool* ok) const;
int64_t ToInt64Strict(bool* ok = nullptr) const; int64_t ToInt64Strict(bool* ok = nullptr) const;
uint64_t ToUInt64Strict(bool* ok = nullptr) const; uint64_t ToUInt64Strict(bool* ok = nullptr) const;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment