Skip to content

Implement String.prototype.substring() #244

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 124 additions & 1 deletion jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,130 @@ ecma_builtin_string_prototype_object_substring (ecma_value_t this_arg, /**< this
ecma_value_t arg1, /**< routine's first argument */
ecma_value_t arg2) /**< routine's second argument */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg1, arg2);
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this implicitly changes the this value?


/* 2 */
ECMA_TRY_CATCH (to_string_val,
ecma_op_to_string (this_arg),
ret_value);

/* 3 */
ecma_string_t *original_string_p = ecma_get_string_from_value (to_string_val);

JERRY_ASSERT (ecma_string_get_length (original_string_p) >= 0);

const uint32_t len = (uint32_t) ecma_string_get_length (original_string_p);

/* 4, 6 */
ECMA_OP_TO_NUMBER_TRY_CATCH (start_num,
arg1,
ret_value);

uint32_t start = 0, end = len;

if (ecma_number_is_nan (start_num) || ecma_number_is_negative (start_num))
{
start = 0;
}
else
{
if (ecma_number_is_infinity (start_num))
{
start = len;
}
else
{
start = ecma_number_to_uint32 (start_num);

if (start > len)
{
start = len;
}
}
}

/* 5, 7 */
if (ecma_is_value_undefined (arg2))
{
end = len;
}
else
{
ECMA_OP_TO_NUMBER_TRY_CATCH (end_num,
arg2,
ret_value);

if (ecma_number_is_nan (end_num) || ecma_number_is_negative (end_num))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if-else block seems identical with the one above, could we have maybe a helper method for it? Another thing: there is the ecma_builtin_helper_array_index_normalize function which does something like this, could you check if that could be used?

{
end = 0;
}
else
{
if (ecma_number_is_infinity (end_num))
{
end = len;
}
else
{
end = ecma_number_to_uint32 (end_num);

if (end > len)
{
end = len;
}
}
}

ECMA_OP_TO_NUMBER_FINALIZE (end_num);
}

if (ecma_is_completion_value_empty (ret_value))
{
JERRY_ASSERT (start <= len && end <= len);

/* 8 */
uint32_t from = start < end ? start : end;

/* 9 */
uint32_t to = start > end ? start : end;

/* 10 */
/* Workaround: avoid repeated call of ecma_string_get_char_at_pos() because its overhead */
uint32_t zt_str_size = (uint32_t) sizeof (ecma_char_t) * (len + 1);
ecma_char_t *original_zt_str_p = (ecma_char_t*) mem_heap_alloc_block (zt_str_size,
MEM_HEAP_ALLOC_SHORT_TERM);
ecma_string_to_zt_string (original_string_p, original_zt_str_p, (ssize_t) zt_str_size);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is changed now to some UTF-8 processing.


uint32_t new_len = to - from;

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] = original_zt_str_p[idx + from];
}

new_str_buffer[new_len] = '\0';
ecma_string_t *new_str_p = ecma_new_ecma_string ((ecma_char_t *) new_str_buffer);

ret_value = ecma_make_normal_completion_value (ecma_make_string_value (new_str_p));

MEM_FINALIZE_LOCAL_ARRAY (new_str_buffer);

mem_heap_free_block (original_zt_str_p);
}

ECMA_OP_TO_NUMBER_FINALIZE (start_num);

ECMA_FINALIZE (to_string_val);
ECMA_FINALIZE (check_coercible_val);

return ret_value;
} /* ecma_builtin_string_prototype_object_substring */

/**
Expand Down
108 changes: 108 additions & 0 deletions tests/jerry/string-prototype-substring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright 2015 Samsung Electronics Co., Ltd.
// Copyright 2015 University of Szeged.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// check properties
assert(Object.getOwnPropertyDescriptor(String.prototype.substring, 'length').configurable === false);

assert(Object.getOwnPropertyDescriptor(String.prototype.substring, 'length').enumerable === false);

assert(Object.getOwnPropertyDescriptor(String.prototype.substring, 'length').writable === false);

assert(String.prototype.substring.length === 2);

assert(String.prototype.substring.call(new String()) === "");

assert(String.prototype.substring.call({}) === "[object Object]");

// check this is undefined
try {
String.prototype.substring.call(undefined);
assert(false);
} catch(e) {
assert(e instanceof TypeError);
}

// check this is null
try {
String.prototype.substring.call(null);
assert(false);
} catch(e) {
assert(e instanceof TypeError);
}

// simple checks
assert("hello world!".substring(0, 11) === "hello world");

assert("hello world!".substring(11, 0) === "hello world");

assert("hello world!".substring(0, 12) === "hello world!");

assert("hello world!".substring(12, 0) === "hello world!");

// check NaN
assert("hello world!".substring(NaN, 12) === "hello world!");

// check NaN
assert("hello world!".substring(2, NaN) === "he");

// check and undefined
assert("hello world!".substring(2, undefined) === "llo world!");

// check negative
assert("hello world!".substring(-1,8) === "hello wo");

// check negative
assert("hello\tworld!".substring(5,-8) === "hello");

// check negative
assert("hello world!".substring(-1,-8) === "");

// check ranges
assert("hello world!".substring(-1,10000) === "hello world!");

assert("hello world!".substring(10000,1000000) === "");

assert("hello world!".substring(100000,1) === "ello world!");

// check integer conversion
assert("hello world!".substring(undefined, 5) === "hello");

assert("hello world!".substring(undefined, "bar") === "");

assert("hello world!".substring(2, true) === "e");

assert("hello world!".substring(2, false) === "he");

assert("hello world!".substring(5, obj) === " world!");

// check other objects
var obj = { substring : String.prototype.substring }

obj.toString = function() {
return "Iam";
}
assert(obj.substring(100000,1) === "am");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should try assert(true.subString() === ); and similar objectsd. I don't know if coercible works.


obj.toString = function() {
throw new ReferenceError ("foo");
};

try {
assert(obj.substring(100000,1));
assert(false);
} catch (e) {
assert(e.message === "foo");
assert(e instanceof ReferenceError);
}