2021-07-05 05:44:58 +00:00
|
|
|
#include <u.h>
|
2021-07-08 05:37:18 +00:00
|
|
|
#include <libc.h>
|
|
|
|
#include <stdio.h>
|
2021-07-05 05:44:58 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2021-07-08 05:37:18 +00:00
|
|
|
uvlong hash(char* str, int array_sz) {
|
2021-07-05 05:44:58 +00:00
|
|
|
uvlong h;
|
|
|
|
uchar* p;
|
|
|
|
|
|
|
|
h = 0;
|
|
|
|
for (p = (unsigned char*)str; *p != '\0'; p++)
|
|
|
|
h = 37 * h + *p;
|
2021-07-08 05:37:18 +00:00
|
|
|
if (array_sz == 0)
|
|
|
|
return h;
|
|
|
|
else
|
|
|
|
return h % array_sz;
|
2021-07-05 05:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char clca(char c) {
|
|
|
|
return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c;
|
|
|
|
} /* char to lowercase */
|
|
|
|
|
|
|
|
char cuca(char c) {
|
|
|
|
return c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c;
|
|
|
|
} /* char to uppercase */
|
|
|
|
|
|
|
|
int slen(char* s) {
|
|
|
|
int i = 0;
|
|
|
|
while (s[i] && s[++i]) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
} /* string length */
|
|
|
|
|
|
|
|
char* st__(char* s, char (*fn)(char)) {
|
|
|
|
int i = 0;
|
|
|
|
char c;
|
|
|
|
while ((c = s[i]))
|
|
|
|
s[i++] = fn(c);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
char* stuc(char* s) { return st__(s, cuca); } /* string to uppercase */
|
|
|
|
|
|
|
|
char* stlc(char* s) { return st__(s, clca); } /* string to lowercase */
|
|
|
|
|
|
|
|
char* scpy(char* src, char* dst, int len) {
|
|
|
|
int i = 0;
|
|
|
|
while ((dst[i] = src[i]) && i < len - 2)
|
|
|
|
i++;
|
|
|
|
dst[i + 1] = '\0';
|
|
|
|
return dst;
|
|
|
|
} /* string copy */
|
|
|
|
|
|
|
|
int scmp(char* a, char* b) {
|
|
|
|
int i = 0;
|
|
|
|
while (a[i] == b[i])
|
|
|
|
if (!a[i++])
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
} /* string compare */
|
|
|
|
|
|
|
|
char* scsw(char* s, char a, char b) {
|
|
|
|
int i = 0;
|
|
|
|
char c;
|
|
|
|
while ((c = s[i]))
|
|
|
|
s[i++] = c == a ? b : c;
|
|
|
|
return s;
|
|
|
|
} /* string char swap */
|
|
|
|
|
|
|
|
char* scat(char* dst, const char* src) {
|
|
|
|
char* ptr = dst + slen(dst);
|
|
|
|
while (*src)
|
|
|
|
*ptr++ = *src++;
|
|
|
|
*ptr = '\0';
|
|
|
|
return dst;
|
|
|
|
} /* string cat */
|
|
|
|
|
|
|
|
int ssin(char* s, char* ss) {
|
|
|
|
int a = 0, b = 0;
|
|
|
|
while (s[a]) {
|
|
|
|
if (s[a] == ss[b]) {
|
|
|
|
if (!ss[b + 1])
|
|
|
|
return a - b;
|
|
|
|
b++;
|
|
|
|
} else
|
|
|
|
b = 0;
|
|
|
|
a++;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
} /* string substring index */
|
|
|
|
|
|
|
|
char* ccat(char* dst, char c) {
|
|
|
|
int len = slen(dst);
|
|
|
|
dst[len] = c;
|
|
|
|
dst[len + 1] = '\0';
|
|
|
|
return dst;
|
|
|
|
}
|
2021-07-08 05:37:18 +00:00
|
|
|
|
|
|
|
char* read_bytes(char* path) {
|
|
|
|
FILE* f;
|
|
|
|
char* buf;
|
|
|
|
long count;
|
|
|
|
|
|
|
|
f = fopen(path, "rb");
|
|
|
|
if (f == nil)
|
|
|
|
return nil;
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
count = ftell(f);
|
|
|
|
rewind(f);
|
|
|
|
|
|
|
|
buf = (char*)malloc(count * sizeof(char));
|
|
|
|
if (!fread(buf, count, 1, f)) {
|
|
|
|
fclose(f);
|
|
|
|
return nil;
|
|
|
|
} else {
|
|
|
|
fclose(f);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char* read_chars(char* path) {
|
|
|
|
FILE* f;
|
|
|
|
char* buf;
|
|
|
|
long count;
|
|
|
|
|
|
|
|
f = fopen(path, "r");
|
|
|
|
if (f == nil)
|
|
|
|
return nil;
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
count = ftell(f);
|
|
|
|
rewind(f);
|
|
|
|
|
|
|
|
buf = (char*)malloc(count * sizeof(char));
|
|
|
|
if (!fread(buf, count, 1, f)) {
|
|
|
|
fclose(f);
|
|
|
|
return nil;
|
|
|
|
} else {
|
|
|
|
fclose(f);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|