Bug 1842773 - Part 16: Replace TypedArrayObject with FixedLengthTypedArrayObject...
[gecko.git] / xpcom / string / nsASCIIMask.h
blob54f51d8957c67ecb166b841476e8f823ca164aad
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/. */
7 #ifndef nsASCIIMask_h_
8 #define nsASCIIMask_h_
10 #include <array>
11 #include <utility>
13 #include "mozilla/Attributes.h"
15 typedef std::array<bool, 128> ASCIIMaskArray;
17 namespace mozilla {
19 // Boolean arrays, fixed size and filled in at compile time, meant to
20 // record something about each of the (standard) ASCII characters.
21 // No extended ASCII for now, there has been no use case.
22 // If you have loops that go through a string character by character
23 // and test for equality to a certain set of characters before deciding
24 // on a course of action, chances are building up one of these arrays
25 // and using it is going to be faster, especially if the set of
26 // characters is more than one long, and known at compile time.
27 class ASCIIMask {
28 public:
29 // Preset masks for some common character groups
30 // When testing, you must check if the index is < 128 or use IsMasked()
32 // if (someChar < 128 && MaskCRLF()[someChar]) this is \r or \n
34 static const ASCIIMaskArray& MaskCRLF();
35 static const ASCIIMaskArray& Mask0to9();
36 static const ASCIIMaskArray& MaskCRLFTab();
37 static const ASCIIMaskArray& MaskWhitespace();
39 static MOZ_ALWAYS_INLINE bool IsMasked(const ASCIIMaskArray& aMask,
40 uint32_t aChar) {
41 return aChar < 128 && aMask[aChar];
45 // Outside of the preset ones, use these templates to create more masks.
47 // The example creation will look like this:
49 // constexpr bool TestABC(char c) { return c == 'A' || c == 'B' || c == 'C'; }
50 // constexpr std::array<bool, 128> sABCMask = CreateASCIIMask(TestABC);
51 // ...
52 // if (someChar < 128 && sABCMask[someChar]) this is A or B or C
54 namespace asciimask_details {
55 template <typename F, size_t... Indices>
56 constexpr std::array<bool, 128> CreateASCIIMask(
57 F fun, std::index_sequence<Indices...>) {
58 return {{fun(Indices)...}};
60 } // namespace asciimask_details
62 template <typename F>
63 constexpr std::array<bool, 128> CreateASCIIMask(F fun) {
64 return asciimask_details::CreateASCIIMask(fun,
65 std::make_index_sequence<128>{});
68 } // namespace mozilla
70 #endif // nsASCIIMask_h_