Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : /* Character/text operations. */
8 :
9 : #ifndef mozilla_TextUtils_h
10 : #define mozilla_TextUtils_h
11 :
12 : #include "mozilla/Assertions.h"
13 : #include "mozilla/TypeTraits.h"
14 :
15 : namespace mozilla {
16 :
17 : namespace detail {
18 :
19 : template<typename Char>
20 : class MakeUnsignedChar
21 : : public MakeUnsigned<Char>
22 : {};
23 :
24 : template<>
25 : class MakeUnsignedChar<char16_t>
26 : {
27 : public:
28 : using Type = char16_t;
29 : };
30 :
31 : template<>
32 : class MakeUnsignedChar<char32_t>
33 : {
34 : public:
35 : using Type = char32_t;
36 : };
37 :
38 : } // namespace detail
39 :
40 : /** Returns true iff |aChar| is ASCII, i.e. in the range [0, 0x80). */
41 : template<typename Char>
42 : constexpr bool
43 : IsAscii(Char aChar)
44 : {
45 : using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
46 2460189 : auto uc = static_cast<UnsignedChar>(aChar);
47 1232249 : return uc < 0x80;
48 : }
49 :
50 : /**
51 : * Returns true iff |aChar| matches [a-z].
52 : *
53 : * This function is basically what you thought islower was, except its behavior
54 : * doesn't depend on the user's current locale.
55 : */
56 : template<typename Char>
57 : constexpr bool
58 : IsAsciiLowercaseAlpha(Char aChar)
59 : {
60 : using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
61 53271 : auto uc = static_cast<UnsignedChar>(aChar);
62 53271 : return 'a' <= uc && uc <= 'z';
63 : }
64 :
65 : /**
66 : * Returns true iff |aChar| matches [A-Z].
67 : *
68 : * This function is basically what you thought isupper was, except its behavior
69 : * doesn't depend on the user's current locale.
70 : */
71 : template<typename Char>
72 : constexpr bool
73 : IsAsciiUppercaseAlpha(Char aChar)
74 : {
75 : using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
76 6722 : auto uc = static_cast<UnsignedChar>(aChar);
77 6722 : return 'A' <= uc && uc <= 'Z';
78 : }
79 :
80 : /**
81 : * Returns true iff |aChar| matches [a-zA-Z].
82 : *
83 : * This function is basically what you thought isalpha was, except its behavior
84 : * doesn't depend on the user's current locale.
85 : */
86 : template<typename Char>
87 : constexpr bool
88 52671 : IsAsciiAlpha(Char aChar)
89 : {
90 59993 : return IsAsciiLowercaseAlpha(aChar) || IsAsciiUppercaseAlpha(aChar);
91 : }
92 :
93 : /**
94 : * Returns true iff |aChar| matches [0-9].
95 : *
96 : * This function is basically what you thought isdigit was, except its behavior
97 : * doesn't depend on the user's current locale.
98 : */
99 : template<typename Char>
100 : constexpr bool
101 : IsAsciiDigit(Char aChar)
102 : {
103 : using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
104 2063252 : auto uc = static_cast<UnsignedChar>(aChar);
105 2063252 : return '0' <= uc && uc <= '9';
106 : }
107 :
108 : /**
109 : * Returns true iff |aChar| matches [a-zA-Z0-9].
110 : *
111 : * This function is basically what you thought isalnum was, except its behavior
112 : * doesn't depend on the user's current locale.
113 : */
114 : template<typename Char>
115 : constexpr bool
116 : IsAsciiAlphanumeric(Char aChar)
117 : {
118 1163 : return IsAsciiDigit(aChar) || IsAsciiAlpha(aChar);
119 : }
120 :
121 : /**
122 : * Converts an ASCII alphanumeric digit [0-9a-zA-Z] to number as if in base-36.
123 : * (This function therefore works for decimal, hexadecimal, etc.).
124 : */
125 : template<typename Char>
126 : uint8_t
127 0 : AsciiAlphanumericToNumber(Char aChar)
128 : {
129 : using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
130 861 : auto uc = static_cast<UnsignedChar>(aChar);
131 :
132 861 : if ('0' <= uc && uc <= '9') {
133 470 : return uc - '0';
134 : }
135 :
136 0 : if ('A' <= uc && uc <= 'Z') {
137 : return uc - 'A' + 10;
138 : }
139 :
140 : // Ideally this function would be constexpr, but unfortunately gcc at least as
141 : // of 6.4 forbids non-constexpr function calls in unevaluated constexpr
142 : // function calls. See bug 1453456. So for now, just assert and leave the
143 : // entire function non-constexpr.
144 : MOZ_ASSERT('a' <= uc && uc <= 'z',
145 : "non-ASCII alphanumeric character can't be converted to number");
146 : return uc - 'a' + 10;
147 : }
148 :
149 : } // namespace mozilla
150 :
151 : #endif /* mozilla_TextUtils_h */
|