Skip to content

Implement String.prototype.substring() #318

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
41 changes: 41 additions & 0 deletions jerry-core/ecma/builtin-objects/ecma-builtin-helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,47 @@ ecma_builtin_helper_array_index_normalize (ecma_number_t index, /**< index */
return norm_index;
} /* ecma_builtin_helper_array_index_normalize */

/**
* Helper function to normalizing a string index
*
* This function clamps the given index to the [0, length] range.
* If the index is negative, 0 value is used.
* If the index is greater than the length of the string, the normalized index will be the length of the string.
*
* See also:
* ECMA-262 v5, 15.5.4.15
*
* Used by:
* - The String.prototype.substring routine.
*
* @return uint32_t - the normalized value of the index
*/
uint32_t
ecma_builtin_helper_string_index_normalize (ecma_number_t index, /**< index */
uint32_t length) /**< string's length */
{
uint32_t norm_index = 0;

if (!ecma_number_is_nan (index) && !ecma_number_is_negative (index))
{
if (ecma_number_is_infinity (index))
{
norm_index = length;
}
else
{
norm_index = ecma_number_to_uint32 (index);

if (norm_index > length)
{
norm_index = length;
}
}
}

return norm_index;
} /* ecma_builtin_helper_string_index_normalize */

/**
* @}
* @}
Expand Down
1 change: 1 addition & 0 deletions jerry-core/ecma/builtin-objects/ecma-builtin-helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extern ecma_completion_value_t ecma_builtin_helper_get_to_locale_string_at_index
extern ecma_completion_value_t ecma_builtin_helper_object_get_properties (ecma_object_t *obj,
bool only_enumerable_properties);
extern uint32_t ecma_builtin_helper_array_index_normalize (ecma_number_t index, uint32_t length);
extern uint32_t ecma_builtin_helper_string_index_normalize (ecma_number_t index, uint32_t length);

#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_DATE_BUILTIN
/* ecma-builtin-helpers-date.cpp */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,69 @@ 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);

/* 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);

const ecma_length_t len = ecma_string_get_length (original_string_p);

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

ecma_length_t start = 0, end = len;

start = ecma_builtin_helper_string_index_normalize (start_num, len);

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

end = ecma_builtin_helper_string_index_normalize (end_num, 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 */
ecma_string_t *new_str_p = ecma_string_substr (original_string_p, from, to);
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (new_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
140 changes: 140 additions & 0 deletions tests/jerry/string-prototype-substring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// 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 end 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 both undefined
assert("hello world!".substring(undefined, undefined) === "hello world!");

var undef_var;
assert("hello world!".substring(undef_var, undef_var) === "hello 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");

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);
}

// check coercible - undefined
try {
assert(true.substring() === "");
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}

// check coercible - null
try {
assert(String.prototype.substring.call(null, 0, 1) === "");
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}

// check coercible - Boolean
assert(String.prototype.substring.call(true, 0, 1) === "t");

// check coercible - Object
var test_object = {firstName:"John", lastName:"Doe"};
assert(String.prototype.substring.call(test_object, 0, 7) === "[object");

// check coercible - Number
assert(String.prototype.substring.call(123, 0, 3) === "123");