2018-04-09 22:02:43 +03:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
|
|
|
/* Character/text operations. */
|
|
|
|
|
|
|
|
#ifndef mozilla_TextUtils_h
|
|
|
|
#define mozilla_TextUtils_h
|
|
|
|
|
2018-04-10 21:42:54 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2019-09-18 11:26:34 +03:00
|
|
|
#include "mozilla/Latin1.h"
|
2018-04-09 22:02:43 +03:00
|
|
|
|
2019-09-26 15:45:00 +03:00
|
|
|
#ifdef MOZ_HAS_JSRUST
|
|
|
|
// Can't include mozilla/Encoding.h here.
|
|
|
|
extern "C" {
|
|
|
|
// Declared as uint8_t instead of char to match declaration in another header.
|
|
|
|
size_t encoding_ascii_valid_up_to(uint8_t const* buffer, size_t buffer_len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-04-09 22:02:43 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2019-09-18 11:26:34 +03:00
|
|
|
// See Utf8.h for IsUtf8() and conversions between UTF-8 and UTF-16.
|
|
|
|
// See Latin1.h for testing UTF-16 and UTF-8 for Latin1ness and
|
|
|
|
// for conversions to and from Latin1.
|
2018-04-09 22:02:43 +03:00
|
|
|
|
2019-09-18 11:26:34 +03:00
|
|
|
// The overloads below are not templated in order to make
|
|
|
|
// implicit conversions to span work as expected for the Span
|
|
|
|
// overloads.
|
|
|
|
|
|
|
|
/** Returns true iff |aChar| is ASCII, i.e. in the range [0, 0x80). */
|
Bug 1795644 - A bit of tuning of inlining in the JS parser. r=arai.
Some profiling of tcampbell's React benchmark [1] shows 6.1 million calls to
js::frontend::GeneralTokenStreamChars<..>::getFullAsciiCodePoint, using 110.57
million instructions (x86_64). That comes out to only 18 insns per call,
which suggests the method is a good candidate for inlining, but it isn't
inlined.
Merely parking an inline annotation on it doesn't help much, because it gets
inlined into js::frontend::TokenStreamSpecific<..>::getCodePoint, but that
isn't inlined into *its* caller(s), so the 6.1 million calls move to
::getCodePoint instead.
This patch requests inlining for both ::getFullAsciiCodePoint and
::getCodePoint and adds some MOZ_NEVER_INLINE annotations to ensure that cold
paths *don't* get inlined into these two, to reduce code bloat and avoid
excessive register pressure.
IsAscii functions in mfbt/TextUtils.h have been marked inline as a precaution;
this probably isn't necessary.
Run time with config [2] is reduced from 0.390 seconds to 0.379 seconds
(2.8% speedup, best of 50 runs), and from 0.402 to 0.396 seconds
(median of 50 runs).
Instruction count falls from 3511.8 million to 3395.8 million, and the number
of data accesses from 1563.7 million to 1487.4 million -- a 4.8% reduction
that is probably caused by avoidance of save/restore sequences in the inlined
fns.
[1] https://github.com/mozilla-spidermonkey/matrix-react-bench
[2] Fedora 35, x86_64, Intel Core i5 1135G7 at 4 ish GHz
configure: --disable-debug --enable-optimize="-g -O2"
run: --no-threads
Differential Revision: https://phabricator.services.mozilla.com/D159500
2022-10-18 08:22:03 +03:00
|
|
|
inline constexpr bool IsAscii(unsigned char aChar) { return aChar < 0x80; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-09-18 11:26:34 +03:00
|
|
|
/** Returns true iff |aChar| is ASCII, i.e. in the range [0, 0x80). */
|
Bug 1795644 - A bit of tuning of inlining in the JS parser. r=arai.
Some profiling of tcampbell's React benchmark [1] shows 6.1 million calls to
js::frontend::GeneralTokenStreamChars<..>::getFullAsciiCodePoint, using 110.57
million instructions (x86_64). That comes out to only 18 insns per call,
which suggests the method is a good candidate for inlining, but it isn't
inlined.
Merely parking an inline annotation on it doesn't help much, because it gets
inlined into js::frontend::TokenStreamSpecific<..>::getCodePoint, but that
isn't inlined into *its* caller(s), so the 6.1 million calls move to
::getCodePoint instead.
This patch requests inlining for both ::getFullAsciiCodePoint and
::getCodePoint and adds some MOZ_NEVER_INLINE annotations to ensure that cold
paths *don't* get inlined into these two, to reduce code bloat and avoid
excessive register pressure.
IsAscii functions in mfbt/TextUtils.h have been marked inline as a precaution;
this probably isn't necessary.
Run time with config [2] is reduced from 0.390 seconds to 0.379 seconds
(2.8% speedup, best of 50 runs), and from 0.402 to 0.396 seconds
(median of 50 runs).
Instruction count falls from 3511.8 million to 3395.8 million, and the number
of data accesses from 1563.7 million to 1487.4 million -- a 4.8% reduction
that is probably caused by avoidance of save/restore sequences in the inlined
fns.
[1] https://github.com/mozilla-spidermonkey/matrix-react-bench
[2] Fedora 35, x86_64, Intel Core i5 1135G7 at 4 ish GHz
configure: --disable-debug --enable-optimize="-g -O2"
run: --no-threads
Differential Revision: https://phabricator.services.mozilla.com/D159500
2022-10-18 08:22:03 +03:00
|
|
|
inline constexpr bool IsAscii(signed char aChar) {
|
2019-09-18 11:26:34 +03:00
|
|
|
return IsAscii(static_cast<unsigned char>(aChar));
|
|
|
|
}
|
2018-04-09 22:02:43 +03:00
|
|
|
|
2019-09-18 11:26:34 +03:00
|
|
|
/** Returns true iff |aChar| is ASCII, i.e. in the range [0, 0x80). */
|
Bug 1795644 - A bit of tuning of inlining in the JS parser. r=arai.
Some profiling of tcampbell's React benchmark [1] shows 6.1 million calls to
js::frontend::GeneralTokenStreamChars<..>::getFullAsciiCodePoint, using 110.57
million instructions (x86_64). That comes out to only 18 insns per call,
which suggests the method is a good candidate for inlining, but it isn't
inlined.
Merely parking an inline annotation on it doesn't help much, because it gets
inlined into js::frontend::TokenStreamSpecific<..>::getCodePoint, but that
isn't inlined into *its* caller(s), so the 6.1 million calls move to
::getCodePoint instead.
This patch requests inlining for both ::getFullAsciiCodePoint and
::getCodePoint and adds some MOZ_NEVER_INLINE annotations to ensure that cold
paths *don't* get inlined into these two, to reduce code bloat and avoid
excessive register pressure.
IsAscii functions in mfbt/TextUtils.h have been marked inline as a precaution;
this probably isn't necessary.
Run time with config [2] is reduced from 0.390 seconds to 0.379 seconds
(2.8% speedup, best of 50 runs), and from 0.402 to 0.396 seconds
(median of 50 runs).
Instruction count falls from 3511.8 million to 3395.8 million, and the number
of data accesses from 1563.7 million to 1487.4 million -- a 4.8% reduction
that is probably caused by avoidance of save/restore sequences in the inlined
fns.
[1] https://github.com/mozilla-spidermonkey/matrix-react-bench
[2] Fedora 35, x86_64, Intel Core i5 1135G7 at 4 ish GHz
configure: --disable-debug --enable-optimize="-g -O2"
run: --no-threads
Differential Revision: https://phabricator.services.mozilla.com/D159500
2022-10-18 08:22:03 +03:00
|
|
|
inline constexpr bool IsAscii(char aChar) {
|
2019-09-18 11:26:34 +03:00
|
|
|
return IsAscii(static_cast<unsigned char>(aChar));
|
|
|
|
}
|
2018-04-09 22:02:43 +03:00
|
|
|
|
2024-02-14 04:33:31 +03:00
|
|
|
#ifdef __cpp_char8_t
|
|
|
|
/** Returns true iff |aChar| is ASCII, i.e. in the range [0, 0x80). */
|
|
|
|
inline constexpr bool IsAscii(char8_t aChar) {
|
|
|
|
return IsAscii(static_cast<unsigned char>(aChar));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-09-18 11:26:34 +03:00
|
|
|
/** Returns true iff |aChar| is ASCII, i.e. in the range [0, 0x80). */
|
Bug 1795644 - A bit of tuning of inlining in the JS parser. r=arai.
Some profiling of tcampbell's React benchmark [1] shows 6.1 million calls to
js::frontend::GeneralTokenStreamChars<..>::getFullAsciiCodePoint, using 110.57
million instructions (x86_64). That comes out to only 18 insns per call,
which suggests the method is a good candidate for inlining, but it isn't
inlined.
Merely parking an inline annotation on it doesn't help much, because it gets
inlined into js::frontend::TokenStreamSpecific<..>::getCodePoint, but that
isn't inlined into *its* caller(s), so the 6.1 million calls move to
::getCodePoint instead.
This patch requests inlining for both ::getFullAsciiCodePoint and
::getCodePoint and adds some MOZ_NEVER_INLINE annotations to ensure that cold
paths *don't* get inlined into these two, to reduce code bloat and avoid
excessive register pressure.
IsAscii functions in mfbt/TextUtils.h have been marked inline as a precaution;
this probably isn't necessary.
Run time with config [2] is reduced from 0.390 seconds to 0.379 seconds
(2.8% speedup, best of 50 runs), and from 0.402 to 0.396 seconds
(median of 50 runs).
Instruction count falls from 3511.8 million to 3395.8 million, and the number
of data accesses from 1563.7 million to 1487.4 million -- a 4.8% reduction
that is probably caused by avoidance of save/restore sequences in the inlined
fns.
[1] https://github.com/mozilla-spidermonkey/matrix-react-bench
[2] Fedora 35, x86_64, Intel Core i5 1135G7 at 4 ish GHz
configure: --disable-debug --enable-optimize="-g -O2"
run: --no-threads
Differential Revision: https://phabricator.services.mozilla.com/D159500
2022-10-18 08:22:03 +03:00
|
|
|
inline constexpr bool IsAscii(char16_t aChar) { return aChar < 0x80; }
|
2018-04-09 22:02:43 +03:00
|
|
|
|
2018-06-07 12:17:10 +03:00
|
|
|
/** Returns true iff |aChar| is ASCII, i.e. in the range [0, 0x80). */
|
Bug 1795644 - A bit of tuning of inlining in the JS parser. r=arai.
Some profiling of tcampbell's React benchmark [1] shows 6.1 million calls to
js::frontend::GeneralTokenStreamChars<..>::getFullAsciiCodePoint, using 110.57
million instructions (x86_64). That comes out to only 18 insns per call,
which suggests the method is a good candidate for inlining, but it isn't
inlined.
Merely parking an inline annotation on it doesn't help much, because it gets
inlined into js::frontend::TokenStreamSpecific<..>::getCodePoint, but that
isn't inlined into *its* caller(s), so the 6.1 million calls move to
::getCodePoint instead.
This patch requests inlining for both ::getFullAsciiCodePoint and
::getCodePoint and adds some MOZ_NEVER_INLINE annotations to ensure that cold
paths *don't* get inlined into these two, to reduce code bloat and avoid
excessive register pressure.
IsAscii functions in mfbt/TextUtils.h have been marked inline as a precaution;
this probably isn't necessary.
Run time with config [2] is reduced from 0.390 seconds to 0.379 seconds
(2.8% speedup, best of 50 runs), and from 0.402 to 0.396 seconds
(median of 50 runs).
Instruction count falls from 3511.8 million to 3395.8 million, and the number
of data accesses from 1563.7 million to 1487.4 million -- a 4.8% reduction
that is probably caused by avoidance of save/restore sequences in the inlined
fns.
[1] https://github.com/mozilla-spidermonkey/matrix-react-bench
[2] Fedora 35, x86_64, Intel Core i5 1135G7 at 4 ish GHz
configure: --disable-debug --enable-optimize="-g -O2"
run: --no-threads
Differential Revision: https://phabricator.services.mozilla.com/D159500
2022-10-18 08:22:03 +03:00
|
|
|
inline constexpr bool IsAscii(char32_t aChar) { return aChar < 0x80; }
|
2019-09-18 11:26:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns |true| iff |aString| contains only ASCII characters, that is,
|
|
|
|
* characters in the range [0x00, 0x80).
|
|
|
|
*
|
|
|
|
* @param aString a 8-bit wide string to scan
|
|
|
|
*/
|
|
|
|
inline bool IsAscii(mozilla::Span<const char> aString) {
|
|
|
|
#if MOZ_HAS_JSRUST()
|
|
|
|
size_t length = aString.Length();
|
|
|
|
const char* ptr = aString.Elements();
|
|
|
|
// For short strings, avoid the function call, since, the SIMD
|
|
|
|
// code won't have a chance to kick in anyway.
|
2019-09-26 15:46:16 +03:00
|
|
|
if (length < mozilla::detail::kShortStringLimitForInlinePaths) {
|
2019-09-18 11:26:34 +03:00
|
|
|
const uint8_t* uptr = reinterpret_cast<const uint8_t*>(ptr);
|
|
|
|
uint8_t accu = 0;
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
accu |= uptr[i];
|
|
|
|
}
|
|
|
|
return accu < 0x80;
|
|
|
|
}
|
|
|
|
return encoding_mem_is_ascii(ptr, length);
|
|
|
|
#else
|
|
|
|
for (char c : aString) {
|
|
|
|
if (!IsAscii(c)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns |true| iff |aString| contains only ASCII characters, that is,
|
|
|
|
* characters in the range [0x00, 0x80).
|
|
|
|
*
|
|
|
|
* @param aString a 16-bit wide string to scan
|
|
|
|
*/
|
|
|
|
inline bool IsAscii(mozilla::Span<const char16_t> aString) {
|
|
|
|
#if MOZ_HAS_JSRUST()
|
2019-09-26 15:46:16 +03:00
|
|
|
size_t length = aString.Length();
|
|
|
|
const char16_t* ptr = aString.Elements();
|
|
|
|
// For short strings, calling into Rust is a pessimization, and the SIMD
|
|
|
|
// code won't have a chance to kick in anyway.
|
|
|
|
// 16 is a bit larger than logically necessary for this function alone,
|
|
|
|
// but it's important that the limit here matches the limit used in
|
|
|
|
// LossyConvertUtf16toLatin1!
|
|
|
|
if (length < mozilla::detail::kShortStringLimitForInlinePaths) {
|
|
|
|
char16_t accu = 0;
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
accu |= ptr[i];
|
|
|
|
}
|
|
|
|
return accu < 0x80;
|
|
|
|
}
|
|
|
|
return encoding_mem_is_basic_latin(ptr, length);
|
2019-09-18 11:26:34 +03:00
|
|
|
#else
|
|
|
|
for (char16_t c : aString) {
|
|
|
|
if (!IsAscii(c)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
#endif
|
2018-06-07 12:17:10 +03:00
|
|
|
}
|
|
|
|
|
2019-02-25 23:22:24 +03:00
|
|
|
/**
|
|
|
|
* Returns true iff every character in the null-terminated string pointed to by
|
|
|
|
* |aChar| is ASCII, i.e. in the range [0, 0x80).
|
|
|
|
*/
|
|
|
|
template <typename Char>
|
|
|
|
constexpr bool IsAsciiNullTerminated(const Char* aChar) {
|
|
|
|
while (Char c = *aChar++) {
|
|
|
|
if (!IsAscii(c)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-18 11:26:34 +03:00
|
|
|
#if MOZ_HAS_JSRUST()
|
2019-09-26 15:45:00 +03:00
|
|
|
/**
|
|
|
|
* Returns the index of the first non-ASCII byte or
|
|
|
|
* the length of the string if there are none.
|
|
|
|
*/
|
|
|
|
inline size_t AsciiValidUpTo(mozilla::Span<const char> aString) {
|
|
|
|
return encoding_ascii_valid_up_to(
|
|
|
|
reinterpret_cast<const uint8_t*>(aString.Elements()), aString.Length());
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:22:24 +03:00
|
|
|
/**
|
2019-09-18 11:26:34 +03:00
|
|
|
* Returns the index of the first unpaired surrogate or
|
|
|
|
* the length of the string if there are none.
|
2019-02-25 23:22:24 +03:00
|
|
|
*/
|
2019-09-18 11:26:34 +03:00
|
|
|
inline size_t Utf16ValidUpTo(mozilla::Span<const char16_t> aString) {
|
|
|
|
return encoding_mem_utf16_valid_up_to(aString.Elements(), aString.Length());
|
2018-09-07 08:41:29 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 11:26:34 +03:00
|
|
|
/**
|
|
|
|
* Replaces unpaired surrogates with U+FFFD in the argument.
|
|
|
|
*
|
|
|
|
* Note: If you have an nsAString, use EnsureUTF16Validity() from
|
|
|
|
* nsReadableUtils.h instead to avoid unsharing a valid shared
|
|
|
|
* string.
|
|
|
|
*/
|
|
|
|
inline void EnsureUtf16ValiditySpan(mozilla::Span<char16_t> aString) {
|
|
|
|
encoding_mem_ensure_utf16_validity(aString.Elements(), aString.Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert ASCII to UTF-16. In debug builds, assert that the input is
|
|
|
|
* ASCII.
|
|
|
|
*
|
|
|
|
* The length of aDest must not be less than the length of aSource.
|
|
|
|
*/
|
|
|
|
inline void ConvertAsciitoUtf16(mozilla::Span<const char> aSource,
|
|
|
|
mozilla::Span<char16_t> aDest) {
|
|
|
|
MOZ_ASSERT(IsAscii(aSource));
|
|
|
|
ConvertLatin1toUtf16(aSource, aDest);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MOZ_HAS_JSRUST
|
|
|
|
|
2018-08-13 23:12:10 +03:00
|
|
|
/**
|
|
|
|
* Returns true iff |aChar| matches Ascii Whitespace.
|
|
|
|
*
|
|
|
|
* This function is intended to match the Infra standard
|
|
|
|
* (https://infra.spec.whatwg.org/#ascii-whitespace)
|
|
|
|
*/
|
|
|
|
template <typename Char>
|
|
|
|
constexpr bool IsAsciiWhitespace(Char aChar) {
|
|
|
|
using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
|
|
|
|
auto uc = static_cast<UnsignedChar>(aChar);
|
|
|
|
return uc == 0x9 || uc == 0xA || uc == 0xC || uc == 0xD || uc == 0x20;
|
|
|
|
}
|
|
|
|
|
2018-04-10 21:42:54 +03:00
|
|
|
/**
|
|
|
|
* Returns true iff |aChar| matches [a-z].
|
|
|
|
*
|
|
|
|
* This function is basically what you thought islower was, except its behavior
|
|
|
|
* doesn't depend on the user's current locale.
|
|
|
|
*/
|
|
|
|
template <typename Char>
|
|
|
|
constexpr bool IsAsciiLowercaseAlpha(Char aChar) {
|
|
|
|
using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
|
|
|
|
auto uc = static_cast<UnsignedChar>(aChar);
|
|
|
|
return 'a' <= uc && uc <= 'z';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true iff |aChar| matches [A-Z].
|
|
|
|
*
|
|
|
|
* This function is basically what you thought isupper was, except its behavior
|
|
|
|
* doesn't depend on the user's current locale.
|
|
|
|
*/
|
|
|
|
template <typename Char>
|
|
|
|
constexpr bool IsAsciiUppercaseAlpha(Char aChar) {
|
|
|
|
using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
|
|
|
|
auto uc = static_cast<UnsignedChar>(aChar);
|
|
|
|
return 'A' <= uc && uc <= 'Z';
|
|
|
|
}
|
|
|
|
|
2018-04-09 22:02:43 +03:00
|
|
|
/**
|
|
|
|
* Returns true iff |aChar| matches [a-zA-Z].
|
|
|
|
*
|
|
|
|
* This function is basically what you thought isalpha was, except its behavior
|
|
|
|
* doesn't depend on the user's current locale.
|
|
|
|
*/
|
|
|
|
template <typename Char>
|
|
|
|
constexpr bool IsAsciiAlpha(Char aChar) {
|
2018-04-10 21:42:54 +03:00
|
|
|
return IsAsciiLowercaseAlpha(aChar) || IsAsciiUppercaseAlpha(aChar);
|
|
|
|
}
|
|
|
|
|
2018-04-13 06:19:30 +03:00
|
|
|
/**
|
|
|
|
* Returns true iff |aChar| matches [0-9].
|
|
|
|
*
|
|
|
|
* This function is basically what you thought isdigit was, except its behavior
|
|
|
|
* doesn't depend on the user's current locale.
|
|
|
|
*/
|
|
|
|
template <typename Char>
|
|
|
|
constexpr bool IsAsciiDigit(Char aChar) {
|
|
|
|
using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
|
|
|
|
auto uc = static_cast<UnsignedChar>(aChar);
|
|
|
|
return '0' <= uc && uc <= '9';
|
|
|
|
}
|
|
|
|
|
2018-06-21 03:21:17 +03:00
|
|
|
/**
|
|
|
|
* Returns true iff |aChar| matches [0-9a-fA-F].
|
|
|
|
*
|
|
|
|
* This function is basically isxdigit, but guaranteed to be only for ASCII.
|
|
|
|
*/
|
|
|
|
template <typename Char>
|
|
|
|
constexpr bool IsAsciiHexDigit(Char aChar) {
|
|
|
|
using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
|
|
|
|
auto uc = static_cast<UnsignedChar>(aChar);
|
|
|
|
return ('0' <= uc && uc <= '9') || ('a' <= uc && uc <= 'f') ||
|
|
|
|
('A' <= uc && uc <= 'F');
|
|
|
|
}
|
|
|
|
|
2018-04-10 21:42:54 +03:00
|
|
|
/**
|
|
|
|
* Returns true iff |aChar| matches [a-zA-Z0-9].
|
|
|
|
*
|
|
|
|
* This function is basically what you thought isalnum was, except its behavior
|
|
|
|
* doesn't depend on the user's current locale.
|
|
|
|
*/
|
|
|
|
template <typename Char>
|
|
|
|
constexpr bool IsAsciiAlphanumeric(Char aChar) {
|
2018-04-13 06:19:30 +03:00
|
|
|
return IsAsciiDigit(aChar) || IsAsciiAlpha(aChar);
|
2018-04-10 21:42:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts an ASCII alphanumeric digit [0-9a-zA-Z] to number as if in base-36.
|
|
|
|
* (This function therefore works for decimal, hexadecimal, etc.).
|
|
|
|
*/
|
|
|
|
template <typename Char>
|
|
|
|
uint8_t AsciiAlphanumericToNumber(Char aChar) {
|
|
|
|
using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
|
|
|
|
auto uc = static_cast<UnsignedChar>(aChar);
|
|
|
|
|
|
|
|
if ('0' <= uc && uc <= '9') {
|
|
|
|
return uc - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('A' <= uc && uc <= 'Z') {
|
|
|
|
return uc - 'A' + 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ideally this function would be constexpr, but unfortunately gcc at least as
|
|
|
|
// of 6.4 forbids non-constexpr function calls in unevaluated constexpr
|
|
|
|
// function calls. See bug 1453456. So for now, just assert and leave the
|
|
|
|
// entire function non-constexpr.
|
|
|
|
MOZ_ASSERT('a' <= uc && uc <= 'z',
|
|
|
|
"non-ASCII alphanumeric character can't be converted to number");
|
|
|
|
return uc - 'a' + 10;
|
2018-04-09 22:02:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_TextUtils_h */
|