summaryrefslogtreecommitdiffhomepage
path: root/tests
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
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')
-rw-r--r--tests/custom/03_stdlib/13_ord35
-rw-r--r--tests/custom/04_bugs/19_truncated_format_string2
-rw-r--r--tests/custom/04_bugs/31_vallist_8bit_shortstrings2
3 files changed, 19 insertions, 20 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 --
diff --git a/tests/custom/04_bugs/19_truncated_format_string b/tests/custom/04_bugs/19_truncated_format_string
index 8ddd0a3..ead0fdb 100644
--- a/tests/custom/04_bugs/19_truncated_format_string
+++ b/tests/custom/04_bugs/19_truncated_format_string
@@ -9,6 +9,6 @@ to the resulting string.
-- Testcase --
{%
let s = sprintf("%");
- print(ord(s, 0, 1), "\n");
+ print([ ord(s, 0), ord(s, 1) ], "\n");
%}
-- End --
diff --git a/tests/custom/04_bugs/31_vallist_8bit_shortstrings b/tests/custom/04_bugs/31_vallist_8bit_shortstrings
index 98ccf0b..9d02f42 100644
--- a/tests/custom/04_bugs/31_vallist_8bit_shortstrings
+++ b/tests/custom/04_bugs/31_vallist_8bit_shortstrings
@@ -3,7 +3,7 @@ to/from pointer addresses, 8 bit characters where incorrectly clamped
to `-1` (`255`).
-- Testcase --
-{{ ord("ö", 1)[0] != -1 }}
+{{ ord("ö", 1) != -1 }}
-- End --
-- Expect stdout --