Files
basilisk55/xpcom/string/nsASCIIMask.h
T
roytam1 2bd3a3c9ef partly import changes from tenfourfox:
- #565: implement nsASCIIMask from M1358297 (41b1fc937)
- #565: M1358297 parts 3 and 4 (3fd15a87a)
- #632: M1424915 M1354233 M1324114 M1343008 M1236277(just backbugs) M1328955 (d87db7e16)
- #632: M1362498 M1397686 M136178 M1320252 M1355875 (82cb3b59e)
- #632: M241788 M1271955 M1249352(p1) + additional local optimizations (438bdb726)
- #632: M1249352 part 2 (2c61821e4)
- #632: M1249352(remaining) M1358297(backbugs) M1369317(pp1,3,4) M1426996 (1eab6170b)
2021-01-25 17:28:58 +08:00

71 lines
2.2 KiB
C++

/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* 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/. */
#ifndef nsASCIIMask_h_
#define nsASCIIMask_h_
#include <array>
#include "mozilla/IndexSequence.h"
typedef std::array<bool, 128> ASCIIMaskArray;
namespace mozilla {
// Boolean arrays, fixed size and filled in at compile time, meant to
// record something about each of the (standard) ASCII characters.
// No extended ASCII for now, there has been no use case.
// If you have loops that go through a string character by character
// and test for equality to a certain set of characters before deciding
// on a course of action, chances are building up one of these arrays
// and using it is going to be faster, especially if the set of
// characters is more than one long, and known at compile time.
class ASCIIMask
{
public:
// Preset masks for some common character groups
// When testing, you must check if the index is < 128 or use IsMasked()
//
// if (someChar < 128 && MaskCRLF()[someChar]) this is \r or \n
static const ASCIIMaskArray& MaskCRLF();
static const ASCIIMaskArray& Mask0to9();
static const ASCIIMaskArray& MaskCRLFTab();
static const ASCIIMaskArray& MaskWhitespace();
static MOZ_ALWAYS_INLINE bool IsMasked(const ASCIIMaskArray& aMask, uint32_t aChar)
{
return aChar < 128 && aMask[aChar];
}
};
// Outside of the preset ones, use these templates to create more masks.
//
// The example creation will look like this:
//
// constexpr bool TestABC(char c) { return c == 'A' || c == 'B' || c == 'C'; }
// constexpr std::array<bool, 128> sABCMask = CreateASCIIMask(TestABC);
// ...
// if (someChar < 128 && sABCMask[someChar]) this is A or B or C
namespace details
{
template<typename F, size_t... Indices>
constexpr std::array<bool, 128> CreateASCIIMask(F fun, mozilla::IndexSequence<Indices...>)
{
return {{ fun(Indices)... }};
}
} // namespace details
template<typename F>
constexpr std::array<bool, 128> CreateASCIIMask(F fun)
{
return details::CreateASCIIMask(fun, mozilla::MakeIndexSequence<128>::Type{});
}
} // namespace mozilla
#endif // nsASCIIMask_h_