1 //===--- UnicodeCharRanges.h - Types and functions for character ranges ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_SUPPORT_UNICODECHARRANGES_H
10 #define LLVM_SUPPORT_UNICODECHARRANGES_H
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/SmallPtrSet.h"
14 #include "llvm/Support/Compiler.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/Mutex.h"
17 #include "llvm/Support/MutexGuard.h"
18 #include "llvm/Support/raw_ostream.h"
24 /// \brief Represents a closed range of Unicode code points [Lower, Upper].
25 struct UnicodeCharRange {
30 inline bool operator<(uint32_t Value, UnicodeCharRange Range) {
31 return Value < Range.Lower;
33 inline bool operator<(UnicodeCharRange Range, uint32_t Value) {
34 return Range.Upper < Value;
37 /// \brief Holds a reference to an ordered array of UnicodeCharRange and allows
38 /// to quickly check if a code point is contained in the set represented by this
40 class UnicodeCharSet {
42 typedef llvm::ArrayRef<UnicodeCharRange> CharRanges;
44 /// \brief Constructs a UnicodeCharSet instance from an array of
45 /// UnicodeCharRanges.
47 /// Array pointed by \p Ranges should have the lifetime at least as long as
48 /// the UnicodeCharSet instance, and should not change. Array is validated by
49 /// the constructor, so it makes sense to create as few UnicodeCharSet
50 /// instances per each array of ranges, as possible.
51 UnicodeCharSet(CharRanges Ranges) : Ranges(Ranges) {
52 assert(rangesAreValid());
55 /// \brief Returns true if the character set contains the Unicode code point
57 bool contains(uint32_t C) const {
58 return std::binary_search(Ranges.begin(), Ranges.end(), C);
62 /// \brief Returns true if each of the ranges is a proper closed range
63 /// [min, max], and if the ranges themselves are ordered and non-overlapping.
64 bool rangesAreValid() const {
66 for (CharRanges::const_iterator I = Ranges.begin(), E = Ranges.end();
68 if (I != Ranges.begin() && Prev >= I->Lower) {
69 DEBUG(llvm::dbgs() << "Upper bound 0x");
70 DEBUG(llvm::dbgs().write_hex(Prev));
71 DEBUG(llvm::dbgs() << " should be less than succeeding lower bound 0x");
72 DEBUG(llvm::dbgs().write_hex(I->Lower) << "\n");
75 if (I->Upper < I->Lower) {
76 DEBUG(llvm::dbgs() << "Upper bound 0x");
77 DEBUG(llvm::dbgs().write_hex(I->Lower));
78 DEBUG(llvm::dbgs() << " should not be less than lower bound 0x");
79 DEBUG(llvm::dbgs().write_hex(I->Upper) << "\n");
88 const CharRanges Ranges;
95 #endif // LLVM_SUPPORT_UNICODECHARRANGES_H