CoolPotOS/apps/libs/string.c

75 lines
1.3 KiB
C

#include "../include/string.h"
int isspace(int c) {
return (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' ||
c == '\v');
}
// isdigit
int isdigit(int c) {
return (c >= '0' && c <= '9');
}
// isalpha
int isalpha(int c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
// isupper
int isupper(int c) {
return (c >= 'A' && c <= 'Z');
}
size_t strnlen(const char *s, size_t maxlen) {
const char *es = s;
while (*es && maxlen) {
es++;
maxlen--;
}
return (es - s);
}
size_t strlen(const char *str) {
size_t len = 0;
while (str[len])
len++;
return len;
}
int strcmp(const char *s1, const char *s2) {
char is_equal = 1;
for (; (*s1 != '\0') && (*s2 != '\0'); s1++, s2++) {
if (*s1 != *s2) {
is_equal = 0;
break;
}
}
if (is_equal) {
if (*s1 != '\0') {
return 1;
} else if (*s2 != '\0') {
return -1;
} else {
return 0;
}
} else {
return (int) (*s1 - *s2);
}
}
char *strcpy(char *dest, const char *src) {
do {
*dest++ = *src++;
} while (*src != 0);
*dest = 0;
}
char *strcat(char *dest, const char *src) {
char *temp = dest;
while (*temp != '\0')
temp++;
while ((*temp++ = *src++) != '\0');
}