summaryrefslogtreecommitdiffstats
path: root/mfbt/TextUtils.h
blob: f09e8d56c23f33a4eda3ba8bcfc33bbfdc325a95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* -*- 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/Assertions.h"
#include "mozilla/Latin1.h"

#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

namespace mozilla {

// 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.

// 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). */
inline constexpr bool IsAscii(unsigned char aChar) { return aChar < 0x80; }

/** Returns true iff |aChar| is ASCII, i.e. in the range [0, 0x80). */
inline constexpr bool IsAscii(signed char aChar) {
  return IsAscii(static_cast<unsigned char>(aChar));
}

/** Returns true iff |aChar| is ASCII, i.e. in the range [0, 0x80). */
inline constexpr bool IsAscii(char aChar) {
  return IsAscii(static_cast<unsigned char>(aChar));
}

/** Returns true iff |aChar| is ASCII, i.e. in the range [0, 0x80). */
inline constexpr bool IsAscii(char16_t aChar) { return aChar < 0x80; }

/** Returns true iff |aChar| is ASCII, i.e. in the range [0, 0x80). */
inline constexpr bool IsAscii(char32_t aChar) { return aChar < 0x80; }

/**
 * 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.
  if (length < mozilla::detail::kShortStringLimitForInlinePaths) {
    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()
  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);
#else
  for (char16_t c : aString) {
    if (!IsAscii(c)) {
      return false;
    }
  }
  return true;
#endif
}

/**
 * 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;
}

#if MOZ_HAS_JSRUST()
/**
 * 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());
}

/**
 * Returns the index of the first unpaired surrogate or
 * the length of the string if there are none.
 */
inline size_t Utf16ValidUpTo(mozilla::Span<const char16_t> aString) {
  return encoding_mem_utf16_valid_up_to(aString.Elements(), aString.Length());
}

/**
 * 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

/**
 * 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;
}

/**
 * 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';
}

/**
 * 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) {
  return IsAsciiLowercaseAlpha(aChar) || IsAsciiUppercaseAlpha(aChar);
}

/**
 * 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';
}

/**
 * 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');
}

/**
 * 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) {
  return IsAsciiDigit(aChar) || IsAsciiAlpha(aChar);
}

/**
 * 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;
}

}  // namespace mozilla

#endif /* mozilla_TextUtils_h */