-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from jow-/lib-fix-index
lib: rework uc_index() implementation
- Loading branch information
Showing
4 changed files
with
168 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
The `index()` function locates an element within a given array or a substring | ||
position within a given string, depending on the type of arguments given. | ||
|
||
Returns `null` if the given haystack argument is neither an array nor a string, | ||
returns `-1` if the element was not found within the array or the substring was | ||
not found within the string. | ||
|
||
Returns the first found index position in all other cases. | ||
|
||
-- Testcase -- | ||
{% | ||
let o = {}; | ||
|
||
printf("%.J\n", [ | ||
index([ 1, 2, "abc", 3, "abc", 1, 2 ], "abc"), // should return 2 | ||
index([ 1, 2, 3 ], 4), // should return -1 | ||
index([ [], {} ], {}), // should return -1 (strict equality) | ||
index([ [], o ], o), // should return 1 (strict equality) | ||
|
||
index("foobarfoobarfoobar", "arf"), // should return 4 | ||
index("test", "hello"), // should return -1 | ||
index("test", "test"), // should return 0 (needle = haystack length special case) | ||
index("test", ""), // should return 0 (zero length needle special case) | ||
index("", ""), // should return 0 (zero length special case) | ||
index("foo\0foo\0foo", "o\0f"), // should return 2 (binary safe) | ||
|
||
index({ test: true }, true), // should return null | ||
index(1234, 3), // should return null | ||
]); | ||
%} | ||
-- End -- | ||
|
||
-- Expect stdout -- | ||
[ | ||
2, | ||
-1, | ||
-1, | ||
1, | ||
4, | ||
-1, | ||
0, | ||
0, | ||
0, | ||
2, | ||
null, | ||
null | ||
] | ||
-- End -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
The `rindex()` function locates an element within a given array or a substring | ||
position within a given string, depending on the type of arguments given. | ||
|
||
Returns `null` if the given haystack argument is neither an array nor a string, | ||
returns `-1` if the element was not found within the array or the substring was | ||
not found within the string. | ||
|
||
Returns the last found index position in all other cases. | ||
|
||
-- Testcase -- | ||
{% | ||
let o = {}; | ||
|
||
printf("%.J\n", [ | ||
rindex([ 1, 2, "abc", 3, "abc", 1, 2 ], "abc"), // should return 4 | ||
rindex([ 1, 2, 3 ], 4), // should return -1 | ||
rindex([ [], {} ], {}), // should return -1 (strict equality) | ||
rindex([ [], o ], o), // should return 1 (strict equality) | ||
|
||
rindex("foobarfoobarfoobar", "arf"), // should return 10 | ||
rindex("test", "hello"), // should return -1 | ||
rindex("test", "test"), // should return 0 (needle = haystack length special case) | ||
rindex("test", ""), // should return 4 (zero length needle special case) | ||
rindex("", ""), // should return 0 (zero length special case) | ||
rindex("foo\0foo\0foo", "o\0f"), // should return 6 (binary safe) | ||
|
||
rindex({ test: true }, true), // should return null | ||
rindex(1234, 3), // should return null | ||
]); | ||
%} | ||
-- End -- | ||
|
||
-- Expect stdout -- | ||
[ | ||
4, | ||
-1, | ||
-1, | ||
1, | ||
10, | ||
-1, | ||
0, | ||
4, | ||
0, | ||
6, | ||
null, | ||
null | ||
] | ||
-- End -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
When index() or rindex() was invoked with a string haystack and a non- | ||
string needle argument, a segmentation fault occurred due to an internal | ||
strlen() invocation on a NULL pointer. | ||
|
||
-- Testcase -- | ||
print(index("abc", []), "\n") | ||
-- End -- | ||
|
||
-- Args -- | ||
-R | ||
-- End -- | ||
|
||
-- Expect stdout -- | ||
-1 | ||
-- End -- | ||
|
||
|
||
-- Testcase -- | ||
print(rindex("abc", []), "\n") | ||
-- End -- | ||
|
||
-- Args -- | ||
-R | ||
-- End -- | ||
|
||
-- Expect stdout -- | ||
-1 | ||
-- End -- |