2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-12 21:06:26 -04:00
|
|
|
#include <stdint.h>
|
2020-05-15 16:26:08 -04:00
|
|
|
#include <stddef.h>
|
2022-02-16 14:13:00 -05:00
|
|
|
#include <string.h>
|
2014-03-12 21:06:26 -04:00
|
|
|
|
2022-05-14 08:34:38 -04:00
|
|
|
#include "py/mpconfig.h"
|
|
|
|
|
2021-08-27 13:47:03 -04:00
|
|
|
#ifndef likely
|
2014-09-11 01:49:21 -04:00
|
|
|
#define likely(x) __builtin_expect((x), 1)
|
2021-08-27 13:47:03 -04:00
|
|
|
#endif
|
2014-09-11 01:49:21 -04:00
|
|
|
|
2023-10-19 16:42:36 -04:00
|
|
|
// CIRCUITPY-CHANGE: avoid compiler warnings
|
2016-11-03 18:50:59 -04:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wcast-align"
|
2014-09-11 01:49:21 -04:00
|
|
|
void *memcpy(void *dst, const void *src, size_t n) {
|
2022-05-14 08:34:38 -04:00
|
|
|
#if CIRCUITPY_FULL_BUILD
|
2015-12-31 09:11:18 -05:00
|
|
|
if (likely(!(((uintptr_t)dst) & 3) && !(((uintptr_t)src) & 3))) {
|
2014-09-12 19:12:41 -04:00
|
|
|
// pointers aligned
|
|
|
|
uint32_t *d = dst;
|
|
|
|
const uint32_t *s = src;
|
2014-09-11 01:49:21 -04:00
|
|
|
|
2014-09-12 19:12:41 -04:00
|
|
|
// copy words first
|
|
|
|
for (size_t i = (n >> 2); i; i--) {
|
2014-09-11 01:49:21 -04:00
|
|
|
*d++ = *s++;
|
|
|
|
}
|
2014-09-11 13:01:48 -04:00
|
|
|
|
2014-09-12 19:12:41 -04:00
|
|
|
if (n & 2) {
|
|
|
|
// copy half-word
|
|
|
|
*(uint16_t*)d = *(const uint16_t*)s;
|
|
|
|
d = (uint32_t*)((uint16_t*)d + 1);
|
|
|
|
s = (const uint32_t*)((const uint16_t*)s + 1);
|
2014-09-11 13:01:48 -04:00
|
|
|
}
|
|
|
|
|
2014-09-12 19:12:41 -04:00
|
|
|
if (n & 1) {
|
|
|
|
// copy byte
|
|
|
|
*((uint8_t*)d) = *((const uint8_t*)s);
|
|
|
|
}
|
2022-05-14 08:34:38 -04:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2014-09-12 19:12:41 -04:00
|
|
|
// unaligned access, copy bytes
|
|
|
|
uint8_t *d = dst;
|
|
|
|
const uint8_t *s = src;
|
2014-09-11 01:49:21 -04:00
|
|
|
|
|
|
|
for (; n; n--) {
|
|
|
|
*d++ = *s++;
|
|
|
|
}
|
2014-03-12 21:06:26 -04:00
|
|
|
}
|
2014-09-11 01:49:21 -04:00
|
|
|
|
|
|
|
return dst;
|
2014-03-12 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
2023-10-19 16:42:36 -04:00
|
|
|
// CIRCUITPY-CHANGE: extern
|
2022-02-16 14:13:00 -05:00
|
|
|
extern void *__memcpy_chk(void *dest, const void *src, size_t len, size_t slen);
|
2020-05-15 16:26:08 -04:00
|
|
|
void *__memcpy_chk(void *dest, const void *src, size_t len, size_t slen) {
|
|
|
|
if (len > slen) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return memcpy(dest, src, len);
|
|
|
|
}
|
|
|
|
|
2014-03-12 21:06:26 -04:00
|
|
|
void *memmove(void *dest, const void *src, size_t n) {
|
2014-10-28 12:20:52 -04:00
|
|
|
if (src < dest && (uint8_t*)dest < (const uint8_t*)src + n) {
|
2014-03-12 21:06:26 -04:00
|
|
|
// need to copy backwards
|
2014-10-28 12:20:52 -04:00
|
|
|
uint8_t *d = (uint8_t*)dest + n - 1;
|
|
|
|
const uint8_t *s = (const uint8_t*)src + n - 1;
|
2014-03-12 21:06:26 -04:00
|
|
|
for (; n > 0; n--) {
|
|
|
|
*d-- = *s--;
|
|
|
|
}
|
|
|
|
return dest;
|
|
|
|
} else {
|
|
|
|
// can use normal memcpy
|
|
|
|
return memcpy(dest, src, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *memset(void *s, int c, size_t n) {
|
2022-05-14 08:34:38 -04:00
|
|
|
#if CIRCUITPY_FULL_BUILD
|
2015-12-31 09:11:18 -05:00
|
|
|
if (c == 0 && ((uintptr_t)s & 3) == 0) {
|
2014-09-12 19:12:41 -04:00
|
|
|
// aligned store of 0
|
|
|
|
uint32_t *s32 = s;
|
|
|
|
for (size_t i = n >> 2; i > 0; i--) {
|
|
|
|
*s32++ = 0;
|
|
|
|
}
|
|
|
|
if (n & 2) {
|
|
|
|
*((uint16_t*)s32) = 0;
|
|
|
|
s32 = (uint32_t*)((uint16_t*)s32 + 1);
|
|
|
|
}
|
|
|
|
if (n & 1) {
|
|
|
|
*((uint8_t*)s32) = 0;
|
|
|
|
}
|
2022-05-14 08:34:38 -04:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2014-09-12 19:12:41 -04:00
|
|
|
uint8_t *s2 = s;
|
|
|
|
for (; n > 0; n--) {
|
|
|
|
*s2++ = c;
|
|
|
|
}
|
2014-03-12 21:06:26 -04:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-11-03 18:50:59 -04:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
2015-04-18 09:27:55 -04:00
|
|
|
int memcmp(const void *s1, const void *s2, size_t n) {
|
|
|
|
const uint8_t *s1_8 = s1;
|
|
|
|
const uint8_t *s2_8 = s2;
|
2014-03-12 21:06:26 -04:00
|
|
|
while (n--) {
|
2015-04-18 09:27:55 -04:00
|
|
|
char c1 = *s1_8++;
|
|
|
|
char c2 = *s2_8++;
|
2014-03-12 21:06:26 -04:00
|
|
|
if (c1 < c2) return -1;
|
|
|
|
else if (c1 > c2) return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-28 09:44:14 -04:00
|
|
|
void *memchr(const void *s, int c, size_t n) {
|
|
|
|
if (n != 0) {
|
|
|
|
const unsigned char *p = s;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (*p++ == c)
|
|
|
|
return ((void *)(p - 1));
|
|
|
|
} while (--n != 0);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-12 21:06:26 -04:00
|
|
|
size_t strlen(const char *str) {
|
|
|
|
int len = 0;
|
|
|
|
for (const char *s = str; *s; s++) {
|
|
|
|
len += 1;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int strcmp(const char *s1, const char *s2) {
|
|
|
|
while (*s1 && *s2) {
|
|
|
|
char c1 = *s1++; // XXX UTF8 get char, next char
|
|
|
|
char c2 = *s2++; // XXX UTF8 get char, next char
|
|
|
|
if (c1 < c2) return -1;
|
|
|
|
else if (c1 > c2) return 1;
|
|
|
|
}
|
|
|
|
if (*s2) return -1;
|
|
|
|
else if (*s1) return 1;
|
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int strncmp(const char *s1, const char *s2, size_t n) {
|
|
|
|
while (*s1 && *s2 && n > 0) {
|
|
|
|
char c1 = *s1++; // XXX UTF8 get char, next char
|
|
|
|
char c2 = *s2++; // XXX UTF8 get char, next char
|
|
|
|
n--;
|
|
|
|
if (c1 < c2) return -1;
|
|
|
|
else if (c1 > c2) return 1;
|
|
|
|
}
|
|
|
|
if (n == 0) return 0;
|
|
|
|
else if (*s2) return -1;
|
|
|
|
else if (*s1) return 1;
|
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *strcpy(char *dest, const char *src) {
|
|
|
|
char *d = dest;
|
|
|
|
while (*src) {
|
|
|
|
*d++ = *src++;
|
|
|
|
}
|
|
|
|
*d = '\0';
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2020-07-02 15:48:16 -04:00
|
|
|
// Public Domain implementation of strncpy from:
|
|
|
|
// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strncpy_function
|
|
|
|
char *strncpy(char *s1, const char *s2, size_t n) {
|
|
|
|
char *dst = s1;
|
|
|
|
const char *src = s2;
|
|
|
|
/* Copy bytes, one at a time. */
|
|
|
|
while (n > 0) {
|
|
|
|
n--;
|
|
|
|
if ((*dst++ = *src++) == '\0') {
|
2020-07-22 02:28:46 -04:00
|
|
|
/* If we get here, we found a null character at the end
|
|
|
|
of s2, so use memset to put null bytes at the end of
|
|
|
|
s1. */
|
|
|
|
memset(dst, '\0', n);
|
2020-07-02 15:48:16 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s1;
|
|
|
|
}
|
|
|
|
|
2014-03-12 21:06:26 -04:00
|
|
|
// needed because gcc optimises strcpy + strcat to this
|
|
|
|
char *stpcpy(char *dest, const char *src) {
|
|
|
|
while (*src) {
|
|
|
|
*dest++ = *src++;
|
|
|
|
}
|
|
|
|
*dest = '\0';
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *strcat(char *dest, const char *src) {
|
|
|
|
char *d = dest;
|
|
|
|
while (*d) {
|
|
|
|
d++;
|
|
|
|
}
|
|
|
|
while (*src) {
|
|
|
|
*d++ = *src++;
|
|
|
|
}
|
|
|
|
*d = '\0';
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Public Domain implementation of strchr from:
|
|
|
|
// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strchr_function
|
|
|
|
char *strchr(const char *s, int c)
|
|
|
|
{
|
|
|
|
/* Scan s for the character. When this loop is finished,
|
|
|
|
s will either point to the end of the string or the
|
|
|
|
character we were looking for. */
|
|
|
|
while (*s != '\0' && *s != (char)c)
|
|
|
|
s++;
|
|
|
|
return ((*s == c) ? (char *) s : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Public Domain implementation of strstr from:
|
|
|
|
// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strstr_function
|
|
|
|
char *strstr(const char *haystack, const char *needle)
|
|
|
|
{
|
|
|
|
size_t needlelen;
|
|
|
|
/* Check for the null needle case. */
|
|
|
|
if (*needle == '\0')
|
|
|
|
return (char *) haystack;
|
|
|
|
needlelen = strlen(needle);
|
|
|
|
for (; (haystack = strchr(haystack, *needle)) != 0; haystack++)
|
|
|
|
if (strncmp(haystack, needle, needlelen) == 0)
|
|
|
|
return (char *) haystack;
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-23 15:26:42 -04:00
|
|
|
|
|
|
|
size_t strspn(const char *s, const char *accept) {
|
|
|
|
const char *ss = s;
|
|
|
|
while (*s && strchr(accept, *s) != NULL) {
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
return s - ss;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t strcspn(const char *s, const char *reject) {
|
|
|
|
const char *ss = s;
|
|
|
|
while (*s && strchr(reject, *s) == NULL) {
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
return s - ss;
|
|
|
|
}
|