Skip to content

Commit ebe65c4

Browse files
author
Bela Toth
committed
Implement TrimStart TrimEnd and aliases
Based on: https://tc39.es/ecma262/#sec-string.prototype.trim JerryScript-DCO-1.0-Signed-off-by: Bela Toth [email protected]
1 parent cfd69d3 commit ebe65c4

12 files changed

+115
-19
lines changed

jerry-core/ecma/base/ecma-helpers-conversion.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ ecma_utf8_string_to_number (const lit_utf8_byte_t *str_p, /**< utf-8 string */
373373
return ECMA_NUMBER_ZERO;
374374
}
375375

376-
ecma_string_trim_helper (&str_p, &str_size);
376+
ecma_string_trim_helper (&str_p, &str_size, ECMA_TRIM_STRING_STARTEND);
377377
const lit_utf8_byte_t *end_p = str_p + (str_size - 1);
378378

379379
if (str_size < 1)

jerry-core/ecma/base/ecma-helpers-number.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ ecma_number_parse_int (const lit_utf8_byte_t *string_buff, /**< routine's first
732732
const lit_utf8_byte_t *string_curr_p = string_buff;
733733

734734
/* 2. Remove leading whitespace. */
735-
ecma_string_trim_helper (&string_curr_p, &string_buff_size);
735+
ecma_string_trim_helper (&string_curr_p, &string_buff_size, ECMA_TRIM_STRING_STARTEND);
736736

737737
const lit_utf8_byte_t *string_end_p = string_curr_p + string_buff_size;
738738
const lit_utf8_byte_t *start_p = string_curr_p;
@@ -909,7 +909,7 @@ ecma_number_parse_float (const lit_utf8_byte_t *string_buff, /**< routine's firs
909909
const lit_utf8_byte_t *str_curr_p = string_buff;
910910

911911
/* 2. Remove leading whitespace. */
912-
ecma_string_trim_helper (&str_curr_p, &string_buff_size);
912+
ecma_string_trim_helper (&str_curr_p, &string_buff_size, ECMA_TRIM_STRING_STARTEND);
913913

914914
const lit_utf8_byte_t *str_end_p = str_curr_p + string_buff_size;
915915
const lit_utf8_byte_t *start_p = str_curr_p;

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,7 +2433,8 @@ ecma_string_substr (const ecma_string_t *string_p, /**< pointer to an ecma strin
24332433
*/
24342434
void
24352435
ecma_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
*/
24792491
ecma_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

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ typedef enum
9696
ECMA_STRING_FLAG_MUST_BE_FREED = (1 << 3), /**< The returned buffer must be freed */
9797
} ecma_string_flag_t;
9898

99+
/**
100+
* Enumerable for string trimming location.
101+
*/
102+
typedef enum
103+
{
104+
ECMA_TRIM_STRING_START, /**< Trim only from the start */
105+
ECMA_TRIM_STRING_END, /**< Trim only from the end */
106+
ECMA_TRIM_STRING_STARTEND, /**< Trim from both start and end */
107+
} ecma_trim_string_where;
108+
99109
/**
100110
* Convert ecma-string's contents to a cesu-8 string and put it into a buffer.
101111
*/
@@ -398,15 +408,18 @@ lit_magic_string_id_t ecma_get_string_magic (const ecma_string_t *string_p);
398408
lit_string_hash_t ecma_string_hash (const ecma_string_t *string_p);
399409
ecma_string_t *ecma_string_substr (const ecma_string_t *string_p, lit_utf8_size_t start_pos, lit_utf8_size_t end_pos);
400410
void ecma_string_trim_helper (const lit_utf8_byte_t **utf8_str_p,
401-
lit_utf8_size_t *utf8_str_size);
402-
ecma_string_t *ecma_string_trim (const ecma_string_t *string_p);
411+
lit_utf8_size_t *utf8_str_size,
412+
ecma_trim_string_where trim_position);
403413
#if ENABLED (JERRY_ESNEXT)
404414
ecma_value_t ecma_string_pad (ecma_value_t original_string_p,
405415
ecma_value_t max_length,
406416
ecma_value_t fill_string,
407417
bool pad_on_start);
408418
#endif /* ENABLED (JERRY_ESNEXT) */
409419

420+
ecma_string_t *ecma_string_trim (const ecma_string_t *string_p,
421+
ecma_trim_string_where trim_position);
422+
410423
ecma_stringbuilder_t ecma_stringbuilder_create (void);
411424
ecma_stringbuilder_t ecma_stringbuilder_create_from (ecma_string_t *string_p);
412425
ecma_stringbuilder_t ecma_stringbuilder_create_raw (const lit_utf8_byte_t *data_p,

jerry-core/ecma/builtin-objects/ecma-builtin-intrinsic.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
#include "ecma-container-object.h"
1919
#include "ecma-array-object.h"
2020
#include "ecma-typedarray-object.h"
21+
#include "ecma-string-object.h"
2122
#include "ecma-gc.h"
2223
#include "lit-char-helpers.h"
24+
#include "ecma-helpers.h"
2325

2426
#if ENABLED (JERRY_ESNEXT)
2527

@@ -44,7 +46,9 @@ enum
4446
ECMA_INTRINSIC_ARRAY_TO_STRING,
4547
ECMA_INTRINSIC_DATE_TO_UTC_STRING,
4648
ECMA_INTRINSIC_PARSE_FLOAT,
47-
ECMA_INTRINSIC_PARSE_INT
49+
ECMA_INTRINSIC_PARSE_INT,
50+
ECMA_INTRINSIC_STRING_TRIM_START,
51+
ECMA_INTRINSIC_STRING_TRIM_END
4852
};
4953

5054
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-intrinsic.inc.h"
@@ -206,6 +210,20 @@ ecma_builtin_intrinsic_dispatch_routine (uint16_t builtin_routine_id, /**< built
206210

207211
return ecma_date_value_to_utc_string (*prim_value_p);
208212
}
213+
case ECMA_INTRINSIC_STRING_TRIM_START:
214+
{
215+
ecma_string_t *to_str_p = ecma_op_to_string (this_arg);
216+
ecma_value_t result = ecma_make_string_value (ecma_string_trim (to_str_p, ECMA_TRIM_STRING_START));
217+
ecma_deref_ecma_string (to_str_p);
218+
return result;
219+
}
220+
case ECMA_INTRINSIC_STRING_TRIM_END:
221+
{
222+
ecma_string_t *to_str_p = ecma_op_to_string (this_arg);
223+
ecma_value_t result = ecma_make_string_value (ecma_string_trim (to_str_p, ECMA_TRIM_STRING_END));
224+
ecma_deref_ecma_string (to_str_p);
225+
return result;
226+
}
209227
default:
210228
{
211229
JERRY_ASSERT (builtin_routine_id == ECMA_INTRINSIC_PARSE_INT
@@ -236,7 +254,6 @@ ecma_builtin_intrinsic_dispatch_routine (uint16_t builtin_routine_id, /**< built
236254

237255
ECMA_FINALIZE_UTF8_STRING (string_buff, string_buff_size);
238256
ecma_deref_ecma_string (str_p);
239-
240257
return result;
241258
}
242259
}

jerry-core/ecma/builtin-objects/ecma-builtin-intrinsic.inc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ ROUTINE (LIT_INTERNAL_MAGIC_STRING_ARRAY_PROTOTYPE_VALUES, ECMA_INTRINSIC_ARRAY_
7474
ROUTINE (LIT_INTERNAL_MAGIC_STRING_TYPEDARRAY_PROTOTYPE_VALUES, ECMA_INTRINSIC_TYPEDARRAY_PROTOTYPE_VALUES, 0, 0)
7575
ROUTINE (LIT_INTERNAL_MAGIC_STRING_SET_PROTOTYPE_VALUES, ECMA_INTRINSIC_SET_PROTOTYPE_VALUES, 0, 0)
7676
ROUTINE (LIT_INTERNAL_MAGIC_STRING_MAP_PROTOTYPE_ENTRIES, ECMA_INTRINSIC_MAP_PROTOTYPE_ENTRIES, 0, 0)
77+
ROUTINE (LIT_INTERNAL_MAGIC_STRING_TRIM_START, ECMA_INTRINSIC_STRING_TRIM_START, 0, 0)
78+
ROUTINE (LIT_INTERNAL_MAGIC_STRING_TRIM_END, ECMA_INTRINSIC_STRING_TRIM_END, 0, 0)
7779
ROUTINE (LIT_MAGIC_STRING_TO_STRING_UL, ECMA_INTRINSIC_ARRAY_TO_STRING, 0, 0)
7880
ROUTINE (LIT_MAGIC_STRING_TO_UTC_STRING_UL, ECMA_INTRINSIC_DATE_TO_UTC_STRING, 0, 0)
7981
ROUTINE (LIT_MAGIC_STRING_PARSE_FLOAT, ECMA_INTRINSIC_PARSE_FLOAT, 1, 1)

jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,9 +1040,10 @@ ecma_builtin_string_prototype_object_conversion_helper (ecma_string_t *input_str
10401040
* Returned value must be freed with ecma_free_value.
10411041
*/
10421042
static ecma_value_t
1043-
ecma_builtin_string_prototype_object_trim (ecma_string_t *original_string_p) /**< this argument */
1043+
ecma_builtin_string_prototype_object_trim (ecma_string_t *original_string_p, /**< this argument */
1044+
ecma_trim_string_where trim_position) /**< Trim position */
10441045
{
1045-
ecma_string_t *trimmed_string_p = ecma_string_trim (original_string_p);
1046+
ecma_string_t *trimmed_string_p = ecma_string_trim (original_string_p, trim_position);
10461047

10471048
return ecma_make_string_value (trimmed_string_p);
10481049
} /* ecma_builtin_string_prototype_object_trim */
@@ -1366,7 +1367,7 @@ ecma_builtin_string_prototype_dispatch_routine (uint16_t builtin_routine_id, /**
13661367
}
13671368
case ECMA_STRING_PROTOTYPE_TRIM:
13681369
{
1369-
ret_value = ecma_builtin_string_prototype_object_trim (string_p);
1370+
ret_value = ecma_builtin_string_prototype_object_trim (string_p, ECMA_TRIM_STRING_STARTEND);
13701371
break;
13711372
}
13721373
#if ENABLED (JERRY_BUILTIN_ANNEXB)

jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.inc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ ROUTINE (LIT_MAGIC_STRING_CODE_POINT_AT, ECMA_STRING_PROTOTYPE_CODE_POINT_AT, 1,
7676
ROUTINE (LIT_MAGIC_STRING_PAD_START, ECMA_STRING_PROTOTYPE_PAD_START, 2, 1)
7777
ROUTINE (LIT_MAGIC_STRING_PAD_END, ECMA_STRING_PROTOTYPE_PAD_END, 2, 1)
7878
ROUTINE (LIT_GLOBAL_SYMBOL_ITERATOR, ECMA_STRING_PROTOTYPE_ITERATOR, 0, 0)
79+
80+
INTRINSIC_PROPERTY (LIT_MAGIC_STRING_TRIM_START, LIT_INTERNAL_MAGIC_STRING_TRIM_START, ECMA_PROPERTY_FIXED)
81+
INTRINSIC_PROPERTY (LIT_MAGIC_STRING_TRIM_LEFT, LIT_INTERNAL_MAGIC_STRING_TRIM_START, ECMA_PROPERTY_FIXED)
82+
INTRINSIC_PROPERTY (LIT_MAGIC_STRING_TRIM_END, LIT_INTERNAL_MAGIC_STRING_TRIM_END, ECMA_PROPERTY_FIXED)
83+
INTRINSIC_PROPERTY (LIT_MAGIC_STRING_TRIM_RIGHT, LIT_INTERNAL_MAGIC_STRING_TRIM_END, ECMA_PROPERTY_FIXED)
7984
#endif /* ENABLED (JERRY_ESNEXT) */
8085

8186
#endif /* ENABLED (JERRY_BUILTIN_STRING) */

jerry-core/lit/lit-magic-strings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ typedef enum
4646
LIT_INTERNAL_MAGIC_STRING_MAP_PROTOTYPE_ENTRIES, /**< Map.prototype entries and [@@iterator] routines */
4747
LIT_INTERNAL_MAGIC_THIS_BINDING_VALUE, /**< FunctionEnvironmentRecord [[ThisBindingValue]] internal slot */
4848
LIT_INTERNAL_MAGIC_PROMISE_CAPABILITY, /**< PromiseCapability record */
49+
LIT_INTERNAL_MAGIC_STRING_TRIM_START,
50+
LIT_INTERNAL_MAGIC_STRING_TRIM_END,
4951
/* List of well known symbols */
5052
LIT_GLOBAL_SYMBOL_HAS_INSTANCE, /**< @@hasInstance well known symbol */
5153
LIT_GLOBAL_SYMBOL_IS_CONCAT_SPREADABLE, /**< @@isConcatSpreadable well known symbol */

jerry-core/lit/lit-magic-strings.inc.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SPECIES, "species")
525525
#if ENABLED (JERRY_BUILTIN_NUMBER)
526526
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_FIXED_UL, "toFixed")
527527
#endif
528+
#if ENABLED (JERRY_BUILTIN_STRING) && ENABLED (JERRY_ESNEXT)
529+
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TRIM_END, "trimEnd")
530+
#endif
528531
#if ENABLED (JERRY_BUILTIN_REGEXP) && ENABLED (JERRY_ESNEXT)
529532
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_UNICODE, "unicode")
530533
#endif
@@ -593,6 +596,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_UINT8_UL, "setUint8")
593596
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SUBARRAY, "subarray")
594597
#endif
595598
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_STRING_UL, "toString")
599+
#if ENABLED (JERRY_BUILTIN_STRING) && ENABLED (JERRY_ESNEXT)
600+
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TRIM_LEFT, "trimLeft")
601+
#endif
596602
#if ENABLED (JERRY_BUILTIN_ANNEXB)
597603
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_UNESCAPE, "unescape")
598604
#endif
@@ -663,6 +669,10 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_STRINGIFY, "stringify")
663669
#if ENABLED (JERRY_BUILTIN_STRING)
664670
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SUBSTRING, "substring")
665671
#endif
672+
#if ENABLED (JERRY_BUILTIN_STRING) && ENABLED (JERRY_ESNEXT)
673+
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TRIM_RIGHT, "trimRight")
674+
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TRIM_START, "trimStart")
675+
#endif
666676
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_UNDEFINED, "undefined")
667677
#if ENABLED (JERRY_BUILTIN_TYPEDARRAY)
668678
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_INT16_ARRAY_UL, "Int16Array")

0 commit comments

Comments
 (0)