mirror of
https://github.com/AsahiLinux/u-boot
synced 2024-11-13 00:17:23 +00:00
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 <sjg@chromium.org>
This commit is contained in:
parent
d667a0d8f4
commit
8565efd509
3 changed files with 42 additions and 3 deletions
|
@ -121,6 +121,24 @@ long trailing_strtol(const char *str);
|
||||||
*/
|
*/
|
||||||
long trailing_strtoln(const char *str, const char *end);
|
long trailing_strtoln(const char *str, const char *end);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* trailing_strtoln_end() - extract trailing integer from a fixed-length string
|
||||||
|
*
|
||||||
|
* Given a fixed-length string this finds a trailing number on the string
|
||||||
|
* and returns it. For example, "abc123" would return 123. Only the
|
||||||
|
* characters between @str and @end - 1 are examined. If @end is NULL, it is
|
||||||
|
* set to str + strlen(str).
|
||||||
|
*
|
||||||
|
* @str: String to examine
|
||||||
|
* @end: Pointer to end of string to examine, or NULL to use the
|
||||||
|
* whole string
|
||||||
|
* @endp: If non-NULL, this is set to point to the character where the
|
||||||
|
* number starts, e.g. for "mmc0" this would be point to the '0'; if no
|
||||||
|
* trailing number is found, it is set to the end of the string
|
||||||
|
* Return: training number if found, else -1
|
||||||
|
*/
|
||||||
|
long trailing_strtoln_end(const char *str, const char *end, char const **endp);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* panic() - Print a message and reset/hang
|
* panic() - Print a message and reset/hang
|
||||||
*
|
*
|
||||||
|
|
14
lib/strto.c
14
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);
|
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;
|
const char *p;
|
||||||
|
|
||||||
|
@ -192,14 +192,24 @@ long trailing_strtoln(const char *str, const char *end)
|
||||||
p = end - 1;
|
p = end - 1;
|
||||||
if (p > str && isdigit(*p)) {
|
if (p > str && isdigit(*p)) {
|
||||||
do {
|
do {
|
||||||
if (!isdigit(p[-1]))
|
if (!isdigit(p[-1])) {
|
||||||
|
if (endp)
|
||||||
|
*endp = p;
|
||||||
return dectoul(p, NULL);
|
return dectoul(p, NULL);
|
||||||
|
}
|
||||||
} while (--p > str);
|
} while (--p > str);
|
||||||
}
|
}
|
||||||
|
if (endp)
|
||||||
|
*endp = end;
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long trailing_strtoln(const char *str, const char *end)
|
||||||
|
{
|
||||||
|
return trailing_strtoln_end(str, end, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
long trailing_strtol(const char *str)
|
long trailing_strtol(const char *str)
|
||||||
{
|
{
|
||||||
return trailing_strtoln(str, NULL);
|
return trailing_strtoln(str, NULL);
|
||||||
|
|
|
@ -244,7 +244,9 @@ STR_TEST(str_xtoa, 0);
|
||||||
|
|
||||||
static int str_trailing(struct unit_test_state *uts)
|
static int str_trailing(struct unit_test_state *uts)
|
||||||
{
|
{
|
||||||
char str1[] = "abc123def";
|
const char str1[] = "abc123def";
|
||||||
|
const char str2[] = "abc123def456";
|
||||||
|
const char *end;
|
||||||
|
|
||||||
ut_asserteq(-1, trailing_strtol(""));
|
ut_asserteq(-1, trailing_strtol(""));
|
||||||
ut_asserteq(-1, trailing_strtol("123"));
|
ut_asserteq(-1, trailing_strtol("123"));
|
||||||
|
@ -259,6 +261,15 @@ static int str_trailing(struct unit_test_state *uts)
|
||||||
|
|
||||||
ut_asserteq(3, trailing_strtol("a3"));
|
ut_asserteq(3, trailing_strtol("a3"));
|
||||||
|
|
||||||
|
ut_asserteq(123, trailing_strtoln_end(str1, str1 + 6, &end));
|
||||||
|
ut_asserteq(3, end - str1);
|
||||||
|
|
||||||
|
ut_asserteq(-1, trailing_strtoln_end(str1, str1 + 7, &end));
|
||||||
|
ut_asserteq(7, end - str1);
|
||||||
|
|
||||||
|
ut_asserteq(456, trailing_strtoln_end(str2, NULL, &end));
|
||||||
|
ut_asserteq(9, end - str2);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
STR_TEST(str_trailing, 0);
|
STR_TEST(str_trailing, 0);
|
||||||
|
|
Loading…
Reference in a new issue