summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--README.md23
-rw-r--r--lib.c35
-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
5 files changed, 43 insertions, 54 deletions
diff --git a/README.md b/README.md
index a078ce4..33e9bd1 100644
--- a/README.md
+++ b/README.md
@@ -719,23 +719,24 @@ a = map(["foo", 1, true, null, 2.2], type);
// a = ["string", "int", "bool", null, "double"]
```
-#### 6.21. `ord(s, ...)`
+#### 6.21. `ord(s [, offset])`
Without further arguments, this function returns the byte value of the first
character in the given string.
-If one or more index arguments are supplied, an array containing the byte
-values at each given index is returned. If an invalid index is supplied, the
-corresponding array entry will be `null`. Negative index entries are counted
-towards the end of the string, e.g. `-2` will return the value of the second
-last character.
+If an offset argument is supplied, the byte value of the character at this
+position is returned. If an invalid index is supplied, the function will
+return `null`. Negative index entries are counted towards the end of the
+string, e.g. `-2` will return the value of the second last character.
```javascript
-ord("Abc"); // 65
-ord("Abc", 0); // [ 65 ]
-ord("Abc", 1, -1); // [ 98, 99 ]
-ord("Abc", 2, 1, 0); // [ 99, 98, 65 ]
-ord("Abc", 10, -10, "nan"); // [ null, null, null ]
+ord("Abc"); // 65
+ord("Abc", 0); // 65
+ord("Abc", 1); // 98
+ord("Abc", 2); // 99
+ord("Abc", 10); // null
+ord("Abc", -10); // null
+ord("Abc", "nan"); // null
```
#### 6.22. `pop(arr)`
diff --git a/lib.c b/lib.c
index aaa3eac..dc48d84 100644
--- a/lib.c
+++ b/lib.c
@@ -648,10 +648,9 @@ static uc_value_t *
uc_ord(uc_vm_t *vm, size_t nargs)
{
uc_value_t *obj = uc_fn_arg(0);
- uc_value_t *rv, *pos;
const char *str;
- size_t i, len;
- int64_t n;
+ int64_t n = 0;
+ size_t len;
if (ucv_type(obj) != UC_STRING)
return NULL;
@@ -659,30 +658,20 @@ uc_ord(uc_vm_t *vm, size_t nargs)
str = ucv_string_get(obj);
len = ucv_string_length(obj);
- if (nargs == 1)
- return str[0] ? ucv_int64_new((int64_t)str[0]) : NULL;
-
- rv = ucv_array_new(vm);
-
- for (i = 1; i < nargs; i++) {
- pos = uc_fn_arg(i);
-
- if (ucv_type(pos) == UC_INTEGER) {
- n = ucv_int64_get(pos);
-
- if (n < 0)
- n += len;
+ if (nargs > 1) {
+ n = ucv_int64_get(uc_fn_arg(1));
- if (n >= 0 && (uint64_t)n < len) {
- ucv_array_push(rv, ucv_int64_new((int64_t)str[n]));
- continue;
- }
- }
+ if (errno == EINVAL)
+ return NULL;
- ucv_array_push(rv, NULL);
+ if (n < 0)
+ n += len;
}
- return rv;
+ if (n < 0 || (uint64_t)n >= len)
+ return NULL;
+
+ return ucv_int64_new((uint8_t)str[n]);
}
static uc_value_t *
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 --