Move more of the simple string functions to the header, to allow inlining 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
This commit is contained in:
Lionel Debroux 2022-09-17 16:06:29 +02:00 committed by Sam Demeulemeester
parent b6992b9ec0
commit 3aeda70e24
2 changed files with 31 additions and 37 deletions

View File

@ -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;
@ -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);

View File

@ -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
@ -45,7 +55,14 @@ 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
@ -53,7 +70,18 @@ size_t strlen(const char *s);
* 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