Position -1 is the last character of a string

Position 0 is invalid, which matches with STRIN/STRRIN
returning 0 on failure.
This commit is contained in:
Rangi
2021-04-19 12:09:32 -04:00
committed by Eldred Habert
parent dc5b7802c8
commit cf2bbe6435
6 changed files with 29 additions and 26 deletions

View File

@@ -191,14 +191,13 @@ static uint32_t adjustNegativePos(int32_t pos, size_t len, char const *functionN
{
/*
* STRSUB and CHARSUB adjust negative `pos` arguments the same way,
* such that position 0 is the last character of a string.
* such that position -1 is the last character of a string.
*/
if (pos < 0)
pos += len + 1;
if (pos < 1) {
pos += len;
if (pos < 1) {
warning(WARNING_BUILTIN_ARG, "%s: Position starts at 1\n", functionName);
pos = 1;
}
warning(WARNING_BUILTIN_ARG, "%s: Position starts at 1\n", functionName);
pos = 1;
}
return (uint32_t)pos;
}