86 lines
No EOL
1.8 KiB
C
86 lines
No EOL
1.8 KiB
C
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <knob/heap.h>
|
|
|
|
//unsophisticated, should copy by dwords where available
|
|
void blockcpy(void *to, const void *from, uint32_t size) {
|
|
for (uint32_t i = 0; i < size; ++i)
|
|
*(uint8_t *)(to++) = *(const uint8_t *)(from++);
|
|
}
|
|
|
|
//unsophisticated, should check by dwords wheere available
|
|
__attribute__ ((__pure__))
|
|
bool blockequ(const void *a, const void *b, uint32_t size) {
|
|
for (uint32_t i = 0; i < size; ++i)
|
|
if (*(uint8_t *)(a++) != *(uint8_t *)(b++))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
//returns length without null-terminator
|
|
uint32_t strcpy(char *to, const char *from) {
|
|
uint32_t i = 0;
|
|
do
|
|
to[i] = from[i];
|
|
while (from[i++]);
|
|
return i - 1;
|
|
}
|
|
|
|
char *strdup(const char *from) {
|
|
const char *end = from;
|
|
while (*(end++))
|
|
;
|
|
char *buf = get_block(end - from);
|
|
blockcpy(buf, from, end - from);
|
|
return buf;
|
|
}
|
|
|
|
__attribute__ ((pure))
|
|
bool strequ(const char *a, const char *b) {
|
|
while (true) {
|
|
if ((*a == '\0') != (*b == '\0'))
|
|
return false;
|
|
if (*a == '\0')
|
|
return true;
|
|
if (*a != *b)
|
|
return false;
|
|
++a;
|
|
++b;
|
|
}
|
|
}
|
|
|
|
__attribute__ ((pure))
|
|
uint32_t strlen(const char *str) {
|
|
uint32_t len = 0;
|
|
while (*str) {
|
|
++len;
|
|
++str;
|
|
}
|
|
return len;
|
|
}
|
|
|
|
void str_trunc_fill(char *str, uint32_t len) {
|
|
const uint8_t orig_len = strlen(str);
|
|
if (orig_len > len) {
|
|
str[len - 4] = ' ';
|
|
str[len - 3] = '.';
|
|
str[len - 2] = '.';
|
|
str[len - 1] = '.';
|
|
str[len] = '\0';
|
|
}
|
|
else if (orig_len != len) {
|
|
for (uint8_t j = orig_len; j < len; ++j)
|
|
str[j] = ' ';
|
|
str[len] = '\0';
|
|
}
|
|
}
|
|
|
|
__attribute__ ((pure))
|
|
uint32_t str_find_any(const char *str, const char *delims) {
|
|
const char *i;
|
|
for (i = str; *i; ++i)
|
|
for (const char *j = delims; *j; ++j)
|
|
if (*i == *j)
|
|
return i - str;
|
|
return i - str;
|
|
} |