Move string functions to libstring
All checks were successful
Build documentation / build-and-deploy (push) Successful in 27s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 27s
This commit is contained in:
1
libstring/.gitignore
vendored
Normal file
1
libstring/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.o
|
||||
28
libstring/Makefile
Normal file
28
libstring/Makefile
Normal file
@@ -0,0 +1,28 @@
|
||||
cc := clang
|
||||
o :=
|
||||
c :=
|
||||
cflags := -isystem . -isystem ../libmsl
|
||||
buildtype ?= release
|
||||
|
||||
include src.mk
|
||||
include ../generic/flags.mk
|
||||
include ../$(platform)/flags.mk
|
||||
|
||||
all: build/libstring.a
|
||||
|
||||
build/libstring.a: $(o)
|
||||
llvm-ar rcs $@ $^
|
||||
|
||||
%.o: %.c
|
||||
$(cc) -c -o $@ $(cflags) $<
|
||||
|
||||
%.o: %.S
|
||||
$(cc) -c -o $@ $(cflags) $<
|
||||
|
||||
clean:
|
||||
rm -f $(o) build/libstring.a
|
||||
|
||||
format:
|
||||
clang-format -i $$(git ls-files '*.c' '*.h')
|
||||
|
||||
.PHONY: all clean format
|
||||
3
libstring/src.mk
Normal file
3
libstring/src.mk
Normal file
@@ -0,0 +1,3 @@
|
||||
c += string.c
|
||||
|
||||
o += string.o
|
||||
47
libstring/string.c
Normal file
47
libstring/string.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
size_t memset (void* dst, uint8_t b, size_t n) {
|
||||
uint8_t* dst1 = dst;
|
||||
size_t i;
|
||||
for (i = 0; i < n; i++)
|
||||
dst1[i] = b;
|
||||
return i;
|
||||
}
|
||||
|
||||
size_t memcpy (void* dst, const void* src, size_t n) {
|
||||
uint8_t* dst1 = dst;
|
||||
const uint8_t* src1 = src;
|
||||
size_t i;
|
||||
for (i = 0; i < n; i++)
|
||||
dst1[i] = src1[i];
|
||||
return i;
|
||||
}
|
||||
|
||||
// SOURCE: https://stackoverflow.com/a/48967408
|
||||
void strncpy (char* dst, const char* src, size_t n) {
|
||||
size_t i = 0;
|
||||
while (i++ != n && (*dst++ = *src++))
|
||||
;
|
||||
}
|
||||
|
||||
size_t strlen (const char* str) {
|
||||
const char* s;
|
||||
for (s = str; *s; ++s)
|
||||
;
|
||||
return (s - str);
|
||||
}
|
||||
|
||||
int memcmp (const void* s1, const void* s2, size_t n) {
|
||||
unsigned char* p = (unsigned char*)s1;
|
||||
unsigned char* q = (unsigned char*)s2;
|
||||
|
||||
while (n--) {
|
||||
if (*p != *q) {
|
||||
return (int)*p - (int)*q;
|
||||
}
|
||||
p++, q++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
13
libstring/string.h
Normal file
13
libstring/string.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef _LIBSTRING_STRING_H
|
||||
#define _LIBSTRING_STRING_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
size_t memset (void* dst, uint8_t b, size_t n);
|
||||
size_t memcpy (void* dst, const void* src, size_t n);
|
||||
void strncpy (char* dst, const char* src, size_t n);
|
||||
size_t strlen (const char* str);
|
||||
int memcmp (const void* s1, const void* s2, size_t n);
|
||||
|
||||
#endif // _LIBSTRING_STRING_H
|
||||
Reference in New Issue
Block a user