unicode.h 8.45 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_STRINGS_UNICODE_H_
#define V8_STRINGS_UNICODE_H_
7 8

#include <sys/types.h>
9
#include "src/base/bit-field.h"
10
#include "src/common/globals.h"
11
#include "src/third_party/utf8-decoder/utf8-decoder.h"
12 13 14 15 16 17 18
/**
 * \file
 * Definitions and convenience functions for working with unicode.
 */

namespace unibrow {

19 20
using uchar = unsigned int;
using byte = unsigned char;
21 22 23 24 25

/**
 * The max length of the result of converting the case of a single
 * character.
 */
26
const int kMaxMappingSize = 4;
27

28
#ifndef V8_INTL_SUPPORT
29 30 31
template <class T, int size = 256>
class Predicate {
 public:
32
  inline Predicate() = default;
33
  inline bool get(uchar c);
34

35 36 37
 private:
  friend class Test;
  bool CalculateValue(uchar c);
38 39 40 41
  class CacheEntry {
   public:
    inline CacheEntry()
        : bit_field_(CodePointField::encode(0) | ValueField::encode(0)) {}
42
    inline CacheEntry(uchar code_point, bool value)
43 44 45 46 47 48
        : bit_field_(
              CodePointField::encode(CodePointField::kMask & code_point) |
              ValueField::encode(value)) {
      DCHECK_IMPLIES((CodePointField::kMask & code_point) != code_point,
                     code_point == static_cast<uchar>(-1));
    }
49 50 51 52 53

    uchar code_point() const { return CodePointField::decode(bit_field_); }
    bool value() const { return ValueField::decode(bit_field_); }

   private:
54 55
    using CodePointField = v8::base::BitField<uchar, 0, 21>;
    using ValueField = v8::base::BitField<bool, 21, 1>;
56 57

    uint32_t bit_field_;
58 59 60 61 62 63 64 65 66 67 68 69 70
  };
  static const int kSize = size;
  static const int kMask = kSize - 1;
  CacheEntry entries_[kSize];
};

// A cache used in case conversion.  It caches the value for characters
// that either have no mapping or map to a single character independent
// of context.  Characters that map to more than one character or that
// map differently depending on context are always looked up.
template <class T, int size = 256>
class Mapping {
 public:
71
  inline Mapping() = default;
72
  inline int get(uchar c, uchar n, uchar* result);
73

74 75 76 77
 private:
  friend class Test;
  int CalculateValue(uchar c, uchar n, uchar* result);
  struct CacheEntry {
78
    inline CacheEntry() : code_point_(kNoChar), offset_(0) {}
79
    inline CacheEntry(uchar code_point, signed offset)
80
        : code_point_(code_point), offset_(offset) {}
81 82 83
    uchar code_point_;
    signed offset_;
    static const int kNoChar = (1 << 21) - 1;
84 85 86 87 88 89 90 91 92 93
  };
  static const int kSize = size;
  static const int kMask = kSize - 1;
  CacheEntry entries_[kSize];
};

class UnicodeData {
 private:
  friend class Test;
  static int GetByteCount();
94
  static const uchar kMaxCodePoint;
95 96
};

97
#endif  // !V8_INTL_SUPPORT
98

99 100
class Utf16 {
 public:
101
  static const int kNoPreviousCharacter = -1;
102 103 104
  static inline bool IsSurrogatePair(int lead, int trail) {
    return IsLeadSurrogate(lead) && IsTrailSurrogate(trail);
  }
105
  static inline bool IsLeadSurrogate(int code) {
106 107
    return (code & 0xfc00) == 0xd800;
  }
108
  static inline bool IsTrailSurrogate(int code) {
109 110 111
    return (code & 0xfc00) == 0xdc00;
  }

112
  static inline int CombineSurrogatePair(uchar lead, uchar trail) {
113 114 115 116 117 118 119 120 121 122 123 124
    return 0x10000 + ((lead & 0x3ff) << 10) + (trail & 0x3ff);
  }
  static const uchar kMaxNonSurrogateCharCode = 0xffff;
  // Encoding a single UTF-16 code unit will produce 1, 2 or 3 bytes
  // of UTF-8 data.  The special case where the unit is a surrogate
  // trail produces 1 byte net, because the encoding of the pair is
  // 4 bytes and the 3 bytes that were used to encode the lead surrogate
  // can be reclaimed.
  static const int kMaxExtraUtf8BytesForOneUtf16CodeUnit = 3;
  // One UTF-16 surrogate is endoded (illegally) as 3 UTF-8 bytes.
  // The illegality stems from the surrogate not being part of a pair.
  static const int kUtf8BytesToCodeASurrogate = 3;
125
  static inline uint16_t LeadSurrogate(uint32_t char_code) {
126 127
    return 0xd800 + (((char_code - 0x10000) >> 10) & 0x3ff);
  }
128
  static inline uint16_t TrailSurrogate(uint32_t char_code) {
129 130
    return 0xdc00 + (char_code & 0x3ff);
  }
131 132
  static inline bool HasUnpairedSurrogate(const uint16_t* code_units,
                                          size_t length);
133 134
};

135 136
class Latin1 {
 public:
137
  static const uint16_t kMaxChar = 0xff;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  // Convert the character to Latin-1 case equivalent if possible.
  static inline uint16_t TryConvertToLatin1(uint16_t c) {
    switch (c) {
      // This are equivalent characters in unicode.
      case 0x39c:
      case 0x3bc:
        return 0xb5;
      // This is an uppercase of a Latin-1 character
      // outside of Latin-1.
      case 0x178:
        return 0xff;
    }
    return c;
  }
};

154
class V8_EXPORT_PRIVATE Utf8 {
155
 public:
156 157
  using State = Utf8DfaDecoder::State;

158
  static inline uchar Length(uchar chr, int previous);
159
  static inline unsigned EncodeOneByte(char* out, uint8_t c);
160
  static inline unsigned Encode(char* out, uchar c, int previous,
161
                                bool replace_invalid = false);
162
  static uchar CalculateValue(const byte* str, size_t length, size_t* cursor);
163 164 165

