builtin: Expands string.h / stdint.h

Declares UINT8_MAX, INT8_MAX and defines strnlen(), strncpy(), strncmp() &
memchr(). Needed by a module I'm integrating into cr51.

BRANCH=none
BUG=none
TEST=make buildall -j

Change-Id: I894b0297216df1b945b36fc77cd3bc5c4ef8aa2b
Signed-off-by: Nadim Taha <ntaha@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/436786
Reviewed-by: Randall Spangler <rspangler@chromium.org>
This commit is contained in:
Nadim Taha 2017-02-02 18:48:03 -08:00 committed by Nadim Taha
parent 61a5649e30
commit 69c3fc2378
5 changed files with 119 additions and 0 deletions

View File

@ -23,6 +23,13 @@ typedef unsigned int uintptr_t;
typedef uint8_t uint_least8_t;
#ifndef UINT8_MAX
#define UINT8_MAX (255U)
#endif
#ifndef INT8_MAX
#define INT8_MAX (127U)
#endif
#ifndef UINT16_MAX
#define UINT16_MAX (65535U)
#endif

View File

@ -14,5 +14,10 @@ int memcmp(const void *s1, const void *s2, size_t len);
void *memcpy(void *dest, const void *src, size_t len);
void *memmove(void *dest, const void *src, size_t n);
void *memset(void *dest, int c, size_t len);
void *memchr(const void *buffer, int c, size_t n);
size_t strnlen(const char *s, size_t maxlen);
char *strncpy(char *dest, const char *src, size_t n);
int strncmp(const char *s1, const char *s2, size_t n);
#endif /* __CROS_EC_STRINGS_H__ */

View File

@ -18,6 +18,18 @@ int strlen(const char *s)
}
size_t strnlen(const char *s, size_t maxlen)
{
size_t len = 0;
while (len < maxlen && *s) {
s++;
len++;
}
return len;
}
int isspace(int c)
{
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
@ -49,6 +61,7 @@ int tolower(int c)
int strcasecmp(const char *s1, const char *s2)
{
int diff;
do {
diff = tolower(*s1) - tolower(*s2);
if (diff)
@ -310,6 +323,20 @@ void *memmove(void *dest, const void *src, size_t len)
}
void *memchr(const void *buffer, int c, size_t n)
{
char *current = (char *)buffer;
char *end = current + n;
while (current != end) {
if (*current == c)
return current;
current++;
}
return NULL;
}
void reverse(void *dest, size_t len)
{
int i;
@ -339,6 +366,35 @@ char *strzcpy(char *dest, const char *src, int len)
}
char *strncpy(char *dest, const char *src, size_t n)
{
char *d = dest;
while (n && *src) {
*d++ = *src++;
n--;
}
if (n)
*d = '\0';
return dest;
}
int strncmp(const char *s1, const char *s2, size_t n)
{
while (n--) {
if (*s1 != *s2)
return *s1 - *s2;
if (!*s1)
break;
s1++;
s2++;
}
return 0;
}
int uint64divmod(uint64_t *n, int d)
{
uint64_t q = 0, mask;

View File

@ -69,9 +69,13 @@ int memcmp(const void *s1, const void *s2, size_t len);
void *memcpy(void *dest, const void *src, size_t len);
__visible void *memset(void *dest, int c, size_t len);
void *memmove(void *dest, const void *src, size_t len);
void *memchr(const void *buffer, int c, size_t n);
int strcasecmp(const char *s1, const char *s2);
int strncasecmp(const char *s1, const char *s2, size_t size);
int strlen(const char *s);
size_t strnlen(const char *s, size_t maxlen);
char *strncpy(char *dest, const char *src, size_t n);
int strncmp(const char *s1, const char *s2, size_t n);
/* Like strtol(), but for integers. */
int strtoi(const char *nptr, char **endptr, int base);

View File

@ -231,6 +231,17 @@ static int test_memset(void)
return EC_SUCCESS;
}
static int test_memchr(void)
{
char *buf = "1234";
TEST_ASSERT(memchr("123", '4', 8) == NULL);
TEST_ASSERT(memchr("123", '3', 2) == NULL);
TEST_ASSERT(memchr(buf, '3', 8) == buf + 2);
TEST_ASSERT(memchr(buf, '4', 4) == buf + 3);
return EC_SUCCESS;
}
static int test_strzcpy(void)
{
char dest[10];
@ -245,11 +256,43 @@ static int test_strzcpy(void)
return EC_SUCCESS;
}
static int test_strncpy(void)
{
char dest[10];
strncpy(dest, "test", 10);
TEST_ASSERT_ARRAY_EQ("test", dest, 5);
strncpy(dest, "12345", 6);
TEST_ASSERT_ARRAY_EQ("12345", dest, 6);
strncpy(dest, "testtesttest", 10);
TEST_ASSERT_ARRAY_EQ("testtestte", dest, 10);
return EC_SUCCESS;
}
static int test_strncmp(void)
{
TEST_ASSERT(strncmp("123", "123", 8) == 0);
TEST_ASSERT(strncmp("789", "456", 8) > 0);
TEST_ASSERT(strncmp("abc", "abd", 4) < 0);
TEST_ASSERT(strncmp("abc", "abd", 2) == 0);
return EC_SUCCESS;
}
static int test_strlen(void)
{
TEST_CHECK(strlen("this is a string") == 16);
}
static int test_strnlen(void)
{
TEST_ASSERT(strnlen("this is a string", 17) == 16);
TEST_ASSERT(strnlen("this is a string", 16) == 16);
TEST_ASSERT(strnlen("this is a string", 5) == 5);
return EC_SUCCESS;
}
static int test_strcasecmp(void)
{
TEST_CHECK((strcasecmp("test string", "TEST strIng") == 0) &&
@ -453,8 +496,12 @@ void run_test(void)
RUN_TEST(test_memmove);
RUN_TEST(test_memcpy);
RUN_TEST(test_memset);
RUN_TEST(test_memchr);
RUN_TEST(test_strzcpy);
RUN_TEST(test_strncpy);
RUN_TEST(test_strncmp);
RUN_TEST(test_strlen);
RUN_TEST(test_strnlen);
RUN_TEST(test_strcasecmp);
RUN_TEST(test_strncasecmp);
RUN_TEST(test_atoi);