Add an API function to calculate the UTF-8 encoded string size from Jerry string. (#1450)

JerryScript-DCO-1.0-Signed-off-by: Robert Sipka rsipka.uszeged@partner.samsung.com
This commit is contained in:
Robert Sipka
2016-11-29 12:25:18 +01:00
committed by GitHub
parent 6a2f54456f
commit 0467239d03
8 changed files with 189 additions and 11 deletions
+34
View File
@@ -281,6 +281,40 @@ lit_utf8_string_length (const lit_utf8_byte_t *utf8_buf_p, /**< utf-8 string */
return length;
} /* lit_utf8_string_length */
/**
* Calculate the required size of an utf-8 encoded string from cesu-8 encoded string
*
* @return size of an utf-8 encoded string
*/
lit_utf8_size_t
lit_get_utf8_size_of_cesu8_string (const lit_utf8_byte_t *cesu8_buf_p, /**< cesu-8 string */
lit_utf8_size_t cesu8_buf_size) /**< string size */
{
lit_utf8_size_t offset = 0;
lit_utf8_size_t utf8_buf_size = cesu8_buf_size;
while (offset < cesu8_buf_size)
{
ecma_char_t ch;
offset += lit_read_code_unit_from_utf8 (cesu8_buf_p + offset, &ch);
if (lit_is_code_point_utf16_high_surrogate (ch) && (offset < cesu8_buf_size))
{
ecma_char_t next_ch;
offset += lit_read_code_unit_from_utf8 (cesu8_buf_p + offset, &next_ch);
if (lit_is_code_point_utf16_low_surrogate (next_ch))
{
utf8_buf_size -= 2;
}
}
}
JERRY_ASSERT (offset == cesu8_buf_size);
return utf8_buf_size;
} /* lit_get_utf8_size_of_cesu8_string */
/**
* Decodes a unicode code point from non-empty utf-8-encoded buffer
*
+1
View File
@@ -95,6 +95,7 @@ bool lit_is_code_point_utf16_high_surrogate (lit_code_point_t);
/* size */
lit_utf8_size_t lit_zt_utf8_string_size (const lit_utf8_byte_t *);
lit_utf8_size_t lit_get_utf8_size_of_cesu8_string (const lit_utf8_byte_t *, lit_utf8_size_t);
/* length */
ecma_length_t lit_utf8_string_length (const lit_utf8_byte_t *, lit_utf8_size_t);