From c90a75823053f1c1ef33c675d42037aa386a88c0 Mon Sep 17 00:00:00 2001 From: John Foerch Date: Sun, 2 Dec 2012 09:40:23 -0500 Subject: [PATCH] position_in_strings: new util Planned usage in future completer nesting design. --- modules/string.js | 21 +++++++++++++++++ tests/simple/utils.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/modules/string.js b/modules/string.js index 13cf984..4331c5a 100644 --- a/modules/string.js +++ b/modules/string.js @@ -175,4 +175,25 @@ function build_url_regexp () { } +/** + * position_in_strings takes a position and an array of strings, and + * returns the index of the string in the array that the position is in. + * At any position which is on the boundary between two strings, the lower + * string is the one that the position is considered to be in. This + * counts also for the first string, so a position of 0 always returns the + * index -1, that is, "before the first string". + */ +function position_in_strings (strings, pos) { + for (var i = 0, t = 0, n = strings.length; + i < n; + ++i) + { + if (strings[i] == null || pos <= t) + break; + t += strings[i].length; + } + return i - 1; +} + + provide("string"); diff --git a/tests/simple/utils.js b/tests/simple/utils.js index 4d2e9ea..89fe7bb 100644 --- a/tests/simple/utils.js +++ b/tests/simple/utils.js @@ -46,6 +46,7 @@ walnut_run({ } }); + walnut_run({ test_possibly_valid_url_1: function () { assert_not(possibly_valid_url("")); @@ -81,3 +82,64 @@ walnut_run({ assert(possibly_valid_url("/")); } }); + + +walnut_run({ + test_position_in_strings_1: function () { + assert_equals(position_in_strings([null], 0), -1); + }, + test_position_in_strings_2: function () { + assert_equals(position_in_strings([], 0), -1); + }, + test_position_in_strings_3: function () { + assert_equals(position_in_strings(["a"], 0), -1); + }, + test_position_in_strings_4: function () { + assert_equals(position_in_strings(["a"], 1), 0); + }, + test_position_in_strings_5: function () { + assert_equals(position_in_strings(["a"], 2), 0); + }, + test_position_in_strings_6: function () { + assert_equals(position_in_strings(["a", "b"], 1), 0); + }, + test_position_in_strings_7: function () { + assert_equals(position_in_strings(["a", "b"], 2), 1); + }, + test_position_in_strings_8: function () { + assert_equals(position_in_strings(["a", "b"], 3), 1); + }, + test_position_in_strings_9: function () { + assert_equals(position_in_strings(["a", "b", "c"], 3), 2); + }, + test_position_in_strings_10: function () { + assert_equals(position_in_strings([""], 1), 0); + }, + test_position_in_strings_11: function () { + assert_equals(position_in_strings(["foo", " ", "bar"], 0), -1); + }, + test_position_in_strings_12: function () { + assert_equals(position_in_strings(["foo", " ", "bar"], 1), 0); + }, + test_position_in_strings_13: function () { + assert_equals(position_in_strings(["foo", " ", "bar"], 2), 0); + }, + test_position_in_strings_14: function () { + assert_equals(position_in_strings(["foo", " ", "bar"], 3), 0); + }, + test_position_in_strings_15: function () { + assert_equals(position_in_strings(["foo", " ", "bar"], 4), 1); + }, + test_position_in_strings_16: function () { + assert_equals(position_in_strings(["foo", " ", "bar"], 5), 2); + }, + test_position_in_strings_17: function () { + assert_equals(position_in_strings(["foo", " ", "bar"], 6), 2); + }, + test_position_in_strings_18: function () { + assert_equals(position_in_strings(["foo", " ", "bar"], 7), 2); + }, + test_position_in_strings_19: function () { + assert_equals(position_in_strings(["foo", " ", "bar"], 8), 2); + } +}); -- 2.11.4.GIT