memtest86plus/lib/string.c
Lionel Debroux 3aeda70e24 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
2023-01-23 15:49:45 +01:00

95 lines
2.0 KiB
C

// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2020 Martin Whitaker.
//
// Derived from an extract of memtest86+ lib.c:
//
// lib.c - MemTest-86 Version 3.4
//
// Released under version 2 of the Gnu Public License.
// By Chris Brady
#include <stddef.h>
#include "string.h"
//------------------------------------------------------------------------------
// Private Functions
//------------------------------------------------------------------------------
void reverse(char s[])
{
int i, j;
char c;
for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
//------------------------------------------------------------------------------
// Public Functions
//------------------------------------------------------------------------------
void *memmove(void *dest, const void *src, size_t n)
{
char *d = (char *)dest, *s = (char *)src;
if (n > 0) {
if (dest < src) {
for (size_t i = 0; i < n; i++) {
d[i] = s[i];
}
}
if (dest > src) {
size_t i = n;
do {
i--;
d[i] = s[i];
} while (i > 0);
}
}
return dest;
}
char *strstr(const char *haystack, const char *needle)
{
size_t haystack_len = strlen(haystack);
size_t needle_len = strlen(needle);
size_t max_idx = haystack_len - needle_len;
for (size_t idx = 0; idx <= max_idx; idx++) {
if (memcmp(haystack + idx, needle, needle_len) == 0) {
return (char *)haystack + idx;
}
}
return NULL;
}
char *itoa(int num, char *str)
{
int i = 0;
/* Special case for 0 */
if (num == 0) {
str[i++] = '0';
str[i] = '\0';
return str;
}
// Parse digits
while (num != 0) {
int rem = num % 10;
str[i++] = (rem > 9) ? (rem-10) + 'a' : rem + '0';
num /= 10;
}
// Last is string terminator
str[i] = '\0';
reverse(str);
return str;
}