jody_string.c 1.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/*
 * Jody Bruchon's string function library  <jody@jodybruchon.com>
 * Copyright (C) 2015-2017
 * Distributed under the GNU General Public License version 2
 */

#include <stdint.h>
#include <unistd.h>

/* Like strncasecmp() but only tests for equality */
extern int strncaseeq(const char *s1, const char *s2, size_t len)
{
	size_t i = 0;

	while (i < len) {
		if (*s1 != *s2) {
			unsigned char c1, c2;

			c1 = *(const unsigned char *)s1;
			c2 = *(const unsigned char *)s2;
			/* Transform upper case to lower case */
			if (c1 == 0 || c2 == 0) return 1;
			if (c1 >= 'A' && c1 <= 'Z') c1 |= 0x20;
			if (c2 >= 'A' && c2 <= 'Z') c2 |= 0x20;
			if (c1 != c2) return 1;
		} else {
			if (*s1 == 0) return 0;
		}
		s1++; s2++;
		i++;
	}
	return 0;
}

/* Like strcasecmp() but only tests for equality */
extern int strcaseeq(const char *s1, const char *s2)
{
	while (1) {
		if (*s1 != *s2) {
			unsigned char c1, c2;

			c1 = *(const unsigned char *)s1;
			c2 = *(const unsigned char *)s2;
			/* Transform upper case to lower case */
			if (c1 == 0 || c2 == 0) return 1;
			if (c1 >= 'A' && c1 <= 'Z') c1 |= 0x20;
			if (c2 >= 'A' && c2 <= 'Z') c2 |= 0x20;
			if (c1 != c2) return 1;
		} else {
			if (*s1 == 0) return 0;
		}
		s1++; s2++;
	}
	return 1;
}


/* Like strncmp() but only tests for equality */
extern int strneq(const char *s1, const char *s2, size_t len)
{
	size_t i = 0;

	if (!len) return 0;

	while (*s1 != '\0' && *s2 != '\0') {
		if (*s1 != *s2) return 1;
		s1++; s2++; i++;
		if (i == len) return 0;
	}
	if (*s1 != *s2) return 1;
	return 0;
}


/* Like strcmp() but only tests for equality */
extern int streq(const char *s1, const char *s2)
{
	while (*s1 != '\0' && *s2 != '\0') {
		if (*s1 != *s2) return 1;
		s1++; s2++;
	}
	if (*s1 != *s2) return 1;
	return 0;
}