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"
|
2014-11-05 12:40:56 +00:00
|
|
|
#include "src/utils.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Definitions and convenience functions for working with unicode.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace unibrow {
|
|
|
|
|
2016-02-02 11:44:01 +00:00
|
|
|
typedef unsigned int uchar;
|
|
|
|
typedef unsigned char byte;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2014-11-05 12:40:56 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
private:
|
|
|
|
friend class Test;
|
|
|
|
bool CalculateValue(uchar c);
|
2014-11-05 12:40:56 +00:00
|
|
|
class CacheEntry {
|
|
|
|
public:
|
|
|
|
inline CacheEntry()
|
|
|
|
: bit_field_(CodePointField::encode(0) | ValueField::encode(0)) {}
|
2008-07-03 15:10:15 +00:00
|
|
|
inline CacheEntry(uchar code_point, bool value)
|
2014-11-05 12:40:56 +00:00
|
|
|
: 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_;
|
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
|
|
|
// 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:
|
2016-02-02 11:44:01 +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);
|
2015-02-05 07:54:24 +00:00
|
|
|
static uchar CalculateValue(const byte* str, size_t length, size_t* 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;
|
Rework scanner-character-streams.
- Smaller, more consistent streams API (Advance, Back, pos, Seek)
- Remove implementations from the header, in favor of creation functions.
Observe:
- Performance:
- All Utf16CharacterStream methods have an inlinable V8_LIKELY w/ a
body of only a few instructions. I expect most calls to end up there.
- There used to be performance problems w/ bookmarking, particularly
with copying too much data on SetBookmark w/ UTF-8 streaming streams.
All those copies are gone.
- The old streaming streams implementation used to copy data even for
2-byte input. It no longer does.
- The only remaining 'slow' method is the Seek(.) slow case for utf-8
streaming streams. I don't expect this to be called a lot; and even if,
I expect it to be offset by the gains in the (vastly more frequent)
calls to the other methods or the 'fast path'.
- If it still bothers us, there are several ways to speed it up.
- API & code cleanliness:
- I want to remove the 'old' API in a follow-up CL, which should mostly
delete code, or replace it 1:1.
- In a 2nd follow-up I want to delete much of the UTF-8 handling in Blink
for streaming streams.
- The "bookmark" is now always implemented (and mostly very fast), so we
should be able to use it for more things.
- Testing & correctness:
- The unit tests now cover all stream implementations,
and are pretty good and triggering all the edge cases.
- Vastly more DCHECKs of the invariants.
BUG=v8:4947
Review-Url: https://codereview.chromium.org/2314663002
Cr-Commit-Position: refs/heads/master@{#39464}
2016-09-16 08:29:41 +00:00
|
|
|
static const uchar kBufferEmpty = 0x0;
|
|
|
|
static const uchar kIncomplete = 0xFFFFFFFC; // any non-valid code point.
|
2008-07-03 15:10:15 +00:00
|
|
|
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;
|
2015-02-05 07:54:24 +00:00
|
|
|
static inline uchar ValueOf(const byte* str, size_t length, size_t* cursor);
|
2016-05-12 13:01:10 +00:00
|
|
|
|
Rework scanner-character-streams.
- Smaller, more consistent streams API (Advance, Back, pos, Seek)
- Remove implementations from the header, in favor of creation functions.
Observe:
- Performance:
- All Utf16CharacterStream methods have an inlinable V8_LIKELY w/ a
body of only a few instructions. I expect most calls to end up there.
- There used to be performance problems w/ bookmarking, particularly
with copying too much data on SetBookmark w/ UTF-8 streaming streams.
All those copies are gone.
- The old streaming streams implementation used to copy data even for
2-byte input. It no longer does.
- The only remaining 'slow' method is the Seek(.) slow case for utf-8
streaming streams. I don't expect this to be called a lot; and even if,
I expect it to be offset by the gains in the (vastly more frequent)
calls to the other methods or the 'fast path'.
- If it still bothers us, there are several ways to speed it up.
- API & code cleanliness:
- I want to remove the 'old' API in a follow-up CL, which should mostly
delete code, or replace it 1:1.
- In a 2nd follow-up I want to delete much of the UTF-8 handling in Blink
for streaming streams.
- The "bookmark" is now always implemented (and mostly very fast), so we
should be able to use it for more things.
- Testing & correctness:
- The unit tests now cover all stream implementations,
and are pretty good and triggering all the edge cases.
- Vastly more DCHECKs of the invariants.
BUG=v8:4947
Review-Url: https://codereview.chromium.org/2314663002
Cr-Commit-Position: refs/heads/master@{#39464}
2016-09-16 08:29:41 +00:00
|
|
|
typedef uint32_t Utf8IncrementalBuffer;
|
|
|
|
static uchar ValueOfIncremental(byte next_byte,
|
|
|
|
Utf8IncrementalBuffer* buffer);
|
2016-10-05 17:18:36 +00:00
|
|
|
static uchar ValueOfIncrementalFinish(Utf8IncrementalBuffer* buffer);
|
Rework scanner-character-streams.
- Smaller, more consistent streams API (Advance, Back, pos, Seek)
- Remove implementations from the header, in favor of creation functions.
Observe:
- Performance:
- All Utf16CharacterStream methods have an inlinable V8_LIKELY w/ a
body of only a few instructions. I expect most calls to end up there.
- There used to be performance problems w/ bookmarking, particularly
with copying too much data on SetBookmark w/ UTF-8 streaming streams.
All those copies are gone.
- The old streaming streams implementation used to copy data even for
2-byte input. It no longer does.
- The only remaining 'slow' method is the Seek(.) slow case for utf-8
streaming streams. I don't expect this to be called a lot; and even if,
I expect it to be offset by the gains in the (vastly more frequent)
calls to the other methods or the 'fast path'.
- If it still bothers us, there are several ways to speed it up.
- API & code cleanliness:
- I want to remove the 'old' API in a follow-up CL, which should mostly
delete code, or replace it 1:1.
- In a 2nd follow-up I want to delete much of the UTF-8 handling in Blink
for streaming streams.
- The "bookmark" is now always implemented (and mostly very fast), so we
should be able to use it for more things.
- Testing & correctness:
- The unit tests now cover all stream implementations,
and are pretty good and triggering all the edge cases.
- Vastly more DCHECKs of the invariants.
BUG=v8:4947
Review-Url: https://codereview.chromium.org/2314663002
Cr-Commit-Position: refs/heads/master@{#39464}
2016-09-16 08:29:41 +00:00
|
|
|
|
2016-05-12 13:01:10 +00:00
|
|
|
// Excludes non-characters from the set of valid code points.
|
|
|
|
static inline bool IsValidCharacter(uchar c);
|
|
|
|
|
|
|
|
static bool Validate(const byte* str, size_t length);
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Uppercase {
|
|
|
|
static bool Is(uchar c);
|
|
|
|
};
|
|
|
|
struct Lowercase {
|
|
|
|
static bool Is(uchar c);
|
|
|
|
};
|
|
|
|
struct Letter {
|
|
|
|
static bool Is(uchar c);
|
|
|
|
};
|
2016-10-17 10:01:42 +00:00
|
|
|
struct V8_EXPORT_PRIVATE ID_Start {
|
2008-07-03 15:10:15 +00:00
|
|
|
static bool Is(uchar c);
|
|
|
|
};
|
2016-10-17 10:01:42 +00:00
|
|
|
struct V8_EXPORT_PRIVATE ID_Continue {
|
2008-07-03 15:10:15 +00:00
|
|
|
static bool Is(uchar c);
|
|
|
|
};
|
2016-10-17 10:01:42 +00:00
|
|
|
struct V8_EXPORT_PRIVATE WhiteSpace {
|
2008-07-03 15:10:15 +00:00
|
|
|
static bool Is(uchar c);
|
|
|
|
};
|
2016-10-17 10:01:42 +00:00
|
|
|
struct V8_EXPORT_PRIVATE 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_
|