Introduce llvm::sys::path::home_directory.
[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 /// \brief Represents a closed range of Unicode code points [Lower, Upper].
25 struct UnicodeCharRange {
26   uint32_t Lower;
27   uint32_t Upper;
28 };
29
30 inline bool operator<(uint32_t Value, UnicodeCharRange Range) {
31   return Value < Range.Lower;
32 }
33 inline bool operator<(UnicodeCharRange Range, uint32_t Value) {
34   return Range.Upper < Value;
35 }
36
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
39 /// array.
40 class UnicodeCharSet {
41 public:
42   typedef llvm::ArrayRef<UnicodeCharRange> CharRanges;
43
44   /// \brief Constructs a UnicodeCharSet instance from an array of
45   /// UnicodeCharRanges.
46   ///
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());
53   }
54
55   /// \brief Returns true if the character set contains the Unicode code point
56   /// \p C.
57   bool contains(uint32_t C) const {
58     return std::binary_search(Ranges.begin(), Ranges.end(), C);
59   }
60
61 private:
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 {
65     uint32_t Prev = 0;
66     for (CharRanges::const_iterator I = Ranges.begin(), E = Ranges.end();
67          I != E; ++I) {
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");
73         return false;
74       }
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");
80         return false;
81       }
82       Prev = I->Upper;
83     }
84
85     return true;
86   }
87
88   const CharRanges Ranges;
89 };
90
91 } // namespace sys
92 } // namespace llvm
93
94
95 #endif // LLVM_SUPPORT_UNICODECHARRANGES_H