Re-landing "Refactoring cl::list_storage from "is a" to "has a" std::vector."
[oota-llvm.git] / include / llvm / Support / UnicodeCharRanges.h
1 //===--- UnicodeCharRanges.h - Types and functions for character ranges ---===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_SUPPORT_UNICODECHARRANGES_H
10 #define LLVM_SUPPORT_UNICODECHARRANGES_H
11
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"
19 #include <algorithm>
20
21 namespace llvm {
22 namespace sys {
23
24 #define DEBUG_TYPE "unicode"
25
26 /// \brief Represents a closed range of Unicode code points [Lower, Upper].
27 struct UnicodeCharRange {
28   uint32_t Lower;
29   uint32_t Upper;
30 };
31
32 inline bool operator<(uint32_t Value, UnicodeCharRange Range) {
33   return Value < Range.Lower;
34 }
35 inline bool operator<(UnicodeCharRange Range, uint32_t Value) {
36   return Range.Upper < Value;
37 }
38
39 /// \brief Holds a reference to an ordered array of UnicodeCharRange and allows
40 /// to quickly check if a code point is contained in the set represented by this
41 /// array.
42 class UnicodeCharSet {
43 public:
44   typedef ArrayRef<UnicodeCharRange> CharRanges;
45
46   /// \brief Constructs a UnicodeCharSet instance from an array of
47   /// UnicodeCharRanges.
48   ///
49   /// Array pointed by \p Ranges should have the lifetime at least as long as
50   /// the UnicodeCharSet instance, and should not change. Array is validated by
51   /// the constructor, so it makes sense to create as few UnicodeCharSet
52   /// instances per each array of ranges, as possible.
53 #ifdef NDEBUG
54   LLVM_CONSTEXPR UnicodeCharSet(CharRanges Ranges) : Ranges(Ranges) {}
55 #else
56   UnicodeCharSet(CharRanges Ranges) : Ranges(Ranges) {
57     assert(rangesAreValid());
58   }
59 #endif
60
61   /// \brief Returns true if the character set contains the Unicode code point
62   /// \p C.
63   bool contains(uint32_t C) const {
64     return std::binary_search(Ranges.begin(), Ranges.end(), C);
65   }
66
67 private:
68   /// \brief Returns true if each of the ranges is a proper closed range
69   /// [min, max], and if the ranges themselves are ordered and non-overlapping.
70   bool rangesAreValid() const {
71     uint32_t Prev = 0;
72     for (CharRanges::const_iterator I = Ranges.begin(), E = Ranges.end();
73          I != E; ++I) {
74       if (I != Ranges.begin() && Prev >= I->Lower) {
75         DEBUG(dbgs() << "Upper bound 0x");
76         DEBUG(dbgs().write_hex(Prev));
77         DEBUG(dbgs() << " should be less than succeeding lower bound 0x");
78         DEBUG(dbgs().write_hex(I->Lower) << "\n");
79         return false;
80       }
81       if (I->Upper < I->Lower) {
82         DEBUG(dbgs() << "Upper bound 0x");
83         DEBUG(dbgs().write_hex(I->Lower));
84         DEBUG(dbgs() << " should not be less than lower bound 0x");
85         DEBUG(dbgs().write_hex(I->Upper) << "\n");
86         return false;
87       }
88       Prev = I->Upper;
89     }
90
91     return true;
92   }
93
94   const CharRanges Ranges;
95 };
96
97 #undef DEBUG_TYPE // "unicode"
98
99 } // namespace sys
100 } // namespace llvm
101
102
103 #endif // LLVM_SUPPORT_UNICODECHARRANGES_H