blob: 4b59f8a92b769b6207c1076b270620a6360f82a1 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===--- UnicodeCharRanges.h - Types and functions for character ranges ---===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8#ifndef LLVM_SUPPORT_UNICODECHARRANGES_H
9#define LLVM_SUPPORT_UNICODECHARRANGES_H
10
11#include "llvm/ADT/ArrayRef.h"
12#include "llvm/ADT/SmallPtrSet.h"
13#include "llvm/Support/Compiler.h"
14#include "llvm/Support/Debug.h"
15#include "llvm/Support/Mutex.h"
16#include "llvm/Support/MutexGuard.h"
17#include "llvm/Support/raw_ostream.h"
18#include <algorithm>
19
20#define DEBUG_TYPE "unicode"
21
22namespace llvm {
23namespace sys {
24
Andrew Scullcdfcccc2018-10-05 20:58:37 +010025/// Represents a closed range of Unicode code points [Lower, Upper].
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010026struct UnicodeCharRange {
27 uint32_t Lower;
28 uint32_t Upper;
29};
30
31inline bool operator<(uint32_t Value, UnicodeCharRange Range) {
32 return Value < Range.Lower;
33}
34inline bool operator<(UnicodeCharRange Range, uint32_t Value) {
35 return Range.Upper < Value;
36}
37
Andrew Scullcdfcccc2018-10-05 20:58:37 +010038/// Holds a reference to an ordered array of UnicodeCharRange and allows
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010039/// to quickly check if a code point is contained in the set represented by this
40/// array.
41class UnicodeCharSet {
42public:
43 typedef ArrayRef<UnicodeCharRange> CharRanges;
44
Andrew Scullcdfcccc2018-10-05 20:58:37 +010045 /// Constructs a UnicodeCharSet instance from an array of
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010046 /// UnicodeCharRanges.
47 ///
48 /// Array pointed by \p Ranges should have the lifetime at least as long as
49 /// the UnicodeCharSet instance, and should not change. Array is validated by
50 /// the constructor, so it makes sense to create as few UnicodeCharSet
51 /// instances per each array of ranges, as possible.
52#ifdef NDEBUG
53
54 // FIXME: This could use constexpr + static_assert. This way we
55 // may get rid of NDEBUG in this header. Unfortunately there are some
56 // problems to get this working with MSVC 2013. Change this when
57 // the support for MSVC 2013 is dropped.
58 constexpr UnicodeCharSet(CharRanges Ranges) : Ranges(Ranges) {}
59#else
60 UnicodeCharSet(CharRanges Ranges) : Ranges(Ranges) {
61 assert(rangesAreValid());
62 }
63#endif
64
Andrew Scullcdfcccc2018-10-05 20:58:37 +010065 /// Returns true if the character set contains the Unicode code point
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010066 /// \p C.
67 bool contains(uint32_t C) const {
68 return std::binary_search(Ranges.begin(), Ranges.end(), C);
69 }
70
71private:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010072 /// Returns true if each of the ranges is a proper closed range
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010073 /// [min, max], and if the ranges themselves are ordered and non-overlapping.
74 bool rangesAreValid() const {
75 uint32_t Prev = 0;
76 for (CharRanges::const_iterator I = Ranges.begin(), E = Ranges.end();
77 I != E; ++I) {
78 if (I != Ranges.begin() && Prev >= I->Lower) {
Andrew Scullcdfcccc2018-10-05 20:58:37 +010079 LLVM_DEBUG(dbgs() << "Upper bound 0x");
80 LLVM_DEBUG(dbgs().write_hex(Prev));
81 LLVM_DEBUG(dbgs() << " should be less than succeeding lower bound 0x");
82 LLVM_DEBUG(dbgs().write_hex(I->Lower) << "\n");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010083 return false;
84 }
85 if (I->Upper < I->Lower) {
Andrew Scullcdfcccc2018-10-05 20:58:37 +010086 LLVM_DEBUG(dbgs() << "Upper bound 0x");
87 LLVM_DEBUG(dbgs().write_hex(I->Lower));
88 LLVM_DEBUG(dbgs() << " should not be less than lower bound 0x");
89 LLVM_DEBUG(dbgs().write_hex(I->Upper) << "\n");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010090 return false;
91 }
92 Prev = I->Upper;
93 }
94
95 return true;
96 }
97
98 const CharRanges Ranges;
99};
100
101} // namespace sys
102} // namespace llvm
103
104#undef DEBUG_TYPE // "unicode"
105
106#endif // LLVM_SUPPORT_UNICODECHARRANGES_H