1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
#include <lib/string.h>
/**
* itoa: convert the integer i into a string, buf, in the base base.
* @i: the integer
* @buf: the string to store the result in
* @base: the numeric base to store it in
*/
void itoa(int i, char* buf, int base)
{
char* p = buf,
* p1 = NULL,
* p2 = NULL;
unsigned long ui = i;
if (base == 10 && i < 0) {
*p++ = '-';
buf++;
ui = -i;
}
/* Create a null-terminated string buf and store the string form of i in it */
do {
int tmp = ui % base;
*p++ = (tmp < 10) ? tmp + '0' : tmp + 'a' - 10;
} while (ui /= base);
*p = 0;
/* Reverse buf */
p1 = buf;
p2 = p - 1;
while (p1 < p2) {
char tmp = *p1;
*p1 = *p2;
*p2 = tmp;
p1++;
p2--;
}
}
void* memset(void* ptr, int value, size_t n)
{
char* p = (char*)ptr;
while (n--)
*p++ = value;
return p;
}
void* memcpy(void* dest, const void* src, size_t n)
{
while (n--)
*(char*)dest++ = *(char*)src++;
return dest;
}
size_t strlen(const char* str)
{
size_t len;
for (len = 0; *str; str++, len++);
return len;
}
int strncmp(const char* s1, const char* s2, size_t n)
{
while (*s1 && *s2 && n--) {
if (*s1 != *s2)
return *s1 - *s2;
s1++;
s2++;
}
return 0;
}
int strcmp(const char* s1, const char* s2)
{
while (*s1 && *s2) {
if (*s1 != *s2)
return *s1 - *s2;
s1++;
s2++;
}
return 0;
}
int memcmp(const void* p1, const void* p2, size_t n)
{
while (n--) {
if (*(char*)p1 != *(char*)p2)
return *(char*)p1 - *(char*)p2;
p1++;
p2++;
}
return 0;
}
char* strchr(const char* s, char c)
{
while (*s) {
if (*s == c)
return (char*)s;
s++;
}
return NULL;
}
char* strncpy(char* dest, const char* src, size_t n)
{
while (*src && n--)
*dest++ = *src++;
return dest;
}
char* strncat(char* dest, const char* src, size_t n)
{
dest += (strlen(dest) - 1);
while (*src && n--)
*dest++ = *src++;
return dest;
}
char* strcpy(char* dest, const char* src)
{
while (*src)
*dest++ = *src++;
return dest;
}
char* strcat(char* dest, const char* src)
{
dest += (strlen(dest) - 1);
while (*src)
*dest++ = *src++;
return dest;
}
|