Loading...
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | /* string.c - common string routines */
/*
* Copyright (c) 2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
/**
*
* @brief Copy a string
*
* @return pointer to destination buffer <d>
*/
char *strcpy(char *_MLIBC_RESTRICT d, const char *_MLIBC_RESTRICT s)
{
char *dest = d;
while (*s != '\0') {
*d = *s;
d++;
s++;
}
*d = '\0';
return dest;
}
/**
*
* @brief Copy part of a string
*
* @return pointer to destination buffer <d>
*/
char *strncpy(char *_MLIBC_RESTRICT d, const char *_MLIBC_RESTRICT s, size_t n)
{
char *dest = d;
while ((n > 0) && *s != '\0') {
*d = *s;
s++;
d++;
n--;
}
while (n > 0) {
*d = '\0';
d++;
n--;
}
return dest;
}
/**
*
* @brief String scanning operation
*
* @return pointer to 1st instance of found byte, or NULL if not found
*/
char *strchr(const char *s, int c)
{
char tmp = (char) c;
while ((*s != tmp) && (*s != '\0'))
s++;
return (*s == tmp) ? (char *) s : NULL;
}
/**
*
* @brief String scanning operation
*
* @return pointer to last instance of found byte, or NULL if not found
*/
char *strrchr(const char *s, int c)
{
char *match = NULL;
do {
if (*s == (char)c) {
match = (char *)s;
}
} while (*s++);
return match;
}
/**
*
* @brief Get string length
*
* @return number of bytes in string <s>
*/
size_t strlen(const char *s)
{
size_t n = 0;
while (*s != '\0') {
s++;
n++;
}
return n;
}
/**
*
* @brief Compare two strings
*
* @return negative # if <s1> < <s2>, 0 if <s1> == <s2>, else positive #
*/
int strcmp(const char *s1, const char *s2)
{
while ((*s1 == *s2) && (*s1 != '\0')) {
s1++;
s2++;
}
return *s1 - *s2;
}
/**
*
* @brief Compare part of two strings
*
* @return negative # if <s1> < <s2>, 0 if <s1> == <s2>, else positive #
*/
int strncmp(const char *s1, const char *s2, size_t n)
{
while ((n > 0) && (*s1 == *s2) && (*s1 != '\0')) {
s1++;
s2++;
n--;
}
return (n == 0) ? 0 : (*s1 - *s2);
}
char *strcat(char *_MLIBC_RESTRICT dest, const char *_MLIBC_RESTRICT src)
{
strcpy(dest + strlen(dest), src);
return dest;
}
char *strncat(char *_MLIBC_RESTRICT dest, const char *_MLIBC_RESTRICT src,
size_t n)
{
char *orig_dest = dest;
size_t len = strlen(dest);
dest += len;
while ((n-- > 0) && (*src != '\0')) {
*dest++ = *src++;
}
*dest = '\0';
return orig_dest;
}
/**
*
* @brief Compare two memory areas
*
* @return negative # if <m1> < <m2>, 0 if <m1> == <m2>, else positive #
*/
int memcmp(const void *m1, const void *m2, size_t n)
{
const char *c1 = m1;
const char *c2 = m2;
if (!n)
return 0;
while ((--n > 0) && (*c1 == *c2)) {
c1++;
c2++;
}
return *c1 - *c2;
}
/**
*
* @brief Copy bytes in memory with overlapping areas
*
* @return pointer to destination buffer <d>
*/
void *memmove(void *d, const void *s, size_t n)
{
char *dest = d;
const char *src = s;
if ((size_t) (d - s) < n) {
/*
* The <src> buffer overlaps with the start of the <dest> buffer.
* Copy backwards to prevent the premature corruption of <src>.
*/
while (n > 0) {
n--;
dest[n] = src[n];
}
} else {
/* It is safe to perform a forward-copy */
while (n > 0) {
*dest = *src;
dest++;
src++;
n--;
}
}
return d;
}
/**
*
* @brief Copy bytes in memory
*
* @return pointer to start of destination buffer
*/
void *memcpy(void *_MLIBC_RESTRICT d, const void *_MLIBC_RESTRICT s, size_t n)
{
/* attempt word-sized copying only if buffers have identical alignment */
unsigned char *d_byte = (unsigned char *)d;
const unsigned char *s_byte = (const unsigned char *)s;
if ((((unsigned int)d ^ (unsigned int)s_byte) & 0x3) == 0) {
/* do byte-sized copying until word-aligned or finished */
while (((unsigned int)d_byte) & 0x3) {
if (n == 0) {
return d;
}
*(d_byte++) = *(s_byte++);
n--;
};
/* do word-sized copying as long as possible */
unsigned int *d_word = (unsigned int *)d_byte;
const unsigned int *s_word = (const unsigned int *)s_byte;
while (n >= sizeof(unsigned int)) {
*(d_word++) = *(s_word++);
n -= sizeof(unsigned int);
}
d_byte = (unsigned char *)d_word;
s_byte = (unsigned char *)s_word;
}
/* do byte-sized copying until finished */
while (n > 0) {
*(d_byte++) = *(s_byte++);
n--;
}
return d;
}
/**
*
* @brief Set bytes in memory
*
* @return pointer to start of buffer
*/
void *memset(void *buf, int c, size_t n)
{
/* do byte-sized initialization until word-aligned or finished */
unsigned char *d_byte = (unsigned char *)buf;
unsigned char c_byte = (unsigned char)c;
while (((unsigned int)d_byte) & 0x3) {
if (n == 0) {
return buf;
}
*(d_byte++) = c_byte;
n--;
};
/* do word-sized initialization as long as possible */
unsigned int *d_word = (unsigned int *)d_byte;
unsigned int c_word = (unsigned int)(unsigned char)c;
c_word |= c_word << 8;
c_word |= c_word << 16;
while (n >= sizeof(unsigned int)) {
*(d_word++) = c_word;
n -= sizeof(unsigned int);
}
/* do byte-sized initialization until finished */
d_byte = (unsigned char *)d_word;
while (n > 0) {
*(d_byte++) = c_byte;
n--;
}
return buf;
}
/**
*
* @brief Scan byte in memory
*
* @return pointer to start of found byte
*/
void *memchr(const void *s, unsigned char c, size_t n)
{
if (n != 0) {
const unsigned char *p = s;
do {
if (*p++ == c) {
return ((void *)(p - 1));
}
} while (--n != 0);
}
return NULL;
}
|