LLVM 22.0.0git
UnicodeCharRanges.h
Go to the documentation of this file.
1//===--- UnicodeCharRanges.h - Types and functions for character ranges ---===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8#ifndef LLVM_SUPPORT_UNICODECHARRANGES_H
9#define LLVM_SUPPORT_UNICODECHARRANGES_H
10
11#include "llvm/ADT/ArrayRef.h"
13#include "llvm/Support/Debug.h"
15
16#define DEBUG_TYPE "unicode"
17
18namespace llvm {
19namespace sys {
20
21/// Represents a closed range of Unicode code points [Lower, Upper].
26
28 return Value < Range.Lower;
29}
31 return Range.Upper < Value;
32}
33
34/// Holds a reference to an ordered array of UnicodeCharRange and allows
35/// to quickly check if a code point is contained in the set represented by this
36/// array.
38public:
40
41 /// Constructs a UnicodeCharSet instance from an array of
42 /// UnicodeCharRanges.
43 ///
44 /// Array pointed by \p Ranges should have the lifetime at least as long as
45 /// the UnicodeCharSet instance, and should not change. Array is validated by
46 /// the constructor, so it makes sense to create as few UnicodeCharSet
47 /// instances per each array of ranges, as possible.
48#ifdef NDEBUG
49
50 // FIXME: This could use constexpr + static_assert. This way we
51 // may get rid of NDEBUG in this header. Unfortunately there are some
52 // problems to get this working with MSVC 2013. Change this when
53 // the support for MSVC 2013 is dropped.
54 constexpr UnicodeCharSet(CharRanges Ranges) : Ranges(Ranges) {}
55#else
56 UnicodeCharSet(CharRanges Ranges) : Ranges(Ranges) {
57 assert(rangesAreValid());
58 }
59#endif
60
61 /// Returns true if the character set contains the Unicode code point
62 /// \p C.
63 bool contains(uint32_t C) const { return llvm::binary_search(Ranges, C); }
64
65private:
66 /// Returns true if each of the ranges is a proper closed range
67 /// [min, max], and if the ranges themselves are ordered and non-overlapping.
68 bool rangesAreValid() const {
69 uint32_t Prev = 0;
70 for (CharRanges::const_iterator I = Ranges.begin(), E = Ranges.end();
71 I != E; ++I) {
72 if (I != Ranges.begin() && Prev >= I->Lower) {
73 LLVM_DEBUG(dbgs() << "Upper bound 0x");
74 LLVM_DEBUG(dbgs().write_hex(Prev));
75 LLVM_DEBUG(dbgs() << " should be less than succeeding lower bound 0x");
76 LLVM_DEBUG(dbgs().write_hex(I->Lower) << "\n");
77 return false;
78 }
79 if (I->Upper < I->Lower) {
80 LLVM_DEBUG(dbgs() << "Upper bound 0x");
81 LLVM_DEBUG(dbgs().write_hex(I->Lower));
82 LLVM_DEBUG(dbgs() << " should not be less than lower bound 0x");
83 LLVM_DEBUG(dbgs().write_hex(I->Upper) << "\n");
84 return false;
85 }
86 Prev = I->Upper;
87 }
88
89 return true;
90 }
91
92 const CharRanges Ranges;
93};
94
95} // namespace sys
96} // namespace llvm
97
98#undef DEBUG_TYPE // "unicode"
99
100#endif // LLVM_SUPPORT_UNICODECHARRANGES_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define I(x, y, z)
Definition MD5.cpp:57
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define LLVM_DEBUG(...)
Definition Debug.h:114
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:131
iterator begin() const
Definition ArrayRef.h:130
LLVM Value Representation.
Definition Value.h:75
bool contains(uint32_t C) const
Returns true if the character set contains the Unicode code point C.
UnicodeCharSet(CharRanges Ranges)
Constructs a UnicodeCharSet instance from an array of UnicodeCharRanges.
ArrayRef< UnicodeCharRange > CharRanges
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
bool operator<(uint32_t Value, UnicodeCharRange Range)
This is an optimization pass for GlobalISel generic memory operations.
auto binary_search(R &&Range, T &&Value)
Provide wrappers to std::binary_search which take ranges instead of having to pass begin/end explicit...
Definition STLExtras.h:1981
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void write_hex(raw_ostream &S, uint64_t N, HexPrintStyle Style, std::optional< size_t > Width=std::nullopt)
Represents a closed range of Unicode code points [Lower, Upper].