diff options
author | Jeff Walden <jwalden@mit.edu> | 2018-04-09 12:02:43 -0700 |
---|---|---|
committer | wolfbeast <mcwerewolf@gmail.com> | 2018-04-29 13:39:27 +0200 |
commit | d308db119d48ce858b194a720f7946349b5f9b47 (patch) | |
tree | 2e0ad36f4fec1a5a79300b4ed449ad28e7e9ae64 | |
parent | 66aa6b595c47231958a855e4b166f5d55df1184d (diff) | |
download | UXP-d308db119d48ce858b194a720f7946349b5f9b47.tar UXP-d308db119d48ce858b194a720f7946349b5f9b47.tar.gz UXP-d308db119d48ce858b194a720f7946349b5f9b47.tar.lz UXP-d308db119d48ce858b194a720f7946349b5f9b47.tar.xz UXP-d308db119d48ce858b194a720f7946349b5f9b47.zip |
Bug 1452619 - Implement mozilla::IsAsciiAlpha. r=froydnj, a=lizzard
-rw-r--r-- | js/src/jsstr.h | 3 | ||||
-rw-r--r-- | mfbt/TextUtils.h | 58 | ||||
-rw-r--r-- | mfbt/moz.build | 1 | ||||
-rw-r--r-- | mfbt/tests/TestTextUtils.cpp | 106 | ||||
-rw-r--r-- | mfbt/tests/moz.build | 1 |
5 files changed, 168 insertions, 1 deletions
diff --git a/js/src/jsstr.h b/js/src/jsstr.h index 3b92aa21b..7e9621d4a 100644 --- a/js/src/jsstr.h +++ b/js/src/jsstr.h @@ -9,6 +9,7 @@ #include "mozilla/HashFunctions.h" #include "mozilla/PodOperations.h" +#include "mozilla/TextUtils.h" #include <stdio.h> @@ -95,7 +96,7 @@ struct JSSubString { #define JS7_UNOCT(c) (JS7_UNDEC(c)) #define JS7_ISHEX(c) ((c) < 128 && isxdigit(c)) #define JS7_UNHEX(c) (unsigned)(JS7_ISDEC(c) ? (c) - '0' : 10 + tolower(c) - 'a') -#define JS7_ISLET(c) ((c) < 128 && isalpha(c)) +#define JS7_ISLET(c) (mozilla::IsAsciiAlpha(c)) extern size_t js_strlen(const char16_t* s); diff --git a/mfbt/TextUtils.h b/mfbt/TextUtils.h new file mode 100644 index 000000000..9494296ce --- /dev/null +++ b/mfbt/TextUtils.h @@ -0,0 +1,58 @@ +/* -*- 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 + +#include "mozilla/TypeTraits.h" + +namespace mozilla { + +namespace detail { + +template<typename Char> +class MakeUnsignedChar + : public MakeUnsigned<Char> +{}; + +template<> +class MakeUnsignedChar<char16_t> +{ +public: + using Type = char16_t; +}; + +template<> +class MakeUnsignedChar<char32_t> +{ +public: + using Type = char32_t; +}; + +} // namespace detail + +/** + * 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) +{ + using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type; + return ('a' <= static_cast<UnsignedChar>(aChar) && + static_cast<UnsignedChar>(aChar) <= 'z') || + ('A' <= static_cast<UnsignedChar>(aChar) && + static_cast<UnsignedChar>(aChar) <= 'Z'); +} + +} // namespace mozilla + +#endif /* mozilla_TextUtils_h */ diff --git a/mfbt/moz.build b/mfbt/moz.build index f23a3b6f5..897a686f4 100644 --- a/mfbt/moz.build +++ b/mfbt/moz.build @@ -87,6 +87,7 @@ EXPORTS.mozilla = [ 'StaticAnalysisFunctions.h', 'TaggedAnonymousMemory.h', 'TemplateLib.h', + 'TextUtils.h', 'ThreadLocal.h', 'ToString.h', 'Tuple.h', diff --git a/mfbt/tests/TestTextUtils.cpp b/mfbt/tests/TestTextUtils.cpp new file mode 100644 index 000000000..db481c138 --- /dev/null +++ b/mfbt/tests/TestTextUtils.cpp @@ -0,0 +1,106 @@ +/* -*- 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 "mozilla/Assertions.h" +#include "mozilla/TextUtils.h" + +using mozilla::IsAsciiAlpha; + +// char + +static_assert(!IsAsciiAlpha('@'), "'@' isn't ASCII alpha"); +static_assert('@' == 0x40, "'@' has value 0x40"); + +static_assert('A' == 0x41, "'A' has value 0x41"); +static_assert(IsAsciiAlpha('A'), "'A' is ASCII alpha"); +static_assert(IsAsciiAlpha('B'), "'B' is ASCII alpha"); +static_assert(IsAsciiAlpha('M'), "'M' is ASCII alpha"); +static_assert(IsAsciiAlpha('Y'), "'Y' is ASCII alpha"); +static_assert(IsAsciiAlpha('Z'), "'Z' is ASCII alpha"); + +static_assert('Z' == 0x5A, "'Z' has value 0x5A"); +static_assert('[' == 0x5B, "'[' has value 0x5B"); +static_assert(!IsAsciiAlpha('['), "'[' isn't ASCII alpha"); + +static_assert(!IsAsciiAlpha('`'), "'`' isn't ASCII alpha"); +static_assert('`' == 0x60, "'`' has value 0x60"); + +static_assert('a' == 0x61, "'a' has value 0x61"); +static_assert(IsAsciiAlpha('a'), "'a' is ASCII alpha"); +static_assert(IsAsciiAlpha('b'), "'b' is ASCII alpha"); +static_assert(IsAsciiAlpha('m'), "'m' is ASCII alpha"); +static_assert(IsAsciiAlpha('y'), "'y' is ASCII alpha"); +static_assert(IsAsciiAlpha('z'), "'z' is ASCII alpha"); + +static_assert('z' == 0x7A, "'z' has value 0x7A"); +static_assert('{' == 0x7B, "'{' has value 0x7B"); +static_assert(!IsAsciiAlpha('{'), "'{' isn't ASCII alpha"); + +// char16_t + +static_assert(!IsAsciiAlpha(u'@'), "u'@' isn't ASCII alpha"); +static_assert(u'@' == 0x40, "u'@' has value 0x40"); + +static_assert(u'A' == 0x41, "u'A' has value 0x41"); +static_assert(IsAsciiAlpha(u'A'), "u'A' is ASCII alpha"); +static_assert(IsAsciiAlpha(u'B'), "u'B' is ASCII alpha"); +static_assert(IsAsciiAlpha(u'M'), "u'M' is ASCII alpha"); +static_assert(IsAsciiAlpha(u'Y'), "u'Y' is ASCII alpha"); +static_assert(IsAsciiAlpha(u'Z'), "u'Z' is ASCII alpha"); + +static_assert(u'Z' == 0x5A, "u'Z' has value 0x5A"); +static_assert(u'[' == 0x5B, "u'[' has value 0x5B"); +static_assert(!IsAsciiAlpha(u'['), "u'[' isn't ASCII alpha"); + +static_assert(!IsAsciiAlpha(u'`'), "u'`' isn't ASCII alpha"); +static_assert(u'`' == 0x60, "u'`' has value 0x60"); + +static_assert(u'a' == 0x61, "u'a' has value 0x61"); +static_assert(IsAsciiAlpha(u'a'), "u'a' is ASCII alpha"); +static_assert(IsAsciiAlpha(u'b'), "u'b' is ASCII alpha"); +static_assert(IsAsciiAlpha(u'm'), "u'm' is ASCII alpha"); +static_assert(IsAsciiAlpha(u'y'), "u'y' is ASCII alpha"); +static_assert(IsAsciiAlpha(u'z'), "u'z' is ASCII alpha"); + +static_assert(u'z' == 0x7A, "u'z' has value 0x7A"); +static_assert(u'{' == 0x7B, "u'{' has value 0x7B"); +static_assert(!IsAsciiAlpha(u'{'), "u'{' isn't ASCII alpha"); + +// char32_t + +static_assert(!IsAsciiAlpha(U'@'), "U'@' isn't ASCII alpha"); +static_assert(U'@' == 0x40, "U'@' has value 0x40"); + +static_assert(U'A' == 0x41, "U'A' has value 0x41"); +static_assert(IsAsciiAlpha(U'A'), "U'A' is ASCII alpha"); +static_assert(IsAsciiAlpha(U'B'), "U'B' is ASCII alpha"); +static_assert(IsAsciiAlpha(U'M'), "U'M' is ASCII alpha"); +static_assert(IsAsciiAlpha(U'Y'), "U'Y' is ASCII alpha"); +static_assert(IsAsciiAlpha(U'Z'), "U'Z' is ASCII alpha"); + +static_assert(U'Z' == 0x5A, "U'Z' has value 0x5A"); +static_assert(U'[' == 0x5B, "U'[' has value 0x5B"); +static_assert(!IsAsciiAlpha(U'['), "U'[' isn't ASCII alpha"); + +static_assert(!IsAsciiAlpha(U'`'), "U'`' isn't ASCII alpha"); +static_assert(U'`' == 0x60, "U'`' has value 0x60"); + +static_assert(U'a' == 0x61, "U'a' has value 0x61"); +static_assert(IsAsciiAlpha(U'a'), "U'a' is ASCII alpha"); +static_assert(IsAsciiAlpha(U'b'), "U'b' is ASCII alpha"); +static_assert(IsAsciiAlpha(U'm'), "U'm' is ASCII alpha"); +static_assert(IsAsciiAlpha(U'y'), "U'y' is ASCII alpha"); +static_assert(IsAsciiAlpha(U'z'), "U'z' is ASCII alpha"); + +static_assert(U'z' == 0x7A, "U'z' has value 0x7A"); +static_assert(U'{' == 0x7B, "U'{' has value 0x7B"); +static_assert(!IsAsciiAlpha(U'{'), "U'{' isn't ASCII alpha"); + +int +main() +{ + return 0; +} diff --git a/mfbt/tests/moz.build b/mfbt/tests/moz.build index f96117e03..bd25ab1d0 100644 --- a/mfbt/tests/moz.build +++ b/mfbt/tests/moz.build @@ -42,6 +42,7 @@ CppUnitTests([ 'TestSHA1', 'TestSplayTree', 'TestTemplateLib', + 'TestTextUtils', 'TestTuple', 'TestTypedEnum', 'TestTypeTraits', |