Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/TextUtils.h
Line
Count
Source (jump to first uncovered line)
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
905k
{
45
905k
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
46
905k
  auto uc = static_cast<UnsignedChar>(aChar);
47
905k
  return uc < 0x80;
48
905k
}
Unexecuted instantiation: bool mozilla::IsAscii<unsigned char>(unsigned char)
bool mozilla::IsAscii<int>(int)
Line
Count
Source
44
905k
{
45
905k
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
46
905k
  auto uc = static_cast<UnsignedChar>(aChar);
47
905k
  return uc < 0x80;
48
905k
}
Unexecuted instantiation: bool mozilla::IsAscii<char16_t>(char16_t)
49
50
template<typename Char>
51
constexpr bool
52
IsNonAsciiLatin1(Char aChar)
53
0
{
54
0
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
55
0
  auto uc = static_cast<UnsignedChar>(aChar);
56
0
  return uc >= 0x80 && uc <= 0xFF;
57
0
}
Unexecuted instantiation: bool mozilla::IsNonAsciiLatin1<char16_t>(char16_t)
Unexecuted instantiation: bool mozilla::IsNonAsciiLatin1<char>(char)
58
59
/**
60
 * Returns true iff |aChar| matches Ascii Whitespace.
61
 *
62
 * This function is intended to match the Infra standard
63
 * (https://infra.spec.whatwg.org/#ascii-whitespace)
64
 */
65
template<typename Char>
66
constexpr bool
67
IsAsciiWhitespace(Char aChar)
68
7.82k
{
69
7.82k
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
70
7.82k
  auto uc = static_cast<UnsignedChar>(aChar);
71
7.82k
  return uc == 0x9 || uc == 0xA || uc == 0xC || uc == 0xD || uc == 0x20;
72
7.82k
}
Unexecuted instantiation: bool mozilla::IsAsciiWhitespace<char16_t>(char16_t)
bool mozilla::IsAsciiWhitespace<char>(char)
Line
Count
Source
68
7.82k
{
69
7.82k
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
70
7.82k
  auto uc = static_cast<UnsignedChar>(aChar);
71
7.82k
  return uc == 0x9 || uc == 0xA || uc == 0xC || uc == 0xD || uc == 0x20;
72
7.82k
}
73
74
/**
75
 * Returns true iff |aChar| matches [a-z].
76
 *
77
 * This function is basically what you thought islower was, except its behavior
78
 * doesn't depend on the user's current locale.
79
 */
80
template<typename Char>
81
constexpr bool
82
IsAsciiLowercaseAlpha(Char aChar)
83
37.6M
{
84
37.6M
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
85
37.6M
  auto uc = static_cast<UnsignedChar>(aChar);
86
37.6M
  return 'a' <= uc && uc <= 'z';
87
37.6M
}
bool mozilla::IsAsciiLowercaseAlpha<char>(char)
Line
Count
Source
83
37.6M
{
84
37.6M
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
85
37.6M
  auto uc = static_cast<UnsignedChar>(aChar);
86
37.6M
  return 'a' <= uc && uc <= 'z';
87
37.6M
}
bool mozilla::IsAsciiLowercaseAlpha<char16_t>(char16_t)
Line
Count
Source
83
530
{
84
530
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
85
530
  auto uc = static_cast<UnsignedChar>(aChar);
86
530
  return 'a' <= uc && uc <= 'z';
87
530
}
Unexecuted instantiation: bool mozilla::IsAsciiLowercaseAlpha<int>(int)
Unexecuted instantiation: bool mozilla::IsAsciiLowercaseAlpha<unsigned char>(unsigned char)
88
89
/**
90
 * Returns true iff |aChar| matches [A-Z].
91
 *
92
 * This function is basically what you thought isupper was, except its behavior
93
 * doesn't depend on the user's current locale.
94
 */
