Implement STRRIN, like STRIN but searching from the right

This commit is contained in:
Rangi
2020-12-09 20:08:26 -05:00
committed by Eldred Habert
parent 3e4c2fe712
commit 58739b0bf2
6 changed files with 40 additions and 2 deletions

View File

@@ -54,6 +54,21 @@ static uint32_t str2int2(uint8_t *s, int32_t length)
return r;
}
static char *strrstr(char *s1, char *s2)
{
size_t len1 = strlen(s1);
size_t len2 = strlen(s2);
if (len2 > len1)
return NULL;
for (char *p = s1 + len1 - len2; p >= s1; p--)
if (!strncmp(p, s2, len2))
return p;
return NULL;
}
static size_t strlenUTF8(const char *s)
{
size_t len = 0;
@@ -233,6 +248,7 @@ static inline void failAssertMsg(enum AssertionType type, char const *msg)
%left T_OP_STRCMP
%left T_OP_STRIN
%left T_OP_STRRIN
%left T_OP_STRSUB
%left T_OP_STRLEN
%left T_OP_STRCAT
@@ -999,6 +1015,11 @@ relocexpr_no_str : scoped_id { rpn_Symbol(&$$, $1); }
rpn_Number(&$$, p ? p - $3 + 1 : 0);
}
| T_OP_STRRIN T_LPAREN string T_COMMA string T_RPAREN {
char *p = strrstr($3, $5);
rpn_Number(&$$, p ? p - $3 + 1 : 0);
}
| T_OP_STRLEN T_LPAREN string T_RPAREN {
rpn_Number(&$$, strlenUTF8($3));
}