2011-11-29 10:56:11 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-04 13:36:43 +00:00
|
|
|
#ifndef V8_UNICODE_H_
|
|
|
|
#define V8_UNICODE_H_
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/globals.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Definitions and convenience functions for working with unicode.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace unibrow {
|
|
|
|
|
|
|
|
typedef unsigned int uchar;
|
|
|
|
typedef unsigned char byte;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The max length of the result of converting the case of a single
|
|
|
|
* character.
|
|
|
|
*/
|
2011-11-29 10:56:11 +00:00
|
|
|
const int kMaxMappingSize = 4;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
template <class T, int size = 256>
|
|
|
|
class Predicate {
|
|
|
|
public:
|
|
|
|
inline Predicate() { }
|
|
|
|
inline bool get(uchar c);
|
|
|
|
private:
|
|
|
|
friend class Test;
|
|
|
|
bool CalculateValue(uchar c);
|
|
|
|
struct CacheEntry {
|
|
|
|
inline CacheEntry() : code_point_(0), value_(0) { }
|
|
|
|
inline CacheEntry(uchar code_point, bool value)
|
|
|
|
: code_point_(code_point),
|
|
|
|
value_(value) { }
|
|
|
|
uchar code_point_ : 21;
|
|
|
|
bool value_ : 1;
|
|
|
|
};
|
|
|
|
static const int kSize = size;
|
|
|
|
static const int kMask = kSize - 1;
|
|
|
|
CacheEntry entries_[kSize];
|
|
|
|
};
|
|
|
|
|
2014-10-08 14:55:03 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// 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 {
|
2008-11-25 11:07:48 +00:00
|
|
|
inline CacheEntry() : code_point_(kNoChar), offset_(0) { }
|
2008-07-03 15:10:15 +00:00
|
|
|
inline CacheEntry(uchar code_point, signed offset)
|
|
|
|
: code_point_(code_point),
|
|
|
|
offset_(offset) { }
|
2008-11-25 11:07:48 +00:00
|
|
|
uchar code_point_;
|
|
|
|
signed offset_;
|
|
|
|
static const int kNoChar = (1 << 21) - 1;
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
static const int kSize = size;
|
|
|
|
static const int kMask = kSize - 1;
|
|
|
|
CacheEntry entries_[kSize];
|
|
|
|
};
|
|
|
|
|
2014-10-08 14:55:03 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
class UnicodeData {
|
|
|
|
private:
|
|
|
|
friend class Test;
|
|
|
|
static int GetByteCount();
|
2011-03-18 20:35:07 +00:00
|
|
|
static const uchar kMaxCodePoint;
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
2014-10-08 14:55:03 +00:00
|
|
|
|
2012-03-12 12:35:28 +00:00
|
|
|
class Utf16 {
|
|
|
|
public:
|
2014-01-20 09:52:54 +00:00
|
|
|
static inline bool IsSurrogatePair(int lead, int trail) {
|
|
|
|
return IsLeadSurrogate(lead) && IsTrailSurrogate(trail);
|
|
|
|
}
|
2012-03-12 13:18:30 +00:00
|
|
|
static inline bool IsLeadSurrogate(int code) {
|
2012-03-12 12:35:28 +00:00
|
|
|
if (code == kNoPreviousCharacter) return false;
|
|
|
|
return (code & 0xfc00) == 0xd800;
|
|
|
|
}
|
2012-03-12 13:18:30 +00:00
|
|
|
static inline bool IsTrailSurrogate(int code) {
|
2012-03-12 12:35:28 +00:00
|
|
|
if (code == kNoPreviousCharacter) return false;
|
|
|
|
return (code & 0xfc00) == 0xdc00;
|
|
|
|
}
|
|
|
|
|
2012-03-12 13:18:30 +00:00
|
|
|
static inline int CombineSurrogatePair(uchar lead, uchar trail) {
|
2012-03-12 12:35:28 +00:00
|
|
|
return 0x10000 + ((lead & 0x3ff) << 10) + (trail & 0x3ff);
|
|
|
|
}
|
2012-03-12 13:18:30 +00:00
|
|
|
static const int kNoPreviousCharacter = -1;
|
2012-03-12 12:35:28 +00:00
|
|
|
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;
|
2012-12-20 09:20:37 +00:00
|
|
|
static inline uint16_t LeadSurrogate(uint32_t char_code) {
|
2012-03-12 12:35:28 +00:00
|
|
|
return 0xd800 + (((char_code - 0x10000) >> 10) & 0x3ff);
|
|
|
|
}
|
2012-12-20 09:20:37 +00:00
|
|
|
static inline uint16_t TrailSurrogate(uint32_t char_code) {
|
2012-03-12 12:35:28 +00:00
|
|
|
return 0xdc00 + (char_code & 0x3ff);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
class Utf8 {
|
|
|
|
public:
|
2012-03-12 12:35:28 +00:00
|
|
|
static inline uchar Length(uchar chr, int previous);
|
2013-03-13 19:43:45 +00:00
|
|
|
static inline unsigned EncodeOneByte(char* out, uint8_t c);
|
2014-01-20 09:52:54 +00:00
|
|
|
static inline unsigned Encode(char* out,
|
|
|
|
uchar c,
|
|
|
|
int previous,
|
|
|
|
bool replace_invalid = false);
|
2010-09-14 11:49:06 +00:00
|
|
|
static uchar CalculateValue(const byte* str,
|
|
|
|
unsigned length,
|
|
|
|
unsigned* cursor);
|
2014-01-20 09:52:54 +00:00
|
|
|
|
|
|
|
// The unicode replacement character, used to signal invalid unicode
|
|
|
|
// sequences (e.g. an orphan surrogate) when converting to a UTF-8 encoding.
|
2008-07-03 15:10:15 +00:00
|
|
|
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;
|
|
|
|
|
2012-03-12 12:35:28 +00:00
|
|
|
// 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;
|
2014-01-20 09:52:54 +00:00
|
|
|
// The maximum size a single UTF-16 code unit may take up when encoded as
|
|
|
|
// UTF-8.
|
|
|
|
static const unsigned kMax16BitCodeUnitSize = 3;
|
2008-07-03 15:10:15 +00:00
|
|
|
static inline uchar ValueOf(const byte* str,
|
|
|
|
unsigned length,
|
|
|
|
unsigned* cursor);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Uppercase {
|
|
|
|
static bool Is(uchar c);
|
|
|
|
};
|
|
|
|
struct Lowercase {
|
|
|
|
static bool Is(uchar c);
|
|
|
|
};
|
|
|
|
struct Letter {
|
|
|
|
static bool Is(uchar c);
|
|
|
|
};
|
2014-10-08 14:55:03 +00:00
|
|
|
struct ID_Start {
|
2008-07-03 15:10:15 +00:00
|
|
|
static bool Is(uchar c);
|
|
|
|
};
|
2014-10-08 14:55:03 +00:00
|
|
|
struct ID_Continue {
|
2008-07-03 15:10:15 +00:00
|
|
|
static bool Is(uchar c);
|
|
|
|
};
|
2014-10-08 14:55:03 +00:00
|
|
|
struct WhiteSpace {
|
2008-07-03 15:10:15 +00:00
|
|
|
static bool Is(uchar c);
|
|
|
|
};
|
2014-10-08 14:55:03 +00:00
|
|
|
struct LineTerminator {
|
2008-07-03 15:10:15 +00:00
|
|
|
static bool Is(uchar c);
|
|
|
|
};
|
|
|
|
struct ToLowercase {
|
2008-11-25 11:07:48 +00:00
|
|
|
static const int kMaxWidth = 3;
|
2013-11-07 09:08:34 +00:00
|
|
|
static const bool kIsToLower = true;
|
2008-07-03 15:10:15 +00:00
|
|
|
static int Convert(uchar c,
|
|
|
|
uchar n,
|
|
|
|
uchar* result,
|
|
|
|
bool* allow_caching_ptr);
|
|
|
|
};
|
|
|
|
struct ToUppercase {
|
2008-11-25 11:07:48 +00:00
|
|
|
static const int kMaxWidth = 3;
|
2013-11-07 09:08:34 +00:00
|
|
|
static const bool kIsToLower = false;
|
2008-11-25 11:07:48 +00:00
|
|
|
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;
|
2008-07-03 15:10:15 +00:00
|
|
|
static int Convert(uchar c,
|
|
|
|
uchar n,
|
|
|
|
uchar* result,
|
|
|
|
bool* allow_caching_ptr);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace unibrow
|
|
|
|
|
2009-05-04 13:36:43 +00:00
|
|
|
#endif // V8_UNICODE_H_
|