2015-04-10 16:44:37 -07:00
|
|
|
/* string.c - common string routines */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Wind River Systems, Inc.
|
|
|
|
*
|
2017-01-18 17:01:01 -08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-04-10 16:44:37 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 17:51:40 -04:00
|
|
|
* @brief Copy a string
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2015-07-01 17:29:04 -04:00
|
|
|
* @return pointer to destination buffer <d>
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2017-02-02 12:01:59 -08:00
|
|
|
char *strcpy(char *_MLIBC_RESTRICT d, const char *_MLIBC_RESTRICT s)
|
2015-04-10 16:44:37 -07:00
|
|
|
{
|
|
|
|
char *dest = d;
|
|
|
|
|
|
|
|
while (*s != '\0') {
|
|
|
|
*d = *s;
|
|
|
|
d++;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*d = '\0';
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 17:51:40 -04:00
|
|
|
* @brief Copy part of a string
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2015-07-01 17:29:04 -04:00
|
|
|
* @return pointer to destination buffer <d>
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2017-02-02 12:01:59 -08:00
|
|
|
char *strncpy(char *_MLIBC_RESTRICT d, const char *_MLIBC_RESTRICT s, size_t n)
|
2015-04-10 16:44:37 -07:00
|
|
|
{
|
|
|
|
char *dest = d;
|
|
|
|
|
|
|
|
while ((n > 0) && *s != '\0') {
|
|
|
|
*d = *s;
|
|
|
|
s++;
|
|
|
|
d++;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (n > 0) {
|
|
|
|
*d = '\0';
|
2015-04-28 10:10:42 -04:00
|
|
|
d++;
|
2015-04-10 16:44:37 -07:00
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 17:51:40 -04:00
|
|
|
* @brief String scanning operation
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2015-07-01 17:29:04 -04:00
|
|
|
* @return pointer to 1st instance of found byte, or NULL if not found
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
char *strchr(const char *s, int c)
|
|
|
|
{
|
|
|
|
char tmp = (char) c;
|
|
|
|
|
|
|
|
while ((*s != tmp) && (*s != '\0'))
|
|
|
|
s++;
|
|
|
|
|
|
|
|
return (*s == tmp) ? (char *) s : NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-24 14:56:02 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 17:51:40 -04:00
|
|
|
* @brief Get string length
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2015-07-01 17:29:04 -04:00
|
|
|
* @return number of bytes in string <s>
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
size_t strlen(const char *s)
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
while (*s != '\0') {
|
|
|
|
s++;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 17:51:40 -04:00
|
|
|
* @brief Compare two strings
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2015-07-01 17:29:04 -04:00
|
|
|
* @return negative # if <s1> < <s2>, 0 if <s1> == <s2>, else positive #
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
int strcmp(const char *s1, const char *s2)
|
|
|
|
{
|
|
|
|
while ((*s1 == *s2) && (*s1 != '\0')) {
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *s1 - *s2;
|
|
|
|
}
|
|
|
|
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 17:51:40 -04:00
|
|
|
* @brief Compare part of two strings
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2015-07-01 17:29:04 -04:00
|
|
|
* @return negative # if <s1> < <s2>, 0 if <s1> == <s2>, else positive #
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-02-02 12:01:59 -08:00
|
|
|
char *strcat(char *_MLIBC_RESTRICT dest, const char *_MLIBC_RESTRICT src)
|
2015-07-31 06:57:00 -04:00
|
|
|
{
|
|
|
|
strcpy(dest + strlen(dest), src);
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2017-02-02 12:01:59 -08:00
|
|
|
char *strncat(char *_MLIBC_RESTRICT dest, const char *_MLIBC_RESTRICT src,
|
|
|
|
size_t n)
|
2016-04-11 17:14:47 -04:00
|
|
|
{
|
|
|
|
char *orig_dest = dest;
|
|
|
|
size_t len = strlen(dest);
|
|
|
|
|
|
|
|
dest += len;
|
|
|
|
while ((n-- > 0) && (*src != '\0')) {
|
|
|
|
*dest++ = *src++;
|
|
|
|
}
|
|
|
|
*dest = '\0';
|
|
|
|
|
|
|
|
return orig_dest;
|
|
|
|
}
|
|
|
|
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 17:51:40 -04:00
|
|
|
* @brief Compare two memory areas
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2015-07-01 17:29:04 -04:00
|
|
|
* @return negative # if <m1> < <m2>, 0 if <m1> == <m2>, else positive #
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-05-19 11:36:53 +03:00
|
|
|
int memcmp(const void *m1, const void *m2, size_t n)
|
|
|
|
{
|
|
|
|
const char *c1 = m1;
|
|
|
|
const char *c2 = m2;
|
|
|
|
|
2015-05-19 20:43:30 +03:00
|
|
|
if (!n)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while ((--n > 0) && (*c1 == *c2)) {
|
2015-05-19 11:36:53 +03:00
|
|
|
c1++;
|
|
|
|
c2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *c1 - *c2;
|
|
|
|
}
|
|
|
|
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 17:51:40 -04:00
|
|
|
* @brief Copy bytes in memory with overlapping areas
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2015-07-01 17:29:04 -04:00
|
|
|
* @return pointer to destination buffer <d>
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 17:51:40 -04:00
|
|
|
* @brief Copy bytes in memory
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2015-07-01 17:29:04 -04:00
|
|
|
* @return pointer to start of destination buffer
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2017-02-02 12:01:59 -08:00
|
|
|
void *memcpy(void *_MLIBC_RESTRICT d, const void *_MLIBC_RESTRICT s, size_t n)
|
2015-04-10 16:44:37 -07:00
|
|
|
{
|
2015-05-25 13:50:16 -04:00
|
|
|
/* 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 */
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
while (n > 0) {
|
2015-05-25 13:50:16 -04:00
|
|
|
*(d_byte++) = *(s_byte++);
|
2015-04-10 16:44:37 -07:00
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 17:51:40 -04:00
|
|
|
* @brief Set bytes in memory
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2015-07-01 17:29:04 -04:00
|
|
|
* @return pointer to start of buffer
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2015-05-25 13:50:16 -04:00
|
|
|
void *memset(void *buf, int c, size_t n)
|
2015-04-10 16:44:37 -07:00
|
|
|
{
|
2015-05-25 13:50:16 -04:00
|
|
|
/* 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;
|
2015-04-10 16:44:37 -07:00
|
|
|
|
|
|
|
while (n > 0) {
|
2015-05-25 13:50:16 -04:00
|
|
|
*(d_byte++) = c_byte;
|
2015-04-10 16:44:37 -07:00
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
2015-05-25 13:50:16 -04:00
|
|
|
return buf;
|
2015-04-10 16:44:37 -07:00
|
|
|
}
|
2015-08-14 15:03:59 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|