@@ -37,17 +37,19 @@ lit_get_magic_string_ex_count (void)
37
37
const lit_utf8_byte_t *
38
38
lit_get_magic_string_utf8 (lit_magic_string_id_t id ) /**< magic string id */
39
39
{
40
- static const lit_utf8_byte_t * const magic_strings [] JERRY_CONST_DATA =
40
+ static const lit_utf8_byte_t * const lit_magic_strings [] JERRY_CONST_DATA =
41
41
{
42
+ #define LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (size , id )
42
43
#define LIT_MAGIC_STRING_DEF (id , utf8_string ) \
43
44
(const lit_utf8_byte_t * ) utf8_string ,
44
45
#include "lit-magic-strings.inc.h"
45
46
#undef LIT_MAGIC_STRING_DEF
47
+ #undef LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE
46
48
};
47
49
48
50
JERRY_ASSERT (id < LIT_MAGIC_STRING__COUNT );
49
51
50
- return magic_strings [id ];
52
+ return lit_magic_strings [id ];
51
53
} /* lit_get_magic_string_utf8 */
52
54
53
55
/**
@@ -60,17 +62,44 @@ lit_get_magic_string_size (lit_magic_string_id_t id) /**< magic string id */
60
62
{
61
63
static const lit_magic_size_t lit_magic_string_sizes [] JERRY_CONST_DATA =
62
64
{
65
+ #define LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (size , id )
63
66
#define LIT_MAGIC_STRING_DEF (id , utf8_string ) \
64
67
sizeof (utf8_string ) - 1 ,
65
68
#include "lit-magic-strings.inc.h"
66
69
#undef LIT_MAGIC_STRING_DEF
70
+ #undef LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE
67
71
};
68
72
69
73
JERRY_ASSERT (id < LIT_MAGIC_STRING__COUNT );
70
74
71
75
return lit_magic_string_sizes [id ];
72
76
} /* lit_get_magic_string_size */
73
77
78
+ /**
79
+ * Get the block start element with the given size from
80
+ * the list of ECMA and implementation-defined magic string constants
81
+ *
82
+ * @return magic string id
83
+ */
84
+ lit_magic_string_id_t
85
+ lit_get_magic_string_size_block_start (lit_utf8_size_t size ) /**< magic string size */
86
+ {
87
+ static const lit_magic_string_id_t const lit_magic_string_size_block_starts [] JERRY_CONST_DATA =
88
+ {
89
+ #define LIT_MAGIC_STRING_DEF (id , utf8_string )
90
+ #define LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (size , id ) \
91
+ id ,
92
+ #include "lit-magic-strings.inc.h"
93
+ LIT_MAGIC_STRING__COUNT
94
+ #undef LIT_MAGIC_STRING_DEF
95
+ #undef LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE
96
+ };
97
+
98
+ JERRY_ASSERT (size <= (sizeof (lit_magic_string_size_block_starts ) / sizeof (lit_magic_string_id_t )));
99
+
100
+ return lit_magic_string_size_block_starts [size ];
101
+ } /* lit_get_magic_string_size_block_start */
102
+
74
103
/**
75
104
* Get specified magic string as zero-terminated string from external table
76
105
*
@@ -145,22 +174,36 @@ lit_is_utf8_string_magic (const lit_utf8_byte_t *string_p, /**< utf-8 string */
145
174
lit_utf8_size_t string_size , /**< string size in bytes */
146
175
lit_magic_string_id_t * out_id_p ) /**< [out] magic string's id */
147
176
{
148
- /* TODO: Improve performance of search */
177
+ * out_id_p = LIT_MAGIC_STRING__COUNT ;
149
178
150
- for (lit_magic_string_id_t id = (lit_magic_string_id_t ) 0 ;
151
- id < LIT_MAGIC_STRING__COUNT ;
152
- id = (lit_magic_string_id_t ) (id + 1 ))
179
+ if (string_size > lit_get_magic_string_size (LIT_MAGIC_STRING__COUNT - 1 ))
153
180
{
154
- if (lit_compare_utf8_string_and_magic_string (string_p , string_size , id ))
155
- {
156
- * out_id_p = id ;
181
+ return false;
182
+ }
183
+
184
+ int first = (int ) lit_get_magic_string_size_block_start (string_size ); /**< First magic string element */
185
+ int last = (int ) (lit_get_magic_string_size_block_start (string_size + 1 ) - 1 ); /**< Last magic string element */
186
+
187
+ while (first <= last )
188
+ {
189
+ int middle = ((first + last ) / 2 ); /**< mid point of search */
190
+ int compare = memcmp (lit_get_magic_string_utf8 (middle ), string_p , string_size );
157
191
192
+ if (compare == 0 )
193
+ {
194
+ * out_id_p = (lit_magic_string_id_t ) middle ;
158
195
return true;
159
196
}
197
+ else if (compare > 0 )
198
+ {
199
+ last = middle - 1 ;
200
+ }
201
+ else
202
+ {
203
+ first = middle + 1 ;
204
+ }
160
205
}
161
206
162
- * out_id_p = LIT_MAGIC_STRING__COUNT ;
163
-
164
207
return false;
165
208
} /* lit_is_utf8_string_magic */
166
209
@@ -194,23 +237,6 @@ bool lit_is_ex_utf8_string_magic (const lit_utf8_byte_t *string_p, /**< utf-8 st
194
237
return false;
195
238
} /* lit_is_ex_utf8_string_magic */
196
239
197
- /**
198
- * Compare utf-8 string and magic string for equality
199
- *
200
- * @return true if strings are equal
201
- * false otherwise
202
- */
203
- bool
204
- lit_compare_utf8_string_and_magic_string (const lit_utf8_byte_t * string_p , /**< utf-8 string */
205
- lit_utf8_size_t string_size , /**< string size in bytes */
206
- lit_magic_string_id_t magic_string_id ) /**< magic string's id */
207
- {
208
- return lit_compare_utf8_strings (string_p ,
209
- string_size ,
210
- lit_get_magic_string_utf8 (magic_string_id ),
211
- lit_get_magic_string_size (magic_string_id ));
212
- } /* lit_compare_utf8_string_and_magic_string */
213
-
214
240
/**
215
241
* Compare utf-8 string and external magic string for equality
216
242
*
0 commit comments