|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -#include <ctype.h> |
18 | 17 | #include "ecma-alloc.h"
|
19 | 18 | #include "ecma-builtins.h"
|
20 | 19 | #include "ecma-conversion.h"
|
|
26 | 25 | #include "ecma-string-object.h"
|
27 | 26 | #include "ecma-try-catch-macro.h"
|
28 | 27 | #include "jrt.h"
|
| 28 | +#include "jrt-libc-includes.h" |
29 | 29 |
|
30 | 30 | #ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_STRING_BUILTIN
|
31 | 31 |
|
@@ -567,45 +567,52 @@ ecma_builtin_string_prototype_object_trim (ecma_value_t this_arg) /**< this argu
|
567 | 567 | ecma_op_to_string (this_arg),
|
568 | 568 | ret_value);
|
569 | 569 |
|
570 |
| - ecma_string_t *original_string_p = ecma_get_string_from_value (to_string_val); |
571 |
| - JERRY_ASSERT (ecma_string_get_length (original_string_p) >= 0); |
| 570 | + if (ecma_is_completion_value_empty (ret_value)) |
| 571 | + { |
| 572 | + ecma_string_t *original_string_p = ecma_get_string_from_value (to_string_val); |
| 573 | + JERRY_ASSERT (ecma_string_get_length (original_string_p) >= 0); |
572 | 574 |
|
573 |
| - /* 3 */ |
574 |
| - const uint32_t len = (uint32_t) ecma_string_get_length (original_string_p); |
| 575 | + /* 3 */ |
| 576 | + const uint32_t len = (uint32_t) ecma_string_get_length (original_string_p); |
575 | 577 |
|
576 |
| - uint32_t prefix = 0, postfix = 0; |
577 |
| - uint32_t new_len = 0; |
| 578 | + /* Workaround: avoid repeated call of ecma_string_get_char_at_pos() because its overhead */ |
| 579 | + uint32_t zt_str_size = (uint32_t) sizeof (ecma_char_t) * (len + 1); |
| 580 | + ecma_char_t *original_zt_str_p = (ecma_char_t*) mem_heap_alloc_block (zt_str_size, |
| 581 | + MEM_HEAP_ALLOC_SHORT_TERM); |
| 582 | + ecma_string_to_zt_string (original_string_p, original_zt_str_p, (ssize_t) zt_str_size); |
578 | 583 |
|
579 |
| - while (prefix < len && |
580 |
| - ecma_is_completion_value_empty (ret_value) && |
581 |
| - isspace (ecma_string_get_char_at_pos (original_string_p, prefix))) |
582 |
| - { |
583 |
| - prefix++; |
584 |
| - } |
| 584 | + uint32_t prefix = 0, postfix = 0; |
| 585 | + uint32_t new_len = 0; |
585 | 586 |
|
586 |
| - while (postfix < len && |
587 |
| - ecma_is_completion_value_empty (ret_value) && |
588 |
| - isspace (ecma_string_get_char_at_pos (original_string_p, len-postfix-1))) |
589 |
| - { |
590 |
| - postfix++; |
591 |
| - } |
| 587 | + while (prefix < len && isspace (original_zt_str_p[prefix])) |
| 588 | + { |
| 589 | + prefix++; |
| 590 | + } |
| 591 | + |
| 592 | + while (postfix < len - prefix && isspace (original_zt_str_p[len-postfix-1])) |
| 593 | + { |
| 594 | + postfix++; |
| 595 | + } |
592 | 596 |
|
593 |
| - new_len = len - prefix - postfix; |
| 597 | + new_len = prefix < len ? len - prefix - postfix : 0; |
594 | 598 |
|
595 |
| - MEM_DEFINE_LOCAL_ARRAY (new_str_buffer, new_len + 1, ecma_char_t); |
| 599 | + MEM_DEFINE_LOCAL_ARRAY (new_str_buffer, new_len + 1, ecma_char_t); |
596 | 600 |
|
597 |
| - for (uint32_t idx = 0; idx < new_len; ++idx) |
598 |
| - { |
599 |
| - new_str_buffer[idx] = ecma_string_get_char_at_pos (original_string_p, idx + prefix); |
600 |
| - } |
| 601 | + for (uint32_t idx = 0; idx < new_len; ++idx) |
| 602 | + { |
| 603 | + new_str_buffer[idx] = original_zt_str_p[idx + prefix]; |
| 604 | + } |
601 | 605 |
|
602 |
| - new_str_buffer[new_len] = '\0'; |
603 |
| - ecma_string_t *new_str_p = ecma_new_ecma_string ((ecma_char_t *) new_str_buffer); |
| 606 | + new_str_buffer[new_len] = '\0'; |
| 607 | + ecma_string_t *new_str_p = ecma_new_ecma_string ((ecma_char_t *) new_str_buffer); |
604 | 608 |
|
605 |
| - /* 4 */ |
606 |
| - ret_value = ecma_make_normal_completion_value (ecma_make_string_value (new_str_p)); |
| 609 | + /* 4 */ |
| 610 | + ret_value = ecma_make_normal_completion_value (ecma_make_string_value (new_str_p)); |
607 | 611 |
|
608 |
| - MEM_FINALIZE_LOCAL_ARRAY (new_str_buffer); |
| 612 | + MEM_FINALIZE_LOCAL_ARRAY (new_str_buffer); |
| 613 | + |
| 614 | + mem_heap_free_block (original_zt_str_p); |
| 615 | + } |
609 | 616 |
|
610 | 617 | ECMA_FINALIZE (to_string_val);
|
611 | 618 | ECMA_FINALIZE (check_coercible_val);
|
|
0 commit comments