-
Notifications
You must be signed in to change notification settings - Fork 684
Implement String.prototype.trim() #191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs lvidacs.u-szeged@partner.samsung.com
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
#include <ctype.h> | ||
#include "ecma-alloc.h" | ||
#include "ecma-builtins.h" | ||
#include "ecma-conversion.h" | ||
|
@@ -554,7 +555,62 @@ ecma_builtin_string_prototype_object_to_locale_upper_case (ecma_value_t this_arg | |
static ecma_completion_value_t | ||
ecma_builtin_string_prototype_object_trim (ecma_value_t this_arg) /**< this argument */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests for Like from here: var orig = ' foo ';
console.log(orig.trim());
var orig = 'foo ';
console.log(orig.trim()); // 'foo' |
||
{ | ||
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg); | ||
ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); | ||
|
||
/* 1 */ | ||
ECMA_TRY_CATCH (check_coercible_val, | ||
ecma_op_check_object_coercible (this_arg), | ||
ret_value); | ||
|
||
/* 2 */ | ||
ECMA_TRY_CATCH (to_string_val, | ||
ecma_op_to_string (this_arg), | ||
ret_value); | ||
|
||
ecma_string_t *original_string_p = ecma_get_string_from_value (to_string_val); | ||
JERRY_ASSERT (ecma_string_get_length (original_string_p) >= 0); | ||
|
||
/* 3 */ | ||
const uint32_t len = (uint32_t) ecma_string_get_length (original_string_p); | ||
|
||
uint32_t prefix = 0, postfix = 0; | ||
uint32_t new_len = 0; | ||
|
||
while (prefix < len && | ||
ecma_is_completion_value_empty (ret_value) && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need to test the |
||
isspace (ecma_string_get_char_at_pos (original_string_p, prefix))) | ||
{ | ||
prefix++; | ||
} | ||
|
||
while (postfix < len && | ||
ecma_is_completion_value_empty (ret_value) && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need to test the ret_value here? |
||
isspace (ecma_string_get_char_at_pos (original_string_p, len-postfix-1))) | ||
{ | ||
postfix++; | ||
} | ||
|
||
new_len = len - prefix - postfix; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if the whole string is filled only with white spaces? |
||
|
||
MEM_DEFINE_LOCAL_ARRAY (new_str_buffer, new_len + 1, ecma_char_t); | ||
|
||
for (uint32_t idx = 0; idx < new_len; ++idx) | ||
{ | ||
new_str_buffer[idx] = ecma_string_get_char_at_pos (original_string_p, idx + prefix); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this function there are 3 calls to the |
||
} | ||
|
||
new_str_buffer[new_len] = '\0'; | ||
ecma_string_t *new_str_p = ecma_new_ecma_string ((ecma_char_t *) new_str_buffer); | ||
|
||
/* 4 */ | ||
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (new_str_p)); | ||
|
||
MEM_FINALIZE_LOCAL_ARRAY (new_str_buffer); | ||
|
||
ECMA_FINALIZE (to_string_val); | ||
ECMA_FINALIZE (check_coercible_val); | ||
|
||
return ret_value; | ||
} /* ecma_builtin_string_prototype_object_trim */ | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK: we usually use the jrt-libc-include.h and not directly the ctype.h/other libc headers.