Skip to content

Commit

Permalink
Move more of the simple string functions to the header, to allow inli…
Browse files Browse the repository at this point in the history
…ning and further optimization.

Before:
   text    data     bss     dec     hex filename
  10374      19    2712   13105    3331 app/config.o
 106854   26720   13344  146918   23de6 memtest_shared
   8734      19    2712   11465    2cc9 app/config.o
 111310   28392  294688  434390   6a0d6 memtest_shared

After:
   text    data     bss     dec     hex filename
  10105      19    2712   12836    3224 app/config.o
 106580   26720   13344  146644   23cd4 memtest_shared
   8653      19    2712   11384    2c78 app/config.o
 110969   28392  294688  434049   69f81 memtest_shared
  • Loading branch information
debrouxl authored and x86fr committed Jan 23, 2023
1 parent b6992b9 commit 3aeda70
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
34 changes: 0 additions & 34 deletions lib/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,6 @@ void reverse(char s[])
// Public Functions
//------------------------------------------------------------------------------

int memcmp(const void *s1, const void *s2, size_t n)
{
const unsigned char *src1 = s1, *src2 = s2;

for (size_t i = 0; i < n; i++) {
if (src1[i] != src2[i]) {
return (int)src1[i] - (int)src2[i];
}
}
return 0;
}

void *memmove(void *dest, const void *src, size_t n)
{
char *d = (char *)dest, *s = (char *)src;
Expand All @@ -64,28 +52,6 @@ void *memmove(void *dest, const void *src, size_t n)
return dest;
}

size_t strlen(const char *s)
{
size_t len = 0;
while (*s++) {
len++;
}
return len;
}

int strncmp(const char *s1, const char *s2, size_t n)
{
for (size_t i = 0; i < n; i++) {
if (s1[i] != s2[i]) {
return (int)s1[i] - (int)s2[i];
}
if (s1[i] == '\0') {
return 0;
}
}
return 0;
}

char *strstr(const char *haystack, const char *needle)
{
size_t haystack_len = strlen(haystack);
Expand Down
34 changes: 31 additions & 3 deletions lib/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@
* between the first mismatching byte in s1 (interpreted as an unsigned
* value) and the corresponding byte in s2.
*/
int memcmp(const void *s1, const void *s2, size_t n);
static inline int memcmp(const void *s1, const void *s2, size_t n)
{
const unsigned char *src1 = s1, *src2 = s2;

for (size_t i = 0; i < n; i++) {
if (src1[i] != src2[i]) {
return (int)src1[i] - (int)src2[i];
}
}
return 0;
}

/**
* Copies n bytes from the memory area pointed to by src to the memory area
Expand All @@ -45,15 +55,33 @@ void *memmove(void *dest, const void *src, size_t n);
/**
* Returns the string length, excluding the terminating null character.
*/
size_t strlen(const char *s);
static inline size_t strlen(const char *s)
{
size_t len = 0;
while (*s++) {
len++;
}
return len;
}

/**
* Compares at most the first n characters in the strings s1 and s2 and
* returns 0 if all characters are the same or the numerical difference
* between the first mismatching character in s1 (interpreted as a signed
* value) and the corresponding character in s2.
*/
int strncmp(const char *s1, const char *s2, size_t n);
static inline int strncmp(const char *s1, const char *s2, size_t n)
{
for (size_t i = 0; i < n; i++) {
if (s1[i] != s2[i]) {
return (int)s1[i] - (int)s2[i];
}
if (s1[i] == '\0') {
return 0;
}
}
return 0;
}

/**
* Finds the first occurrence of the substring needle in the string haystack
Expand Down

0 comments on commit 3aeda70

Please sign in to comment.