Skip to content

Commit 50b64bf

Browse files
committed
Add ecma_new_ecma_string version that takes buffer with specified number of characters, instead of zero-terminated string.
JerryScript-DCO-1.0-Signed-off-by: Szilard Ledan [email protected] JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent d0e9edc commit 50b64bf

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

jerry-core/ecma/base/ecma-helpers-string.cpp

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,54 @@ ecma_init_ecma_string_from_magic_string_ex_id (ecma_string_t *string_p, /**< des
455455
string_p->u.magic_string_ex_id = magic_string_ex_id;
456456
} /* ecma_init_ecma_string_from_magic_string_ex_id */
457457

458+
/**
459+
* Allocate new ecma-string and fill it with specified number of characters from specified buffer
460+
*
461+
* @return pointer to ecma-string descriptor
462+
*/
463+
ecma_string_t*
464+
ecma_new_ecma_string (const ecma_char_t *string_p, /**< input string */
465+
const ecma_length_t length) /**< number of characters */
466+
{
467+
JERRY_ASSERT (string_p != NULL);
468+
JERRY_ASSERT (length > 0 && length <= ecma_zt_string_length (string_p));
469+
470+
if (length != ecma_zt_string_length (string_p))
471+
{
472+
/* FIXME: update this when 'ecma_is_charset_magic' interface is added */
473+
ecma_char_t *zt_str_p = (ecma_char_t *) mem_heap_alloc_block ((size_t) (length + 1), MEM_HEAP_ALLOC_SHORT_TERM);
474+
memcpy (zt_str_p, string_p, length * sizeof (ecma_char_t));
475+
zt_str_p[length] = 0;
476+
477+
ecma_magic_string_id_t magic_string_id;
478+
if (ecma_is_zt_string_magic (zt_str_p, &magic_string_id))
479+
{
480+
mem_heap_free_block (zt_str_p);
481+
return ecma_get_magic_string (magic_string_id);
482+
}
483+
484+
ecma_magic_string_ex_id_t magic_string_ex_id;
485+
if (ecma_is_zt_ex_string_magic (zt_str_p, &magic_string_ex_id))
486+
{
487+
mem_heap_free_block (zt_str_p);
488+
return ecma_get_magic_string_ex (magic_string_ex_id);
489+
}
490+
mem_heap_free_block (zt_str_p);
491+
}
492+
493+
ecma_string_t *string_desc_p = ecma_alloc_string ();
494+
string_desc_p->refs = 1;
495+
string_desc_p->is_stack_var = false;
496+
string_desc_p->container = ECMA_STRING_CONTAINER_HEAP_CHUNKS;
497+
string_desc_p->hash = ecma_chars_buffer_calc_hash_last_chars (string_p, length);
498+
string_desc_p->u.common_field = 0;
499+
500+
ecma_collection_header_t *collection_p = ecma_new_chars_collection (string_p, length);
501+
ECMA_SET_NON_NULL_POINTER (string_desc_p->u.collection_cp, collection_p);
502+
503+
return string_desc_p;
504+
} /* ecma_new_ecma_string */
505+
458506
/**
459507
* Allocate new ecma-string and fill it with characters from specified buffer
460508
*
@@ -485,19 +533,7 @@ ecma_new_ecma_string (const ecma_char_t *string_p) /**< zero-terminated string *
485533
length++;
486534
}
487535

488-
JERRY_ASSERT (length > 0);
489-
490-
ecma_string_t* string_desc_p = ecma_alloc_string ();
491-
string_desc_p->refs = 1;
492-
string_desc_p->is_stack_var = false;
493-
string_desc_p->container = ECMA_STRING_CONTAINER_HEAP_CHUNKS;
494-
string_desc_p->hash = ecma_chars_buffer_calc_hash_last_chars (string_p, length);
495-
496-
string_desc_p->u.common_field = 0;
497-
ecma_collection_header_t *collection_p = ecma_new_chars_collection (string_p, length);
498-
ECMA_SET_NON_NULL_POINTER (string_desc_p->u.collection_cp, collection_p);
499-
500-
return string_desc_p;
536+
return ecma_new_ecma_string (string_p, length);
501537
} /* ecma_new_ecma_string */
502538

503539
/**

jerry-core/ecma/base/ecma-helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ extern bool ecma_is_completion_value_normal_false (ecma_completion_value_t value
110110
extern bool ecma_is_completion_value_empty (ecma_completion_value_t value);
111111

112112
/* ecma-helpers-string.cpp */
113+
extern ecma_string_t* ecma_new_ecma_string (const ecma_char_t *string_p, const ecma_length_t length);
113114
extern ecma_string_t* ecma_new_ecma_string (const ecma_char_t *string_p);
114115
extern ecma_string_t* ecma_new_ecma_string_from_uint32 (uint32_t uint_number);
115116
extern ecma_string_t* ecma_new_ecma_string_from_number (ecma_number_t number);

0 commit comments

Comments
 (0)