Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:35

0001 //===--- UnicodeCharRanges.h - Types and functions for character ranges ---===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 #ifndef LLVM_SUPPORT_UNICODECHARRANGES_H
0009 #define LLVM_SUPPORT_UNICODECHARRANGES_H
0010 
0011 #include "llvm/ADT/ArrayRef.h"
0012 #include "llvm/Support/Compiler.h"
0013 #include "llvm/Support/Debug.h"
0014 #include "llvm/Support/raw_ostream.h"
0015 #include <algorithm>
0016 
0017 #define DEBUG_TYPE "unicode"
0018 
0019 namespace llvm {
0020 namespace sys {
0021 
0022 /// Represents a closed range of Unicode code points [Lower, Upper].
0023 struct UnicodeCharRange {
0024   uint32_t Lower;
0025   uint32_t Upper;
0026 };
0027 
0028 inline bool operator<(uint32_t Value, UnicodeCharRange Range) {
0029   return Value < Range.Lower;
0030 }
0031 inline bool operator<(UnicodeCharRange Range, uint32_t Value) {
0032   return Range.Upper < Value;
0033 }
0034 
0035 /// Holds a reference to an ordered array of UnicodeCharRange and allows
0036 /// to quickly check if a code point is contained in the set represented by this
0037 /// array.
0038 class UnicodeCharSet {
0039 public:
0040   typedef ArrayRef<UnicodeCharRange> CharRanges;
0041 
0042   /// Constructs a UnicodeCharSet instance from an array of
0043   /// UnicodeCharRanges.
0044   ///
0045   /// Array pointed by \p Ranges should have the lifetime at least as long as
0046   /// the UnicodeCharSet instance, and should not change. Array is validated by
0047   /// the constructor, so it makes sense to create as few UnicodeCharSet
0048   /// instances per each array of ranges, as possible.
0049 #ifdef NDEBUG
0050 
0051   // FIXME: This could use constexpr + static_assert. This way we
0052   // may get rid of NDEBUG in this header. Unfortunately there are some
0053   // problems to get this working with MSVC 2013. Change this when
0054   // the support for MSVC 2013 is dropped.
0055   constexpr UnicodeCharSet(CharRanges Ranges) : Ranges(Ranges) {}
0056 #else
0057   UnicodeCharSet(CharRanges Ranges) : Ranges(Ranges) {
0058     assert(rangesAreValid());
0059   }
0060 #endif
0061 
0062   /// Returns true if the character set contains the Unicode code point
0063   /// \p C.
0064   bool contains(uint32_t C) const {
0065     return std::binary_search(Ranges.begin(), Ranges.end(), C);
0066   }
0067 
0068 private:
0069   /// Returns true if each of the ranges is a proper closed range
0070   /// [min, max], and if the ranges themselves are ordered and non-overlapping.
0071   bool rangesAreValid() const {
0072     uint32_t Prev = 0;
0073     for (CharRanges::const_iterator I = Ranges.begin(), E = Ranges.end();
0074          I != E; ++I) {
0075       if (I != Ranges.begin() && Prev >= I->Lower) {
0076         LLVM_DEBUG(dbgs() << "Upper bound 0x");
0077         LLVM_DEBUG(dbgs().write_hex(Prev));
0078         LLVM_DEBUG(dbgs() << " should be less than succeeding lower bound 0x");
0079         LLVM_DEBUG(dbgs().write_hex(I->Lower) << "\n");
0080         return false;
0081       }
0082       if (I->Upper < I->Lower) {
0083         LLVM_DEBUG(dbgs() << "Upper bound 0x");
0084         LLVM_DEBUG(dbgs().write_hex(I->Lower));
0085         LLVM_DEBUG(dbgs() << " should not be less than lower bound 0x");
0086         LLVM_DEBUG(dbgs().write_hex(I->Upper) << "\n");
0087         return false;
0088       }
0089       Prev = I->Upper;
0090     }
0091 
0092     return true;
0093   }
0094 
0095   const CharRanges Ranges;
0096 };
0097 
0098 } // namespace sys
0099 } // namespace llvm
0100 
0101 #undef DEBUG_TYPE // "unicode"
0102 
0103 #endif // LLVM_SUPPORT_UNICODECHARRANGES_H