@@ -2433,7 +2433,8 @@ ecma_string_substr (const ecma_string_t *string_p, /**< pointer to an ecma strin
24332433 */
24342434void
24352435ecma_string_trim_helper (const lit_utf8_byte_t * * utf8_str_p , /**< [in, out] current string position */
2436- lit_utf8_size_t * utf8_str_size ) /**< [in, out] size of the given string */
2436+ lit_utf8_size_t * utf8_str_size , /**< [in, out] size of the given string */
2437+ ecma_trim_string_where trim_position ) /** < enum, signaling trimmin position */
24372438{
24382439 ecma_char_t ch ;
24392440 lit_utf8_size_t read_size ;
@@ -2443,13 +2444,11 @@ ecma_string_trim_helper (const lit_utf8_byte_t **utf8_str_p, /**< [in, out] curr
24432444 while (current_p < nonws_start_p )
24442445 {
24452446 read_size = lit_read_code_unit_from_utf8 (current_p , & ch );
2446-
24472447 if (!lit_char_is_white_space (ch ))
24482448 {
24492449 nonws_start_p = current_p ;
24502450 break ;
24512451 }
2452-
24532452 current_p += read_size ;
24542453 }
24552454
@@ -2458,17 +2457,30 @@ ecma_string_trim_helper (const lit_utf8_byte_t **utf8_str_p, /**< [in, out] curr
24582457 while (current_p > nonws_start_p )
24592458 {
24602459 read_size = lit_read_prev_code_unit_from_utf8 (current_p , & ch );
2461-
24622460 if (!lit_char_is_white_space (ch ))
24632461 {
24642462 break ;
24652463 }
2466-
24672464 current_p -= read_size ;
24682465 }
24692466
2470- * utf8_str_p = nonws_start_p ;
2471- * utf8_str_size = (lit_utf8_size_t ) (current_p - nonws_start_p );
2467+ if (trim_position == ECMA_TRIM_STRING_END )
2468+ {
2469+ * utf8_str_size = * utf8_str_size - (lit_utf8_size_t ) ((* utf8_str_p + * utf8_str_size ) - current_p );
2470+ }
2471+ else
2472+ {
2473+ if (trim_position == ECMA_TRIM_STRING_START )
2474+ {
2475+ * utf8_str_size = (lit_utf8_size_t ) ((* utf8_str_p + * utf8_str_size ) - nonws_start_p );
2476+ }
2477+ else
2478+ {
2479+ * utf8_str_size = (lit_utf8_size_t ) (current_p - nonws_start_p );
2480+ }
2481+
2482+ * utf8_str_p = nonws_start_p ;
2483+ }
24722484} /* ecma_string_trim_helper */
24732485
24742486/**
@@ -2477,7 +2489,8 @@ ecma_string_trim_helper (const lit_utf8_byte_t **utf8_str_p, /**< [in, out] curr
24772489 * @return trimmed ecma string
24782490 */
24792491ecma_string_t *
2480- ecma_string_trim (const ecma_string_t * string_p ) /**< pointer to an ecma string */
2492+ ecma_string_trim (const ecma_string_t * string_p , /**< pointer to an ecma string */
2493+ ecma_trim_string_where trim_position ) /** < enum, signaling trimmin position */
24812494{
24822495 ecma_string_t * ret_string_p ;
24832496
@@ -2487,7 +2500,7 @@ ecma_string_trim (const ecma_string_t *string_p) /**< pointer to an ecma string
24872500
24882501 if (utf8_str_size > 0 )
24892502 {
2490- ecma_string_trim_helper (& utf8_str_p , & utf8_str_size );
2503+ ecma_string_trim_helper (& utf8_str_p , & utf8_str_size , trim_position );
24912504 ret_string_p = ecma_new_ecma_string_from_utf8 (utf8_str_p , utf8_str_size );
24922505 }
24932506 else
0 commit comments