unicode.h 6.64 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_UNICODE_H_
#define V8_UNICODE_H_
7 8

#include <sys/types.h>
9
#include "src/globals.h"
10
#include "src/utils.h"
11 12 13 14 15 16 17
/**
 * \file
 * Definitions and convenience functions for working with unicode.
 */

namespace unibrow {

18 19
typedef unsigned int uchar;
typedef unsigned char byte;
20 21 22 23 24

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

template <class T, int size = 256>
class Predicate {
 public:
  inline Predicate() { }
  inline bool get(uchar c);
32

33 34 35
 private:
  friend class Test;
  bool CalculateValue(uchar c);
36 37 38 39
  class CacheEntry {
   public:
    inline CacheEntry()
        : bit_field_(CodePointField::encode(0) | ValueField::encode(0)) {}
40
    inline CacheEntry(uchar code_point, bool value)
41 42 43 44 45 46 47 48 49 50 51
        : bit_field_(CodePointField::encode(code_point) |
                     ValueField::encode(value)) {}

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

   private:
    class CodePointField : public v8::internal::BitField<uchar, 0, 21> {};
    class ValueField : public v8::internal::BitField<bool, 21, 1> {};

    uint32_t bit_field_;
52 53 54 55 56 57
  };
  static const int kSize = size;
  static const int kMask = kSize - 1;
  CacheEntry entries_[kSize];
};

58

59 60 61 62 63 64 65 66 67 68 69 70 71
// 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:
  inline Mapping() { }
  inline int get(uchar c, uchar n, uchar* result);
 private:
  friend class Test;
  int CalculateValue(uchar c, uchar n, uchar* result);
  struct CacheEntry {
72
    inline CacheEntry() : code_point_(kNoChar), offset_(0) { }
73 74 75
    inline CacheEntry(uchar code_point, signed offset)
      : code_point_(code_point),
        offset_(offset) { }
76 77 78
    uchar code_point_;
    signed offset_;
    static const int kNoChar = (1 << 21) - 1;
79 80 81 82 83 84
  };
  static const int kSize = size;
  static const int kMask = kSize - 1;
  CacheEntry entries_[kSize];
};

85

86 87 88 89
class UnicodeData {
 private:
  friend class Test;
  static int GetByteCount();
90
  static const uchar kMaxCodePoint;
91 92
};

93

94 95
class Utf16 {
 public:
96 97 98
  static inline bool IsSurrogatePair(int lead, int trail) {
    return IsLeadSurrogate(lead) && IsTrailSurrogate(trail);
  }
99
  static inline bool IsLeadSurrogate(int code) {
100 101 102
    if (code == kNoPreviousCharacter) return false;
    return (code & 0xfc00) == 0xd800;
  }
103
  static inline bool IsTrailSurrogate(int code) {
104 105 106 107
    if (code == kNoPreviousCharacter) return false;
    return (code & 0xfc00) == 0xdc00;
  }

108
  static inline int CombineSurrogatePair(uchar lead, uchar trail) {
109 110
    return 0x10000 + ((lead & 0x3ff) << 10) + (trail & 0x3ff);
  }
111
  static const int kNoPreviousCharacter = -1;
112 113 114 115 116 117 118 119 120 121
  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;
122
  static inline uint16_t LeadSurrogate(uint32_t char_code) {
123 124
    return 0xd800 + (((char_code - 0x10000) >> 10) & 0x3ff);
  }
125
  static inline uint16_t TrailSurrogate(uint32_t char_code) {
126 127 128 129 130
    return 0xdc00 + (char_code & 0x3ff);
  }
};


131 132
class Utf8 {
 public:
133
  static inline uchar Length(uchar chr, int previous);
134
  static inline unsigned EncodeOneByte(char* out, uint8_t c);
135 136 137 138
  static inline unsigned Encode(char* out,
                                uchar c,
                                int previous,
                                bool replace_invalid = false);
139
  static uchar CalculateValue(const byte* str, size_t length, size_t* cursor);
140 141 142

  // The unicode replacement character, used to signal invalid unicode
  // sequences (e.g. an orphan surrogate) when converting to a UTF-8 encoding.
143 144 145 146 147 148 149
  static const uchar kBadChar = 0xFFFD;
  static const unsigned kMaxEncodedSize   = 4;
  static const unsigned kMaxOneByteChar   = 0x7f;
  static const unsigned kMaxTwoByteChar   = 0x7ff;
  static const unsigned kMaxThreeByteChar = 0xffff;
  static const unsigned kMaxFourByteChar  = 0x1fffff;

150 151 152 153
  // 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;
154 155 156
  // The maximum size a single UTF-16 code unit may take up when encoded as
  // UTF-8.
  static const unsigned kMax16BitCodeUnitSize  = 3;
157
  static inline uchar ValueOf(const byte* str, size_t length, size_t* cursor);
158 159 160 161 162 163 164 165 166 167 168
};

struct Uppercase {
  static bool Is(uchar c);
};
struct Lowercase {
  static bool Is(uchar c);
};
struct Letter {
  static bool Is(uchar c);
};
169
struct ID_Start {
170 171
  static bool Is(uchar c);
};
172
struct ID_Continue {
173 174
  static bool Is(uchar c);
};
175
struct WhiteSpace {
176 177
  static bool Is(uchar c);
};
178
struct LineTerminator {
179 180 181
  static bool Is(uchar c);
};
struct ToLowercase {
182
  static const int kMaxWidth = 3;
183
  static const bool kIsToLower = true;
184 185 186 187 188 189
  static int Convert(uchar c,
                     uchar n,
                     uchar* result,
                     bool* allow_caching_ptr);
};
struct ToUppercase {
190
  static const int kMaxWidth = 3;
191
  static const bool kIsToLower = false;
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  static int Convert(uchar c,
                     uchar n,
                     uchar* result,
                     bool* allow_caching_ptr);
};
struct Ecma262Canonicalize {
  static const int kMaxWidth = 1;
  static int Convert(uchar c,
                     uchar n,
                     uchar* result,
                     bool* allow_caching_ptr);
};
struct Ecma262UnCanonicalize {
  static const int kMaxWidth = 4;
  static int Convert(uchar c,
                     uchar n,
                     uchar* result,
                     bool* allow_caching_ptr);
};
struct CanonicalizationRange {
  static const int kMaxWidth = 1;
213 214 215 216 217 218 219 220
  static int Convert(uchar c,
                     uchar n,
                     uchar* result,
                     bool* allow_caching_ptr);
};

}  // namespace unibrow

221
#endif  // V8_UNICODE_H_