From 8565efd509236dc7d4e766de39edae2cefb10057 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 24 Apr 2022 23:30:58 -0600 Subject: lib: Add a way to find the postiion of a trailing number At present it is not possible to find out which part of the string is the number part and which is before it. Add a new variant which provides this feature, so we can separate the two in the caller. Signed-off-by: Simon Glass --- lib/strto.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/strto.c b/lib/strto.c index b1d803a77d9..6462d4fddf1 100644 --- a/lib/strto.c +++ b/lib/strto.c @@ -183,7 +183,7 @@ long long simple_strtoll(const char *cp, char **endp, unsigned int base) return simple_strtoull(cp, endp, base); } -long trailing_strtoln(const char *str, const char *end) +long trailing_strtoln_end(const char *str, const char *end, char const **endp) { const char *p; @@ -192,14 +192,24 @@ long trailing_strtoln(const char *str, const char *end) p = end - 1; if (p > str && isdigit(*p)) { do { - if (!isdigit(p[-1])) + if (!isdigit(p[-1])) { + if (endp) + *endp = p; return dectoul(p, NULL); + } } while (--p > str); } + if (endp) + *endp = end; return -1; } +long trailing_strtoln(const char *str, const char *end) +{ + return trailing_strtoln_end(str, end, NULL); +} + long trailing_strtol(const char *str) { return trailing_strtoln(str, NULL); -- cgit v1.2.3