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#include <algorithm>
16
17#define DEBUG_TYPE "unicode"
18
19namespace llvm {
20namespace sys {
21
22/// Represents a closed range of Unicode code points [Lower, Upper].
27
29 return Value < Range.Lower;
30}
32 return Range.Upper < Value;
33}
34
35/// Holds a reference to an ordered array of UnicodeCharRange and allows
36/// to quickly check if a code point is contained in the set represented by this
37/// array.
39public:
41
42 /// Constructs a UnicodeCharSet instance from an array of
43 /// UnicodeCharRanges.
44 ///
45 /// Array pointed by \p Ranges should have the lifetime at least as long as
46 /// the UnicodeCharSet instance, and should not change. Array is validated by
47 /// the constructor, so it makes sense to create as few UnicodeCharSet
48 /// instances per each array of ranges, as possible.
49#ifdef NDEBUG
50
51 // FIXME: This could use constexpr + static_assert. This way we
52 // may get rid of NDEBUG in this header. Unfortunately there are some
53 // problems to get this working with MSVC 2013. Change this when
54 // the support for MSVC 2013 is dropped.
55 constexpr UnicodeCharSet(CharRanges Ranges) : Ranges(Ranges) {}
56#else
57 UnicodeCharSet(CharRanges Ranges) : Ranges(Ranges) {
58 assert(rangesAreValid());
59 }
60#endif
61
62 /// Returns true if the character set contains the Unicode code point
63 /// \p C.
64 bool contains(uint32_t C) const { return llvm::binary_search(Ranges, C); }
65
66private:
67 /// Returns true if each of the ranges is a proper closed range
68 /// [min, max], and if the ranges themselves are ordered and non-overlapping.
69 bool rangesAreValid() const {
70 uint32_t Prev = 0;
71 for (CharRanges::const_iterator I = Ranges.begin(), E = Ranges.end();
72 I != E; ++I) {
73 if (I != Ranges.begin() && Prev >= I->Lower) {
74 LLVM_DEBUG(dbgs() << "Upper bound 0x");
75 LLVM_DEBUG(dbgs().write_hex(Prev));
76 LLVM_DEBUG(dbgs() << " should be less than succeeding lower bound 0x");
77 LLVM_DEBUG(dbgs().write_hex(I->Lower) << "\n");
78 return false;
79 }
80 if (I->Upper < I->Lower) {
81 LLVM_DEBUG(dbgs() << "Upper bound 0x");
82 LLVM_DEBUG(dbgs().write_hex(I->Lower));
83 LLVM_DEBUG(dbgs() << " should not be less than lower bound 0x");
84 LLVM_DEBUG(dbgs().write_hex(I->Upper) << "\n");
85 return false;
86 }
87 Prev = I->Upper;
88 }
89
90 return true;
91 }
92
93 const CharRanges Ranges;
94};
95
96} // namespace sys
97} // namespace llvm
98
99#undef DEBUG_TYPE // "unicode"
100
101#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:58
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:41
iterator end() const
Definition ArrayRef.h:136
iterator begin() const
Definition ArrayRef.h:135
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.
ArrayRef< UnicodeCharRange > CharRanges
UnicodeCharSet(CharRanges Ranges)
Constructs a UnicodeCharSet instance from an array of UnicodeCharRanges.
@ 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:1961
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].