Skip to content

Instantly share code, notes, and snippets.

@untodesu
Created January 1, 2020 14:43
Show Gist options
  • Save untodesu/a42b79b0887a5c21e7b25a1ea232e86c to your computer and use it in GitHub Desktop.
Save untodesu/a42b79b0887a5c21e7b25a1ea232e86c to your computer and use it in GitHub Desktop.
//=======================================================================
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//=======================================================================
#include <libcommon/common.h>
int C_strlen(const char *s)
{
int i = 0;
while(*(++i + s));
return i;
}
char * C_strcpy(char *dst, int dst_size, const char *src)
{
const int src_size = C_strlen(src);
for(int i = 0; (i < src_size) && (i < dst_size); i++) {
*(dst + i) = *(src + i);
}
return dst;
}
int C_strcmp(const char *s1, const char *s2)
{
while(*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *s1 - *s2;
}
void * C_memset(void *dst, int fill, int count)
{
for(int i = 0; i < count; i++) {
*((unsigned char *)dst + i) = (unsigned char)fill;
}
return dst;
}
void * C_memcpy(void *dst, int dst_size, void *src, int src_size)
{
unsigned char *dst_ch = (unsigned char *)dst;
unsigned char *src_ch = (unsigned char *)src;
for(int i = 0; (i < dst_size) && (i < src_size); i++) {
*(dst_ch + i) = *(src_ch + i);
}
return dst;
}
int C_memcmp(void *p1, int p1_size, void *p2, int p2_size)
{
unsigned char *p1_ch = (unsigned char *)p1;
unsigned char *p2_ch = (unsigned char *)p2;
for(int i = 0; (i < p1_size) && (i < p2_size); i++) {
if(*(p1_ch + i) != *(p2_ch + i)) {
return (*(p1_ch + i) - *(p2_ch + i));
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment