@@ -145,22 +145,52 @@ lit_is_utf8_string_magic (const lit_utf8_byte_t *string_p, /**< utf-8 string */
145
145
lit_utf8_size_t string_size , /**< string size in bytes */
146
146
lit_magic_string_id_t * out_id_p ) /**< [out] magic string's id */
147
147
{
148
- /* TODO: Improve performance of search */
148
+ static const lit_utf8_byte_t * const magic_strings [] JERRY_CONST_DATA =
149
+ {
150
+ #define LIT_MAGIC_STRING_DEF (id , utf8_string ) \
151
+ (const lit_utf8_byte_t * ) utf8_string ,
152
+ #include "lit-magic-strings.inc.h"
153
+ #undef LIT_MAGIC_STRING_DEF
154
+ };
149
155
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 ))
156
+ * out_id_p = LIT_MAGIC_STRING__COUNT ;
157
+
158
+ int first = 0 ; /**< First magic string element */
159
+ int last = (int ) (LIT_MAGIC_STRING__COUNT - 1 ); /**< Last magic string element */
160
+ int middle ; /**< mid point of search */
161
+
162
+ while (first <= last )
153
163
{
154
- if (lit_compare_utf8_string_and_magic_string (string_p , string_size , id ))
155
- {
156
- * out_id_p = id ;
164
+ middle = ((first + last ) / 2 );
157
165
158
- return true;
166
+ int compare = memcmp (magic_strings [middle ], string_p , string_size );
167
+ if (compare == 0 )
168
+ {
169
+ lit_utf8_size_t id_size = lit_zt_utf8_string_size (magic_strings [middle ]);
170
+ if (id_size == string_size )
171
+ {
172
+ * out_id_p = (lit_magic_string_id_t ) middle ;
173
+ return true;
174
+ }
175
+ else if (id_size > string_size )
176
+ {
177
+ last = middle - 1 ;
178
+ }
179
+ else
180
+ {
181
+ return false;
182
+ }
183
+ }
184
+ else if (compare > 0 )
185
+ {
186
+ last = middle - 1 ;
187
+ }
188
+ else
189
+ {
190
+ first = middle + 1 ;
159
191
}
160
192
}
161
193
162
- * out_id_p = LIT_MAGIC_STRING__COUNT ;
163
-
164
194
return false;
165
195
} /* lit_is_utf8_string_magic */
166
196
@@ -194,22 +224,6 @@ bool lit_is_ex_utf8_string_magic (const lit_utf8_byte_t *string_p, /**< utf-8 st
194
224
return false;
195
225
} /* lit_is_ex_utf8_string_magic */
196
226
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
227
214
228
/**
215
229
* Compare utf-8 string and external magic string for equality
0 commit comments