@@ -37,14 +37,6 @@ 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 =
41
- {
42
- #define LIT_MAGIC_STRING_DEF (id , utf8_string ) \
43
- (const lit_utf8_byte_t * ) utf8_string ,
44
- #include "lit-magic-strings.inc.h"
45
- #undef LIT_MAGIC_STRING_DEF
46
- };
47
-
48
40
JERRY_ASSERT (id < LIT_MAGIC_STRING__COUNT );
49
41
50
42
return magic_strings [id ];
@@ -145,22 +137,32 @@ lit_is_utf8_string_magic (const lit_utf8_byte_t *string_p, /**< utf-8 string */
145
137
lit_utf8_size_t string_size , /**< string size in bytes */
146
138
lit_magic_string_id_t * out_id_p ) /**< [out] magic string's id */
147
139
{
148
- /* TODO: Improve performance of search */
140
+ * out_id_p = LIT_MAGIC_STRING__COUNT ;
149
141
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 ))
142
+ int first = 0 ; /**< First magic string element */
143
+ int last = (int ) (LIT_MAGIC_STRING__COUNT - 1 ); /**< Last magic string element */
144
+
145
+ while (first <= last )
153
146
{
154
- if ( lit_compare_utf8_string_and_magic_string ( string_p , string_size , id ))
155
- {
156
- * out_id_p = id ;
147
+ int middle = (( first + last ) / 2 ); /**< mid point of search */
148
+
149
+ int compare = lit_compare_utf8_string_and_magic_string ( string_p , string_size , magic_strings [ middle ]) ;
157
150
151
+ if (compare == 0 )
152
+ {
153
+ * out_id_p = (lit_magic_string_id_t ) middle ;
158
154
return true;
159
155
}
156
+ else if (compare < 0 )
157
+ {
158
+ last = middle - 1 ;
159
+ }
160
+ else
161
+ {
162
+ first = middle + 1 ;
163
+ }
160
164
}
161
165
162
- * out_id_p = LIT_MAGIC_STRING__COUNT ;
163
-
164
166
return false;
165
167
} /* lit_is_utf8_string_magic */
166
168
@@ -197,18 +199,40 @@ bool lit_is_ex_utf8_string_magic (const lit_utf8_byte_t *string_p, /**< utf-8 st
197
199
/**
198
200
* Compare utf-8 string and magic string for equality
199
201
*
200
- * @return true if strings are equal
201
- * false otherwise
202
+ * @return 0, if areas are equal;
203
+ * <0, if string's content is lexicographically less, than magic string's content;
204
+ * >0, otherwise
202
205
*/
203
- bool
206
+ int
204
207
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 */
208
+ const lit_utf8_size_t buf_size , /**< string size */
209
+ const lit_utf8_byte_t * magic_string_p ) /**< magic string */
207
210
{
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 ));
211
+ lit_utf8_size_t position = 0 ;
212
+
213
+ while (position < buf_size )
214
+ {
215
+ if (magic_string_p [position ] == '\0' )
216
+ {
217
+ return 1 ;
218
+ }
219
+
220
+ int diff = ((int ) string_p [position ]) - ((int ) magic_string_p [position ]);
221
+
222
+ if (diff )
223
+ {
224
+ return diff ;
225
+ }
226
+
227
+ position ++ ;
228
+ }
229
+
230
+ if (magic_string_p [position ] == '\0' )
231
+ {
232
+ return 0 ;
233
+ }
234
+
235
+ return -1 ;
212
236
} /* lit_compare_utf8_string_and_magic_string */
213
237
214
238
/**
0 commit comments