2019-07-03 12:15:40 +02:00
|
|
|
#include "string.h"
|
|
|
|
|
|
|
|
// C library implementation
|
|
|
|
|
2019-07-03 21:04:05 +02:00
|
|
|
int32_t strcmp(const uint8_t *s1, const uint8_t *s2) {
|
2019-07-03 12:15:40 +02:00
|
|
|
while ((*s1) && (*s1 == *s2)) {
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
return (*(uint8_t*)s1 - *(uint8_t*)s2);
|
|
|
|
}
|
2019-07-03 21:04:05 +02:00
|
|
|
uint8_t *itoa(int32_t val, uint8_t *buf, uint32_t radix) {
|
2019-07-03 12:15:40 +02:00
|
|
|
uint32_t i = 0;
|
|
|
|
uint32_t start = i;
|
|
|
|
|
|
|
|
if(val < 0 && radix == 10) {
|
|
|
|
buf[i++] = '-';
|
|
|
|
start = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(radix == 10) {
|
|
|
|
buf[i++] = '0';
|
|
|
|
buf[i++] = 'x';
|
|
|
|
start = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
int x = val;
|
|
|
|
do {
|
|
|
|
int a = x % radix;
|
|
|
|
if(a < 10)
|
|
|
|
buf[i++] = a + '0';
|
|
|
|
else
|
|
|
|
buf[i++]= a + 'a' - 10;
|
|
|
|
} while(x /= radix);
|
|
|
|
|
2019-07-03 21:04:05 +02:00
|
|
|
uint8_t *s = buf+start;
|
|
|
|
uint8_t *e = buf+(i-1);
|
2019-07-03 12:15:40 +02:00
|
|
|
|
|
|
|
while(s < e) {
|
2019-07-03 21:04:05 +02:00
|
|
|
uint8_t t = *s;
|
2019-07-03 12:15:40 +02:00
|
|
|
*s = *e;
|
|
|
|
*e = t;
|
|
|
|
i++;
|
|
|
|
e--;
|
|
|
|
}
|
2019-07-03 21:04:05 +02:00
|
|
|
buf[i] = 0;
|
|
|
|
return buf;
|
2019-07-03 12:15:40 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 21:04:05 +02:00
|
|
|
uint8_t *uitoa(uint32_t val, uint8_t *buf, uint32_t radix) {
|
2019-07-03 12:15:40 +02:00
|
|
|
uint32_t i = 0;
|
|
|
|
uint32_t start = i;
|
|
|
|
uint32_t x = val;
|
|
|
|
|
2019-07-08 12:01:01 +02:00
|
|
|
if(radix == 16) {
|
2019-07-03 12:15:40 +02:00
|
|
|
buf[i++] = '0';
|
|
|
|
buf[i++] = 'x';
|
|
|
|
start = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
uint32_t a = x % radix;
|
|
|
|
if(a < 10)
|
|
|
|
buf[i++] = a + '0';
|
|
|
|
else
|
|
|
|
buf[i++] = a + 'a' - 10;
|
|
|
|
} while(x /= radix);
|
|
|
|
|
2019-07-03 21:04:05 +02:00
|
|
|
uint8_t *s = buf+start;
|
2019-07-08 12:01:01 +02:00
|
|
|
uint8_t *e = buf+(i-1);
|
2019-07-03 12:15:40 +02:00
|
|
|
|
|
|
|
while(s < e) {
|
2019-07-03 21:04:05 +02:00
|
|
|
uint8_t t = *s;
|
2019-07-03 12:37:16 +02:00
|
|
|
*s = *e;
|
2019-07-03 12:15:40 +02:00
|
|
|
*e = t;
|
|
|
|
s++;
|
|
|
|
e--;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[i] = 0;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2019-07-03 21:04:05 +02:00
|
|
|
size_t strlen(const uint8_t *buf) {
|
2019-07-03 12:15:40 +02:00
|
|
|
unsigned int i = 0;
|
|
|
|
while(buf[i] != 0)
|
|
|
|
i++;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Worst memset implementation
|
|
|
|
* i could find on the net.
|
|
|
|
* however it works so... */
|
2019-07-03 21:04:05 +02:00
|
|
|
void *memset(void *s, uint32_t c, size_t n) {
|
2019-07-03 12:15:40 +02:00
|
|
|
char *mem = (char*)s;
|
|
|
|
|
|
|
|
for(size_t i = 0; i < n; i++)
|
|
|
|
mem[i] = (uint8_t)c;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *memmove(void *dst, const void *src, size_t len) {
|
|
|
|
char *dstmem = (char*)dst;
|
|
|
|
char *srcmem = (char*)src;
|
|
|
|
|
|
|
|
for(size_t i = 0; i < len; i++)
|
|
|
|
dstmem[i] = srcmem[i];
|
|
|
|
|
|
|
|
return dstmem;
|
|
|
|
}
|
|
|
|
|
2019-07-03 21:04:05 +02:00
|
|
|
void strupper(uint8_t *str) {
|
2019-07-03 12:15:40 +02:00
|
|
|
for(unsigned int i = 0; i < strlen(str); i++) {
|
|
|
|
if(str[i] == 'a' && str[i] < 'z')
|
|
|
|
str[i] &= 0x4F;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 21:04:05 +02:00
|
|
|
void strlower(uint8_t *str) {
|
2019-07-03 12:15:40 +02:00
|
|
|
for(unsigned int i = 0; i < strlen(str); i++) {
|
|
|
|
if(str[i] == 'A' && str[i] < 'Z')
|
|
|
|
str[i] |= 0x60;
|
|
|
|
}
|
|
|
|
}
|