Skip to content

Instantly share code, notes, and snippets.

@untodesu
Last active January 3, 2020 21:09
Show Gist options
  • Save untodesu/b2f8288a0b16092dbe1015168aa890af to your computer and use it in GitHub Desktop.
Save untodesu/b2f8288a0b16092dbe1015168aa890af to your computer and use it in GitHub Desktop.
v//==============================================================================
// Copyright (c) 2020, undbsd
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//==============================================================================
#include <libcommon/common.h>
void C_memset(void *dst, int dsize, int fill, int count)
{
register char *s = dst;
count = (count > dsize) ? dsize : count; //Max
while(count--) {
*s++ = (char)fill;
}
}
void C_memcpy(void *dst, int dsize, void *src, int ssize)
{
register char *dp = dst;
register char *sp = src;
int i = (dsize > ssize) ? ssize : dsize; //Min
while(i--) {
*dp++ = *sp++;
}
}
int C_memcmp(void *a, int asize, void *b, int bsize)
{
register char *ca = a;
register char *cb = b;
while(asize-- && bsize--) {
if(*ca != *cb) {
return (*ca - *cb);
}
ca++;
cb++;
}
return 0;
}
int C_strlen(const char *s)
{
int c = 0;
while(*s++) {
c++;
}
return c;
}
void C_strcpy(char *dst, int dsize, const char *src)
{
while(*src && dsize--) {
*dst++ = *src++;
}
if(dsize) {
//The null-terminator
*dst++ = '\0';
}
}
int C_strcmp(const char *a, const char *b)
{
while(*a && (*a == *b)) {
a++;
b++;
}
return (*a - *b);
}
char * C_strchr(const char *s, char c)
{
while(*s) {
if(*s == c) {
return (char *)s;
}
s++;
}
return null;
}
bool C_isdigit(char c)
{
return (c >= '0' && c <= '9');
}
bool C_ishex(char c)
{
return ((c >= '0' && c <= '9') || // 0..9
(c >= 'a' && c <= 'f') || // a..f
(c >= 'A' && c <= 'F')); // A..F
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment