summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/03_stdlib/13_ord
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-02-11 18:13:02 +0100
committerJo-Philipp Wich <jo@mein.io>2022-02-11 18:13:02 +0100
commit86f0662f891ac83f474a412b4271af996f1ea44e (patch)
tree58032802705426c0f66fe1c13399140a95e21903 /tests/custom/03_stdlib/13_ord
parent116a8ce35fd50e586d1c79d6f99237428adfa2ef (diff)
lib: change `ord()` to always return single byte value
The most common usecase is extracting the value of a single byte at a specific offset, e.g. to scan a string char-by-char to construct a hash. Furthermore, constructing an array which contains the results of multiple `ord()` invocations is trivial while efficiently extracting a single byte value without the overhead of an intermediate array is not. Due to that, change `ord()` to always return a single integer byte value at the offset specified as second argument or at offset 0 in case no argument was supplied. That means that `ord("Abc", 0, 1, 2)` will now return `65` instead of the former `[ 65, 98, 99 ]` result. Code relying on the former behaviour should either perform multiple calls to `ord()`, passing different offsets each time or switch to the `struct` module which allows efficient unpacking of string data. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/custom/03_stdlib/13_ord')
-rw-r--r--tests/custom/03_stdlib/13_ord35
1 files changed, 17 insertions, 18 deletions
diff --git a/tests/custom/03_stdlib/13_ord b/tests/custom/03_stdlib/13_ord
index 3a69228..7edeff0 100644
--- a/tests/custom/03_stdlib/13_ord
+++ b/tests/custom/03_stdlib/13_ord
@@ -1,19 +1,14 @@
-The `ord()` function extracts the byte values of characters within the
-given input string at different offsets, depending on the arguments.
+The `ord()` function extracts the byte value of the character within the
+given input string at the given offset.
-Without further arguments, the function will return the byte value of
-the first character within the given string.
-
-If one or more offset arguments are given, the function returns an array
-containing the byte values of each character at the corresponding offset.
+If the offset argument is omitted, the byte value of the first character
+is returned.
Returns `null` if the given input string argument is not a string.
-Returns `null` if the given input string is empty and no offset arguments
-are provided.
+Returns `null` if the given input string is empty.
-If invalid offsets are given, the corresponding values within the result
-array will be set to `null`.
+Returns `null` if the given offset value is invalid.
Invalid offsets are non-integer values or integers equal to or larger than
the length of the input string. Negative offsets are converted to positive
@@ -26,11 +21,13 @@ too large, the offset is considered invalid.
print(join("\n", [
ord(123),
ord(""),
+ ord("abcd", "inval"),
ord("abcd"),
ord("abcd", 0),
- ord("abcd", 1, 3, 2),
- ord("abcd", -1, -2),
- ord("abcd", -10, 10)
+ ord("abcd", 1),
+ ord("abcd", -1),
+ ord("abcd", 10),
+ ord("abcd", -10)
]), "\n");
%}
-- End --
@@ -38,9 +35,11 @@ too large, the offset is considered invalid.
-- Expect stdout --
null
null
+null
+97
97
-[ 97 ]
-[ 98, 100, 99 ]
-[ 100, 99 ]
-[ null, null ]
+98
+100
+null
+null
-- End --