Skip to content

Commit caa1617

Browse files
lvidacsgalpeter
authored andcommitted
Implement String.prototype.trim()
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs lvidacs.u-szeged@partner.samsung.com
1 parent 913519d commit caa1617

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

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

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "ecma-string-object.h"
2626
#include "ecma-try-catch-macro.h"
2727
#include "jrt.h"
28+
#include "jrt-libc-includes.h"
2829

2930
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_STRING_BUILTIN
3031

@@ -554,7 +555,66 @@ ecma_builtin_string_prototype_object_to_locale_upper_case (ecma_value_t this_arg
554555
static ecma_completion_value_t
555556
ecma_builtin_string_prototype_object_trim (ecma_value_t this_arg) /**< this argument */
556557
{
557-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg);
558+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
559+
560+
/* 1 */
561+
ECMA_TRY_CATCH (check_coercible_val,
562+
ecma_op_check_object_coercible (this_arg),
563+
ret_value);
564+
565+
/* 2 */
566+
ECMA_TRY_CATCH (to_string_val,
567+
ecma_op_to_string (this_arg),
568+
ret_value);
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);
572+
573+
/* 3 */
574+
const uint32_t len = (uint32_t) ecma_string_get_length (original_string_p);
575+
576+
/* Workaround: avoid repeated call of ecma_string_get_char_at_pos() because its overhead */
577+
uint32_t zt_str_size = (uint32_t) sizeof (ecma_char_t) * (len + 1);
578+
ecma_char_t *original_zt_str_p = (ecma_char_t*) mem_heap_alloc_block (zt_str_size,
579+
MEM_HEAP_ALLOC_SHORT_TERM);
580+
ecma_string_to_zt_string (original_string_p, original_zt_str_p, (ssize_t) zt_str_size);
581+
582+
uint32_t prefix = 0, postfix = 0;
583+
uint32_t new_len = 0;
584+
585+
while (prefix < len && isspace (original_zt_str_p[prefix]))
586+
{
587+
prefix++;
588+
}
589+
590+
while (postfix < len - prefix && isspace (original_zt_str_p[len - postfix - 1]))
591+
{
592+
postfix++;
593+
}
594+
595+
new_len = prefix < len ? len - prefix - postfix : 0;
596+
597+
MEM_DEFINE_LOCAL_ARRAY (new_str_buffer, new_len + 1, ecma_char_t);
598+
599+
for (uint32_t idx = 0; idx < new_len; ++idx)
600+
{
601+
new_str_buffer[idx] = original_zt_str_p[idx + prefix];
602+
}
603+
604+
new_str_buffer[new_len] = '\0';
605+
ecma_string_t *new_str_p = ecma_new_ecma_string ((ecma_char_t *) new_str_buffer);
606+
607+
/* 4 */
608+
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (new_str_p));
609+
610+
MEM_FINALIZE_LOCAL_ARRAY (new_str_buffer);
611+
612+
mem_heap_free_block (original_zt_str_p);
613+
614+
ECMA_FINALIZE (to_string_val);
615+
ECMA_FINALIZE (check_coercible_val);
616+
617+
return ret_value;
558618
} /* ecma_builtin_string_prototype_object_trim */
559619

560620
/**

tests/jerry/string-prototype-trim.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
2+
// Copyright 2015 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
// check properties
17+
assert(Object.getOwnPropertyDescriptor(String.prototype.trim, 'length').configurable === false);
18+
19+
assert(Object.getOwnPropertyDescriptor(String.prototype.trim, 'length').enumerable === false);
20+
21+
assert(Object.getOwnPropertyDescriptor(String.prototype.trim, 'length').writable === false);
22+
23+
assert(String.prototype.trim.length === 0);
24+
25+
// check this value
26+
assert(String.prototype.trim.call(new String()) === "");
27+
28+
assert(String.prototype.trim.call({}) === "[object Object]");
29+
30+
// check undefined
31+
try {
32+
String.prototype.trim.call(undefined);
33+
assert(false);
34+
} catch(e) {
35+
assert(e instanceof TypeError);
36+
}
37+
38+
// check null
39+
try {
40+
String.prototype.trim.call(null);
41+
assert(false);
42+
} catch(e) {
43+
assert(e instanceof TypeError);
44+
}
45+
46+
// simple checks
47+
assert(" hello world".trim() === "hello world");
48+
49+
assert("hello world ".trim() === "hello world");
50+
51+
assert(" hello world ".trim() === "hello world");
52+
53+
assert("\t hello world\n".trim() === "hello world");
54+
55+
assert("\t\n hello world\t \n ".trim() === "hello world");
56+
57+
assert("hello world\n \t\t".trim() === "hello world");
58+
59+
assert(" hello world \\ ".trim() === "hello world \\");
60+
61+
assert("**hello world**".trim() === "**hello world**");
62+
63+
assert(" \t \n".trim() === "");
64+
65+
assert(" ".trim() === "");
66+
67+
assert("".trim() === "");
68+
69+
// FIXME: add unicode tests when unicode support available

0 commit comments

Comments
 (0)