lnm/src/lnm_utils.c

70 lines
1.2 KiB
C

#include <stdint.h>
#include <string.h>
#include "lnm/common.h"
bool lnm_strneq(const char *s1, size_t s1_len, const char *s2, size_t s2_len) {
return (s1_len == s2_len) && (memcmp(s1, s2, s1_len) == 0);
}
bool lnm_strnieq(const char *s1, size_t s1_len, const char *s2, size_t s2_len) {
bool equal = s1_len == s2_len;
for (size_t i = 0; i < s1_len && equal; i++) {
equal = s1[i] == s2[i] ||
(('a' <= s1[i]) && (s1[i] <= 'z') && (s1[i] - 32 == s2[i]));
}
return equal;
}
uint64_t lnm_ipow(uint64_t base, uint64_t power) {
uint64_t res = 1;
while (power > 0) {
res *= base;
power--;
}
return res;
}
uint64_t lnm_atoi(const char *s, size_t len) {
uint64_t res = 0;
for (size_t i = 0; i < len; i++) {
if (s[i] < '0' || '9' < s[i]) {
return 0;
}
uint64_t val = s[i] - '0';
res += val * lnm_ipow(10, (len - 1) - i);
}
return res;
}
uint64_t lnm_digits(uint64_t num) {
int digits = 1;
while (num > 9) {
digits++;
num /= 10;
}
return digits;
}
bool lnm_is_ascii(const char *s) {
bool valid = true;
while (valid && *s != '\0') {
valid = *s < 0;
s++;
}
return valid;
}