Permalink
Browse files

position_in_strings: new util

Planned usage in future completer nesting design.
  • Loading branch information...
1 parent 1663ff4 commit c90a75823053f1c1ef33c675d42037aa386a88c0 @retroj committed Dec 2, 2012
Showing with 83 additions and 0 deletions.
  1. +21 −0 modules/string.js
  2. +62 −0 tests/simple/utils.js
View
@@ -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");
View
@@ -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);
+ }
+});

0 comments on commit c90a758

Please sign in to comment.