  // The unicode replacement character, used to signal invalid unicode
  // sequences (e.g. an orphan surrogate) when converting to a UTF-8 encoding.
166
  static const uchar kBadChar = 0xFFFD;
167 168
  static const uchar kBufferEmpty = 0x0;
  static const uchar kIncomplete = 0xFFFFFFFC;  // any non-valid code point.
169 170 171
  static const unsigned kMaxEncodedSize = 4;
  static const unsigned kMaxOneByteChar = 0x7f;
  static const unsigned kMaxTwoByteChar = 0x7ff;
172
  static const unsigned kMaxThreeByteChar = 0xffff;
173
  static const unsigned kMaxFourByteChar = 0x1fffff;
174

175 176 177 178
  // A single surrogate is coded as a 3 byte UTF-8 sequence, but two together
  // that match are coded as a 4 byte UTF-8 sequence.
  static const unsigned kBytesSavedByCombiningSurrogates = 2;
  static const unsigned kSizeOfUnmatchedSurrogate = 3;
179 180
  // The maximum size a single UTF-16 code unit may take up when encoded as
  // UTF-8.
181
  static const unsigned kMax16BitCodeUnitSize = 3;
182
  static inline uchar ValueOf(const byte* str, size_t length, size_t* cursor);
clemensh's avatar
clemensh committed
183

184
  using Utf8IncrementalBuffer = uint32_t;
185 186
  static inline uchar ValueOfIncremental(const byte** cursor, State* state,
                                         Utf8IncrementalBuffer* buffer);
187
  static uchar ValueOfIncrementalFinish(State* state);
188

clemensh's avatar
clemensh committed
189 190 191
  // Excludes non-characters from the set of valid code points.
  static inline bool IsValidCharacter(uchar c);

192 193 194 195 196 197 198 199 200
  // Validate if the input has a valid utf-8 encoding. Unlike JS source code
  // this validation function will accept any unicode code point, including
  // kBadChar and BOMs.
  //
  // This method checks for:
  // - valid utf-8 endcoding (e.g. no over-long encodings),
  // - absence of surrogates,
  // - valid code point range.
  static bool ValidateEncoding(const byte* str, size_t length);
201 202 203 204 205 206 207 208
};

struct Uppercase {
  static bool Is(uchar c);
};
struct Letter {
  static bool Is(uchar c);
};
209
#ifndef V8_INTL_SUPPORT
210
struct V8_EXPORT_PRIVATE ID_Start {
211 212
  static bool Is(uchar c);
};
213
struct V8_EXPORT_PRIVATE ID_Continue {
214 215
  static bool Is(uchar c);
};
216
struct V8_EXPORT_PRIVATE WhiteSpace {
217 218
  static bool Is(uchar c);
};
219
#endif  // !V8_INTL_SUPPORT
220 221 222 223 224

// LineTerminator:       'JS_Line_Terminator' in point.properties
// ES#sec-line-terminators lists exactly 4 code points:
// LF (U+000A), CR (U+000D), LS(U+2028), PS(U+2029)
V8_INLINE bool IsLineTerminator(uchar c) {
225
  return c == 0x000A || c == 0x000D || c == 0x2028 || c == 0x2029;
226 227
}

228 229 230 231
V8_INLINE bool IsStringLiteralLineTerminator(uchar c) {
  return c == 0x000A || c == 0x000D;
}

232
#ifndef V8_INTL_SUPPORT
lybvinci's avatar
lybvinci committed
233
struct V8_EXPORT_PRIVATE ToLowercase {
234
  static const int kMaxWidth = 3;
235
  static const bool kIsToLower = true;
236
  static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr);
237
};
lybvinci's avatar
lybvinci committed
238
struct V8_EXPORT_PRIVATE ToUppercase {
239
  static const int kMaxWidth = 3;
240
  static const bool kIsToLower = false;
241
  static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr);
242
};
243
struct V8_EXPORT_PRIVATE Ecma262Canonicalize {
244
  static const int kMaxWidth = 1;
245
  static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr);
246
};
247
struct V8_EXPORT_PRIVATE Ecma262UnCanonicalize {
248
  static const int kMaxWidth = 4;
249
  static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr);
250
};
251
struct V8_EXPORT_PRIVATE CanonicalizationRange {
252
  static const int kMaxWidth = 1;
253
  static int Convert(uchar c, uchar n, uchar* result, bool* allow_caching_ptr);
254
};
255
#endif  // !V8_INTL_SUPPORT
256 257 258

}  // namespace unibrow

259
#endif  // V8_STRINGS_UNICODE_H_