95
template<typename Char>
96
constexpr bool
97
IsAsciiUppercaseAlpha(Char aChar)
98
6.36M
{
99
6.36M
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
100
6.36M
  auto uc = static_cast<UnsignedChar>(aChar);
101
6.36M
  return 'A' <= uc && uc <= 'Z';
102
6.36M
}
bool mozilla::IsAsciiUppercaseAlpha<char>(char)
Line
Count
Source
98
6.36M
{
99
6.36M
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
100
6.36M
  auto uc = static_cast<UnsignedChar>(aChar);
101
6.36M
  return 'A' <= uc && uc <= 'Z';
102
6.36M
}
bool mozilla::IsAsciiUppercaseAlpha<char16_t>(char16_t)
Line
Count
Source
98
255
{
99
255
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
100
255
  auto uc = static_cast<UnsignedChar>(aChar);
101
255
  return 'A' <= uc && uc <= 'Z';
102
255
}
Unexecuted instantiation: bool mozilla::IsAsciiUppercaseAlpha<int>(int)
Unexecuted instantiation: bool mozilla::IsAsciiUppercaseAlpha<unsigned char>(unsigned char)
103
104
/**
105
 * Returns true iff |aChar| matches [a-zA-Z].
106
 *
107
 * This function is basically what you thought isalpha was, except its behavior
108
 * doesn't depend on the user's current locale.
109
 */
110
template<typename Char>
111
constexpr bool
112
IsAsciiAlpha(Char aChar)
113
37.6M
{
114
37.6M
  return IsAsciiLowercaseAlpha(aChar) || IsAsciiUppercaseAlpha(aChar);
115
37.6M
}
bool mozilla::IsAsciiAlpha<char>(char)
Line
Count
Source
113
37.6M
{
114
37.6M
  return IsAsciiLowercaseAlpha(aChar) || IsAsciiUppercaseAlpha(aChar);
115
37.6M
}
bool mozilla::IsAsciiAlpha<char16_t>(char16_t)
Line
Count
Source
113
530
{
114
530
  return IsAsciiLowercaseAlpha(aChar) || IsAsciiUppercaseAlpha(aChar);
115
530
}
Unexecuted instantiation: bool mozilla::IsAsciiAlpha<int>(int)
Unexecuted instantiation: bool mozilla::IsAsciiAlpha<unsigned char>(unsigned char)
116
117
/**
118
 * Returns true iff |aChar| matches [0-9].
119
 *
120
 * This function is basically what you thought isdigit was, except its behavior
121
 * doesn't depend on the user's current locale.
122
 */
123
template<typename Char>
124
constexpr bool
125
IsAsciiDigit(Char aChar)
126
9.23M
{
127
9.23M
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
128
9.23M
  auto uc = static_cast<UnsignedChar>(aChar);
129
9.23M
  return '0' <= uc && uc <= '9';
130
9.23M
}
bool mozilla::IsAsciiDigit<char>(char)
Line
Count
Source
126
4.33M
{
127
4.33M
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
128
4.33M
  auto uc = static_cast<UnsignedChar>(aChar);
129
4.33M
  return '0' <= uc && uc <= '9';
130
4.33M
}
bool mozilla::IsAsciiDigit<char16_t>(char16_t)
Line
Count
Source
126
3.25M
{
127
3.25M
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
128
3.25M
  auto uc = static_cast<UnsignedChar>(aChar);
129
3.25M
  return '0' <= uc && uc <= '9';
130
3.25M
}
bool mozilla::IsAsciiDigit<unsigned char>(unsigned char)
Line
Count
Source
126
1.62M
{
127
1.62M
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
128
1.62M
  auto uc = static_cast<UnsignedChar>(aChar);
129
1.62M
  return '0' <= uc && uc <= '9';
130
1.62M
}
bool mozilla::IsAsciiDigit<int>(int)
Line
Count
Source
126
21.8k
{
127
21.8k
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
128
21.8k
  auto uc = static_cast<UnsignedChar>(aChar);
129
21.8k
  return '0' <= uc && uc <= '9';
130
21.8k
}
131
132
/**
133
 * Returns true iff |aChar| matches [0-9a-fA-F].
134
 *
135
 * This function is basically isxdigit, but guaranteed to be only for ASCII.
136
 */
