XXIIVV

A collection of helpers to do specific operations in ANSI C.

Instead of including entire libraries like stdlib.h, string.h and ctype.h, one might prefer to manually select only the handful of functions that be used in a given project.

To see an example of these basic helpers used into projects, have a look at Oscean, Moogle, or Parade.

Integer maximum

int
imax(int a, int b)
{
	return a > b ? a : b;
}

Integer minimum

int
imin(int a, int b)
{
	return a < b ? a : b;
}

Character is alpha

int
cial(char c)
{
	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}

Character is number

int
cinu(char c)
{
	return c >= '0' && c <= '9';
}

Character is space

int
cisp(char c)
{
	return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}

Character is alphanumericspace

int
cans(char c)
{
	return cial(c) || cinu(c) || cisp(c);
}

Character to lowercase

int
clca(int c)
{
	return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c;
}

Character to uppercase

int
cuca(char c)
{
	return c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c;
}

Character padding

int
cpad(char* s, char c)
{
	int i = 0;
	while(s[i] == c && s[i] != '\0' && s[++i])
		;
	return i;
}

Character position

int
cpos(char* s, char c)
{
	int i = 0;
	while(s[i] != '\0' && s[i])
		if(s[i++] == c)
			return i - 1;
	return -1;
}

String length

int
slen(char* s)
{
	int n = 0;
	while(s[n] != '\0' && s[++n])
		;
	return n;
}

String from character reverse

char*
spor(char* s, int c)
{
	int i;
	for(i = slen(s); i >= 0; --i)
		if(s[i] == c)
			return s + i;
	return s - 1;
}

String to uppercase

char*
suca(char* s)
{
	int i;
	for(i = 0; i < slen(s); i++)
		s[i] = cuca(s[i]);
	return s;
}

String to lowercase

char*
slca(char* s)
{
	int i;
	for(i = 0; i < slen(s); i++)
		s[i] = clca(s[i]);
	return s;
}

String character swap

char*
scsw(char* s, char a, char b)
{
	int i;
	for(i = 0; i < slen(s); i++)
		s[i] = s[i] == a ? b : s[i];
	return s;
}

String compare

int
scmp(char* a, char* b)
{
	int i, l = slen(a);
	if(l != slen(b))
		return 0;
	for(i = 0; i < l; ++i)
		if(a[i] != b[i])
			return 0;
	return 1;
}

String is alphanumericspace

int
sans(char* s)
{
	int i;
	for(i = 0; i < slen(s); i++)
		if(!cans(s[i]))
			return 0;
	return 1;
}

String trim

char*
strm(char* s)
{
	char* end;
	while(cisp(*s))
		s++;
	if(*s == 0)
		return s;
	end = s + slen(s) - 1;
	while(end > s && cisp(*end))
		end--;
	end[1] = '\0';
	return s;
}

String position in string

int
spos(char* a, char* b)
{
	int i, j, alen = slen(a), blen = slen(b);
	for(i = 0; i < alen; i++) {
		for(j = 0; j < blen; j++) {
			if(a[i + j] == '\0')
				return -1;
			if(a[i + j] != b[j])
				break;
			if(j == blen - 1)
				return i;
		}
	}
	return -1;
}

String to int

int
sint(char *s)
{
	int i = 0, num = 0;
	while(s[i] && cinu(s[i]))
		num = num * 10 + (s[i++] - '0');
	return num;
}

String is url

int
surl(char* s)
{
	return spos(s, "://") >= 0 || spos(s, "./") >= 0;
}

String copy

char*
scpy(char* src, char* dest)
{
	int i = 0;
	while((dest[i] = src[i]) != '\0')
		i++;
	return dest;
}

String substring

char*
sstr(char* src, char* dest, int from, int to)
{
	int i;
	char *a = (char*)src + from, *b = (char*)dest;
	for(i = 0; i < to; i++)
		b[i] = a[i];
	dest[to] = '\0';
	return dest;
}

Array find string

int
afnd(char* src[], int len, char* val)
{
	int i;
	for(i = 0; i < len; i++)
		if(scmp(src[i], val))
			return i;
	return -1;
}

String cat

char*
scat(char* dest, const char* src)
{
	char* ptr = dest + slen(dest);
	while(*src != '\0')
		*ptr++ = *src++;
	*ptr = '\0';
	return dest;
}

Character to hex value

unsigned char
chex(char c)
{
	if(c >= 'a' && c <= 'f')
		return 10 + c - 'a';
	if(c >= 'A' && c <= 'F')
		return 10 + c - 'A';
	return (c - '0') & 0xF;
}

String to hex value

unsigned short
shex(char* s)
{
	int i, n = 0, l = slen(s);
	for(i = 0; i < l; ++i)
		n |= (chex(s[i]) << ((l - i - 1) * 4));
	return n;
}

Date

Leap Year

int
leapyear(int y)
{
	if(y % 400 == 0)
		return 1;
	else if(y % 100 == 0)
		return 0;
	else if(y % 4 == 0)
		return 1;
	return 0;
}