common/string.c (view raw)
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
#include <stdint.h>
#include "string.h"
void *memcpy (void *destination, const void *source, size_t num) {
if (destination >= source) {
for (uint32_t i = 0; i < num; i++) {
((uint8_t*)destination)[num - 1 - i] = ((uint8_t*)source)[num - 1 - i];
}
} else {
for (uint32_t i = 0; i < num; i++) {
((uint8_t*)destination)[i] = ((uint8_t*)source)[i];
}
}
return destination;
}
char *strcpy(char *destination, const char *source) {
int i = 0;
while (source[i]) {
destination[i] = source[i];
i++;
}
destination[i] = 0;
return destination;
}
void *memset (void *ptr, int value, size_t num) {
for (uint32_t i = 0; i < num; i++) {
((uint8_t*)ptr)[i] = (uint8_t)value;
}
return ptr;
}
unsigned int strlen (const char *str) {
int i = 0;
while (str[i]) { i++; }
return i;
}
int strcmp(const char *str1, const char *str2) {
int i = 0;
while (1) {
if (str1[i] != str2[i]) {
return str1[i] - str2[i];
}
if (str1[i] == 0) {
return 0;
}
i++;
}
}
const char * strchr ( const char * str, int character ) {
int i = 0;
while (1) {
if (str[i] == 0) {
return NULL;
}
if (str[i] == character) {
return &(str[i]);
}
i++;
}
}
/**
* Creates a copy of the string @p src into buffer @p dest with whitespace
* stripped from the left side. Truncates the string if the buffer is too small.
*
* @param src Source string to be copied
* @param dest Destination buffer to copy the stripped string into
* @param bufflen Length of buffer dest
*
* @return Length of the resulting string
*/
size_t strlstrip(char *dest, const char *src, size_t bufflen) {
/* skip whitespace at the start */
while (WHITESPACE(*src)) {
src++;
}
/* copy until string stops */
size_t i = 0;
for (; src[i] != 0 && i < bufflen; i++) {
dest[i] = src[i];
}
dest[i] = 0;
return i;
}
/**
* Creates a copy of the string @p src into buffer @p dest with whitespace
* stripped from the right side.
*
* @param src Source string to be copied
* @param dest Destination buffer to copy the stripped string into
* @param srclen Length of the original string
*
* @return Length of the resulting string
*/
size_t strrstrip(char *dest, const char *src, size_t srclen) {
/* skip whitespace at the end */
while (srclen > 0 && WHITESPACE(src[srclen - 1])) {
srclen--;
}
/* copy until string stops */
size_t i = 0;
for (; src[i] != 0 && i < srclen; i++) {
dest[i] = src[i];
}
dest[i] = 0;
return i;
}
/**
* Compares two strings in a case-sensitive manner.
*
* @param str1 String to be compared
* @param str2 String to be compared
*
* @return 0 if both strings are equal,
* <0 if upper(str1) < upper(str2),
* >0 if upper(str1) > upper(str2)
*/
int sstrcmp(const char *str1, const char *str2) {
int i = 0;
while (1) {
if (TO_UPPER(str1[i]) != TO_UPPER(str2[i])) {
return TO_UPPER(str1[i]) - TO_UPPER(str2[i]);
}
if (str1[i] == 0) {
return 0;
}
i++;
}
}