1 | // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #ifndef RUNTIME_VM_UNIBROW_H_ |
6 | #define RUNTIME_VM_UNIBROW_H_ |
7 | |
8 | #include <sys/types.h> |
9 | |
10 | #include "vm/globals.h" |
11 | |
12 | /** |
13 | * \file |
14 | * Definitions and convenience functions for working with unicode. |
15 | */ |
16 | |
17 | namespace unibrow { |
18 | |
19 | // A cache used in case conversion. It caches the value for characters |
20 | // that either have no mapping or map to a single character independent |
21 | // of context. Characters that map to more than one character or that |
22 | // map differently depending on context are always looked up. |
23 | template <class T, intptr_t size = 256> |
24 | class Mapping { |
25 | public: |
26 | inline Mapping() {} |
27 | inline intptr_t get(int32_t c, int32_t n, int32_t* result); |
28 | |
29 | private: |
30 | friend class Test; |
31 | intptr_t CalculateValue(int32_t c, int32_t n, int32_t* result); |
32 | struct CacheEntry { |
33 | inline CacheEntry() : code_point_(kNoChar), offset_(0) {} |
34 | inline CacheEntry(int32_t code_point, signed offset) |
35 | : code_point_(code_point), offset_(offset) {} |
36 | int32_t code_point_; |
37 | signed offset_; |
38 | static const intptr_t kNoChar = (1 << 21) - 1; |
39 | }; |
40 | static const intptr_t kSize = size; |
41 | static const intptr_t kMask = kSize - 1; |
42 | CacheEntry entries_[kSize]; |
43 | }; |
44 | |
45 | struct Letter { |
46 | static bool Is(int32_t c); |
47 | }; |
48 | struct Ecma262Canonicalize { |
49 | static const intptr_t kMaxWidth = 1; |
50 | static intptr_t Convert(int32_t c, |
51 | int32_t n, |
52 | int32_t* result, |
53 | bool* allow_caching_ptr); |
54 | }; |
55 | struct Ecma262UnCanonicalize { |
56 | static const intptr_t kMaxWidth = 4; |
57 | static intptr_t Convert(int32_t c, |
58 | int32_t n, |
59 | int32_t* result, |
60 | bool* allow_caching_ptr); |
61 | }; |
62 | struct CanonicalizationRange { |
63 | static const intptr_t kMaxWidth = 1; |
64 | static intptr_t Convert(int32_t c, |
65 | int32_t n, |
66 | int32_t* result, |
67 | bool* allow_caching_ptr); |
68 | }; |
69 | |
70 | } // namespace unibrow |
71 | |
72 | #endif // RUNTIME_VM_UNIBROW_H_ |
73 | |