treewide: Move skip_atoi function to commonlib
BUG=none TEST=Build and verify on Screebo TEST=make unit-tests ``` $ make tests/commonlib/bsd/string-test [==========] tests_commonlib_bsd_string-test(tests): Running 1 test(s). [ RUN ] test_skip_atoi [ OK ] test_skip_atoi [==========] tests_commonlib_bsd_string-test(tests): 1 test(s) run. [ PASSED ] 1 test(s). ``` Change-Id: Ifaaa80d0c696a625592ce301f9e3eefb2b4dcd98 Signed-off-by: Maximilian Brune <maximilian.brune@9elements.com> Reviewed-on: https://review.coreboot.org/c/coreboot/+/82910 Reviewed-by: Jakub Czapiga <czapiga@google.com> Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Subrata Banik <subratabanik@google.com>
This commit is contained in:
committed by
Julius Werner
parent
3da7829958
commit
829b94dc98
@ -29,6 +29,7 @@
|
|||||||
#ifndef _STRING_H
|
#ifndef _STRING_H
|
||||||
#define _STRING_H
|
#define _STRING_H
|
||||||
|
|
||||||
|
#include <commonlib/bsd/string.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -47,6 +47,7 @@ ifeq ($(CONFIG_LP_LIBC),y)
|
|||||||
libc-srcs += $(coreboottop)/src/commonlib/bsd/elog.c
|
libc-srcs += $(coreboottop)/src/commonlib/bsd/elog.c
|
||||||
libc-srcs += $(coreboottop)/src/commonlib/bsd/gcd.c
|
libc-srcs += $(coreboottop)/src/commonlib/bsd/gcd.c
|
||||||
libc-srcs += $(coreboottop)/src/commonlib/bsd/ipchksum.c
|
libc-srcs += $(coreboottop)/src/commonlib/bsd/ipchksum.c
|
||||||
|
libc-srcs += $(coreboottop)/src/commonlib/bsd/string.c
|
||||||
ifeq ($(CONFIG_LP_GPL),y)
|
ifeq ($(CONFIG_LP_GPL),y)
|
||||||
libc-srcs += $(coreboottop)/src/commonlib/list.c
|
libc-srcs += $(coreboottop)/src/commonlib/list.c
|
||||||
endif
|
endif
|
||||||
|
@ -65,3 +65,7 @@ decompressor-y += bsd/gcd.c
|
|||||||
all-y += bsd/gcd.c
|
all-y += bsd/gcd.c
|
||||||
|
|
||||||
all-y += bsd/ipchksum.c
|
all-y += bsd/ipchksum.c
|
||||||
|
|
||||||
|
decompressor-y += bsd/string.c
|
||||||
|
smm-y += bsd/string.c
|
||||||
|
all-y += bsd/string.c
|
||||||
|
10
src/commonlib/bsd/include/commonlib/bsd/string.h
Normal file
10
src/commonlib/bsd/include/commonlib/bsd/string.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
/* SPDX-License-Identifier: BSD-3-Clause */
|
||||||
|
|
||||||
|
#ifndef _COMMONLIB_BSD_STRING_H_
|
||||||
|
#define _COMMONLIB_BSD_STRING_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
unsigned int skip_atoi(char **ptr);
|
||||||
|
|
||||||
|
#endif /* _COMMONLIB_BSD_STRING_H_ */
|
15
src/commonlib/bsd/string.c
Normal file
15
src/commonlib/bsd/string.c
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
/* SPDX-License-Identifier: BSD-3-Clause */
|
||||||
|
|
||||||
|
#include <commonlib/bsd/string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
unsigned int skip_atoi(char **ptr)
|
||||||
|
{
|
||||||
|
unsigned int result = 0;
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
for (str = *ptr; isdigit(str[0]); str++)
|
||||||
|
result = result * 10 + (str[0] - '0');
|
||||||
|
*ptr = str;
|
||||||
|
return result;
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
#ifndef STRING_H
|
#ifndef STRING_H
|
||||||
#define STRING_H
|
#define STRING_H
|
||||||
|
|
||||||
|
#include <commonlib/bsd/string.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
void *memcpy(void *dest, const void *src, size_t n);
|
void *memcpy(void *dest, const void *src, size_t n);
|
||||||
@ -36,11 +37,4 @@ long atol(const char *str);
|
|||||||
*/
|
*/
|
||||||
char *strrchr(const char *s, int c);
|
char *strrchr(const char *s, int c);
|
||||||
|
|
||||||
/*
|
|
||||||
* Parses an unsigned integer and moves the input pointer forward to the first
|
|
||||||
* character that's not a valid digit. s and *s must not be NULL. Result
|
|
||||||
* undefined if it overruns the return type size.
|
|
||||||
*/
|
|
||||||
unsigned int skip_atoi(char **s);
|
|
||||||
|
|
||||||
#endif /* STRING_H */
|
#endif /* STRING_H */
|
||||||
|
@ -125,15 +125,6 @@ int strncmp(const char *s1, const char *s2, int maxlen)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int skip_atoi(char **s)
|
|
||||||
{
|
|
||||||
unsigned int i = 0;
|
|
||||||
|
|
||||||
while (isdigit(**s))
|
|
||||||
i = i*10 + *((*s)++) - '0';
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
int strspn(const char *str, const char *spn)
|
int strspn(const char *str, const char *spn)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
tests-y += helpers-test
|
tests-y += helpers-test
|
||||||
tests-y += gcd-test
|
tests-y += gcd-test
|
||||||
tests-y += ipchksum-test
|
tests-y += ipchksum-test
|
||||||
|
tests-y += string-test
|
||||||
|
|
||||||
helpers-test-srcs += tests/commonlib/bsd/helpers-test.c
|
helpers-test-srcs += tests/commonlib/bsd/helpers-test.c
|
||||||
|
|
||||||
@ -11,3 +12,6 @@ gcd-test-srcs += src/commonlib/bsd/gcd.c
|
|||||||
|
|
||||||
ipchksum-test-srcs += tests/commonlib/bsd/ipchksum-test.c
|
ipchksum-test-srcs += tests/commonlib/bsd/ipchksum-test.c
|
||||||
ipchksum-test-srcs += src/commonlib/bsd/ipchksum.c
|
ipchksum-test-srcs += src/commonlib/bsd/ipchksum.c
|
||||||
|
|
||||||
|
string-test-srcs += tests/commonlib/bsd/string-test.c
|
||||||
|
string-test-srcs += src/commonlib/bsd/string.c
|
||||||
|
38
tests/commonlib/bsd/string-test.c
Normal file
38
tests/commonlib/bsd/string-test.c
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
|
||||||
|
#include <commonlib/bsd/string.h>
|
||||||
|
#include <tests/test.h>
|
||||||
|
|
||||||
|
/* Used to test skip_atoi */
|
||||||
|
struct str_with_u_val_t {
|
||||||
|
char *str;
|
||||||
|
uint32_t value;
|
||||||
|
uint32_t offset;
|
||||||
|
} str_with_u_val[] = {
|
||||||
|
{"42aa", 42, 2},
|
||||||
|
{"a", 0, 0},
|
||||||
|
{"0", 0, 1},
|
||||||
|
{"4a2", 4, 1},
|
||||||
|
};
|
||||||
|
|
||||||
|
static void test_skip_atoi(void **state)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *ptr, *copy;
|
||||||
|
|
||||||
|
for (i = 0; i < ARRAY_SIZE(str_with_u_val); i++) {
|
||||||
|
ptr = str_with_u_val[i].str;
|
||||||
|
copy = ptr;
|
||||||
|
assert_true(str_with_u_val[i].value == skip_atoi(&ptr));
|
||||||
|
assert_int_equal(str_with_u_val[i].offset, ptr - copy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
const struct CMUnitTest tests[] = {
|
||||||
|
cmocka_unit_test(test_skip_atoi),
|
||||||
|
};
|
||||||
|
|
||||||
|
return cb_run_group_tests(tests, NULL, NULL);
|
||||||
|
}
|
@ -43,18 +43,6 @@ struct str_with_l_val_t {
|
|||||||
{"\t\n\r\f\v-42", -42},
|
{"\t\n\r\f\v-42", -42},
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Used to test skip_atoi */
|
|
||||||
struct str_with_u_val_t {
|
|
||||||
char *str;
|
|
||||||
uint32_t value;
|
|
||||||
uint32_t offset;
|
|
||||||
} str_with_u_val[] = {
|
|
||||||
{"42aa", 42, 2},
|
|
||||||
{"a", 0, 0},
|
|
||||||
{"0", 0, 1},
|
|
||||||
{"4a2", 4, 1},
|
|
||||||
};
|
|
||||||
|
|
||||||
static void test_strdup(void **state)
|
static void test_strdup(void **state)
|
||||||
{
|
{
|
||||||
char str[] = "Hello coreboot\n";
|
char str[] = "Hello coreboot\n";
|
||||||
@ -204,19 +192,6 @@ static void test_strncmp(void **state)
|
|||||||
assert_int_equal(0, strncmp(str, str2, str2_len - 1));
|
assert_int_equal(0, strncmp(str, str2, str2_len - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_skip_atoi(void **state)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
char *ptr, *copy;
|
|
||||||
|
|
||||||
for (i = 0; i < ARRAY_SIZE(str_with_u_val); i++) {
|
|
||||||
ptr = str_with_u_val[i].str;
|
|
||||||
copy = ptr;
|
|
||||||
assert_true(str_with_u_val[i].value == skip_atoi(&ptr));
|
|
||||||
assert_int_equal(str_with_u_val[i].offset, ptr - copy);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_strspn(void **state)
|
static void test_strspn(void **state)
|
||||||
{
|
{
|
||||||
char str[] = "4213401234";
|
char str[] = "4213401234";
|
||||||
@ -260,7 +235,6 @@ int main(void)
|
|||||||
cmocka_unit_test(test_strcpy),
|
cmocka_unit_test(test_strcpy),
|
||||||
cmocka_unit_test(test_strcmp),
|
cmocka_unit_test(test_strcmp),
|
||||||
cmocka_unit_test(test_strncmp),
|
cmocka_unit_test(test_strncmp),
|
||||||
cmocka_unit_test(test_skip_atoi),
|
|
||||||
cmocka_unit_test(test_strspn),
|
cmocka_unit_test(test_strspn),
|
||||||
cmocka_unit_test(test_strcspn),
|
cmocka_unit_test(test_strcspn),
|
||||||
cmocka_unit_test(test_atol),
|
cmocka_unit_test(test_atol),
|
||||||
|
Reference in New Issue
Block a user