2010-02-21 08:12:52 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Jonathan Westhues, Sept 2005
|
|
|
|
//
|
|
|
|
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
|
|
|
// at your option, any later version. See the LICENSE.txt file for the text of
|
|
|
|
// the license.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Common string.h functions
|
|
|
|
//-----------------------------------------------------------------------------
|
2010-02-21 08:10:28 +08:00
|
|
|
#include "string.h"
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void *memcpy(void *dest, const void *src, int len) {
|
2019-03-10 03:34:41 +08:00
|
|
|
uint8_t *d = dest;
|
|
|
|
const uint8_t *s = src;
|
2019-03-10 07:00:59 +08:00
|
|
|
while ((len--) > 0) {
|
2019-03-10 03:34:41 +08:00
|
|
|
*d = *s;
|
|
|
|
d++;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return dest;
|
2010-02-21 08:10:28 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void *memset(void *dest, int c, int len) {
|
2019-03-10 03:34:41 +08:00
|
|
|
uint8_t *d = dest;
|
2019-03-10 07:00:59 +08:00
|
|
|
while ((len--) > 0) {
|
2019-03-10 03:34:41 +08:00
|
|
|
*d = c;
|
|
|
|
d++;
|
|
|
|
}
|
|
|
|
return dest;
|
2010-02-21 08:10:28 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
int memcmp(const void *av, const void *bv, int len) {
|
2019-03-10 03:34:41 +08:00
|
|
|
const uint8_t *a = av;
|
|
|
|
const uint8_t *b = bv;
|
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
while ((len--) > 0) {
|
|
|
|
if (*a != *b) {
|
2019-03-10 03:34:41 +08:00
|
|
|
return *a - *b;
|
|
|
|
}
|
|
|
|
a++;
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
return 0;
|
2010-02-21 08:10:28 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void memxor(uint8_t *dest, uint8_t *src, size_t len) {
|
2019-03-10 07:00:59 +08:00
|
|
|
for (; len > 0; len--, dest++, src++)
|
|
|
|
*dest ^= *src;
|
2014-09-12 05:23:46 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
int strlen(const char *str) {
|
2019-04-08 17:40:52 +08:00
|
|
|
const char *p;
|
|
|
|
for (p = str; *p != '\0'; ++p) {
|
2019-03-10 03:34:41 +08:00
|
|
|
}
|
2019-04-08 17:40:52 +08:00
|
|
|
return p-str;
|
2010-02-21 08:10:28 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
char *strncat(char *dest, const char *src, unsigned int n) {
|
2019-03-10 03:34:41 +08:00
|
|
|
unsigned int dest_len = strlen(dest);
|
|
|
|
unsigned int i;
|
2010-02-21 08:10:28 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
for (i = 0 ; i < n && src[i] != '\0' ; i++)
|
|
|
|
dest[dest_len + i] = src[i];
|
|
|
|
dest[dest_len + i] = '\0';
|
2010-02-21 08:10:28 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
return dest;
|
2010-02-21 08:10:28 +08:00
|
|
|
}
|
2010-10-19 22:25:17 +08:00
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
char *strcat(char *dest, const char *src) {
|
2019-03-10 03:34:41 +08:00
|
|
|
unsigned int dest_len = strlen(dest);
|
|
|
|
unsigned int i;
|
2010-10-19 22:25:17 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
for (i = 0 ; src[i] != '\0' ; i++)
|
|
|
|
dest[dest_len + i] = src[i];
|
|
|
|
dest[dest_len + i] = '\0';
|
2010-10-19 22:25:17 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
return dest;
|
2010-10-19 22:25:17 +08:00
|
|
|
}
|
|
|
|
////////////////////////////////////////// code to do 'itoa'
|
|
|
|
|
|
|
|
/* reverse: reverse string s in place */
|
2019-03-10 18:20:22 +08:00
|
|
|
void strreverse(char s[]) {
|
2010-10-19 22:25:17 +08:00
|
|
|
int c, i, j;
|
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
|
2010-10-19 22:25:17 +08:00
|
|
|
c = s[i];
|
|
|
|
s[i] = s[j];
|
|
|
|
s[j] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* itoa: convert n to characters in s */
|
2019-03-10 18:20:22 +08:00
|
|
|
void itoa(int n, char s[]) {
|
2010-10-19 22:25:17 +08:00
|
|
|
int i, sign;
|
|
|
|
|
|
|
|
if ((sign = n) < 0) /* record sign */
|
|
|
|
n = -n; /* make n positive */
|
|
|
|
i = 0;
|
|
|
|
do { /* generate digits in reverse order */
|
|
|
|
s[i++] = n % 10 + '0'; /* get next digit */
|
|
|
|
} while ((n /= 10) > 0); /* delete it */
|
|
|
|
if (sign < 0)
|
|
|
|
s[i++] = '-';
|
|
|
|
s[i] = '\0';
|
|
|
|
strreverse(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////// END 'itoa' CODE
|