137
template<typename Char>
138
constexpr bool
139
IsAsciiHexDigit(Char aChar)
140
240k
{
141
240k
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
142
240k
  auto uc = static_cast<UnsignedChar>(aChar);
143
240k
  return ('0' <= uc && uc <= '9') ||
144
240k
         ('a' <= uc && uc <= 'f') ||
145
240k
         ('A' <= uc && uc <= 'F');
146
240k
}
bool mozilla::IsAsciiHexDigit<unsigned char>(unsigned char)
Line
Count
Source
140
240k
{
141
240k
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
142
240k
  auto uc = static_cast<UnsignedChar>(aChar);
143
240k
  return ('0' <= uc && uc <= '9') ||
144
240k
         ('a' <= uc && uc <= 'f') ||
145
240k
         ('A' <= uc && uc <= 'F');
146
240k
}
Unexecuted instantiation: bool mozilla::IsAsciiHexDigit<char>(char)
147
148
/**
149
 * Returns true iff |aChar| matches [a-zA-Z0-9].
150
 *
151
 * This function is basically what you thought isalnum was, except its behavior
152
 * doesn't depend on the user's current locale.
153
 */
154
template<typename Char>
155
constexpr bool
156
IsAsciiAlphanumeric(Char aChar)
157
17.2k
{
158
17.2k
  return IsAsciiDigit(aChar) || IsAsciiAlpha(aChar);
159
17.2k
}
bool mozilla::IsAsciiAlphanumeric<char>(char)
Line
Count
Source
157
15.9k
{
158
15.9k
  return IsAsciiDigit(aChar) || IsAsciiAlpha(aChar);
159
15.9k
}
bool mozilla::IsAsciiAlphanumeric<char16_t>(char16_t)
Line
Count
Source
157
1.35k
{
158
1.35k
  return IsAsciiDigit(aChar) || IsAsciiAlpha(aChar);
159
1.35k
}
Unexecuted instantiation: bool mozilla::IsAsciiAlphanumeric<unsigned char>(unsigned char)
160
161
/**
162
 * Converts an ASCII alphanumeric digit [0-9a-zA-Z] to number as if in base-36.
163
 * (This function therefore works for decimal, hexadecimal, etc.).
164
 */
165
template<typename Char>
166
uint8_t
167
AsciiAlphanumericToNumber(Char aChar)
168
1.35k
{
169
1.35k
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
170
1.35k
  auto uc = static_cast<UnsignedChar>(aChar);
171
1.35k
172
1.35k
  if ('0' <= uc && uc <= '9') {
173
822
    return uc - '0';
174
822
  }
175
530
176
530
  if ('A' <= uc && uc <= 'Z') {
177
255
    return uc - 'A' + 10;
178
255
  }
179
275
180
275
  // Ideally this function would be constexpr, but unfortunately gcc at least as
181
275
  // of 6.4 forbids non-constexpr function calls in unevaluated constexpr
182
275
  // function calls.  See bug 1453456.  So for now, just assert and leave the
183
275
  // entire function non-constexpr.
184
275
  MOZ_ASSERT('a' <= uc && uc <= 'z',
185
275
             "non-ASCII alphanumeric character can't be converted to number");
186
275
  return uc - 'a' + 10;
187
275
}
unsigned char mozilla::AsciiAlphanumericToNumber<char16_t>(char16_t)
Line
Count
Source
168
1.35k
{
169
1.35k
  using UnsignedChar = typename detail::MakeUnsignedChar<Char>::Type;
170
1.35k
  auto uc = static_cast<UnsignedChar>(aChar);
171
1.35k
172
1.35k
  if ('0' <= uc && uc <= '9') {
173
822
    return uc - '0';
174
822
  }
175
530
176
530
  if ('A' <= uc && uc <= 'Z') {
177
255
    return uc - 'A' + 10;
178
255
  }
179
275
180
275
  // Ideally this function would be constexpr, but unfortunately gcc at least as
181
275
  // of 6.4 forbids non-constexpr function calls in unevaluated constexpr
182
275
  // function calls.  See bug 1453456.  So for now, just assert and leave the
183
275
  // entire function non-constexpr.
184
275
  MOZ_ASSERT('a' <= uc && uc <= 'z',
185
275
             "non-ASCII alphanumeric character can't be converted to number");
186
275
  return uc - 'a' + 10;
187
275
}
Unexecuted instantiation: unsigned char mozilla::AsciiAlphanumericToNumber<int>(int)
Unexecuted instantiation: unsigned char mozilla::AsciiAlphanumericToNumber<unsigned char>(unsigned char)
188
189
} // namespace mozilla
190
191
#endif /* mozilla_TextUtils_h */