ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_freestanding_str.c
Go to the documentation of this file.
1
17
18#if defined(__GNUC__) && !defined(__clang__)
19#pragma GCC optimize("no-tree-loop-distribute-patterns")
20#endif
21
22#include <stddef.h>
23#include <stdint.h>
24
25#include "ra8_freestanding.h"
26
27#if !defined(RA8_OFF_TARGET) || defined(RA8_TEST_FREESTANDING)
28
42size_t strlen(const char* s)
43{
44 size_t len = 0U;
45 while (s[len] != '\0') {
46 ++len;
47 }
48 return len;
49}
50
65size_t strnlen(const char* s, size_t maxlen)
66{
67 size_t len = 0U;
68 while (len < maxlen) {
69 if (s[len] == '\0') {
70 break;
71 }
72 ++len;
73 }
74 return len;
75}
76
93int strcmp(const char* s1, const char* s2)
94{
95 const uint8_t* p1 = (const uint8_t*)s1;
96 const uint8_t* p2 = (const uint8_t*)s2;
97 while (*p1 != 0U) {
98 if (*p1 != *p2) {
99 break;
100 }
101 ++p1;
102 ++p2;
103 }
104 return (int)*p1 - (int)*p2;
105}
106
124int strncmp(const char* s1, const char* s2, size_t n)
125{
126 if (n == 0U) {
127 return 0;
128 }
129 const uint8_t* p1 = (const uint8_t*)s1;
130 const uint8_t* p2 = (const uint8_t*)s2;
131 for (size_t i = 0U; i < n; ++i) {
132 if (p1[i] != p2[i]) {
133 return (int)p1[i] - (int)p2[i];
134 }
135 if (p1[i] == 0U) {
136 return 0;
137 }
138 }
139 return 0;
140}
141
157char* strchr(const char* s, int c)
158{
159 const char target = (char)c;
160 const char* cursor = s;
161 char* result = nullptr;
162 while (*cursor != '\0') {
163 if (*cursor == target) {
164 (void)memcpy((void*)&result, (const void*)&cursor, sizeof(result));
165 break;
166 }
167 ++cursor;
168 }
169 if (target == '\0') {
170 (void)memcpy((void*)&result, (const void*)&cursor, sizeof(result));
171 }
172 return result;
173}
174
190char* strrchr(const char* s, int c)
191{
192 const char target = (char)c;
193 const char* cursor = s;
194 const char* last = nullptr;
195 while (*cursor != '\0') {
196 if (*cursor == target) {
197 last = cursor;
198 }
199 ++cursor;
200 }
201 const char* result_source = (target == '\0') ? cursor : last;
202 char* result = nullptr;
203 (void)memcpy((void*)&result, (const void*)&result_source, sizeof(result));
204 return result;
205}
206
222char* strstr(const char* haystack, const char* needle)
223{
224 if (needle[0] == '\0') {
225 char* result = nullptr;
226 (void)memcpy((void*)&result, (const void*)&haystack, sizeof(result));
227 return result;
228 }
229 for (size_t i = 0U; haystack[i] != '\0'; ++i) {
230 size_t j = 0U;
231 while (needle[j] != '\0') {
232 if (haystack[i + j] != needle[j]) {
233 break;
234 }
235 ++j;
236 }
237 if (needle[j] == '\0') {
238 const char* result_source = &haystack[i];
239 char* result = nullptr;
240 (void)memcpy((void*)&result, (const void*)&result_source, sizeof(result));
241 return result;
242 }
243 }
244 return nullptr;
245}
246
261char* strcpy(char* dst, const char* src)
262{
263 size_t i = 0U;
264 while (src[i] != '\0') {
265 dst[i] = src[i];
266 ++i;
267 }
268 dst[i] = '\0';
269 return dst;
270}
271
288char* strncpy(char* dst, const char* src, size_t n)
289{
290 size_t i = 0U;
291 while (i < n) {
292 if (src[i] == '\0') {
293 break;
294 }
295 dst[i] = src[i];
296 ++i;
297 }
298 while (i < n) {
299 dst[i] = '\0';
300 ++i;
301 }
302 return dst;
303}
304
305#endif /* !RA8_OFF_TARGET || RA8_TEST_FREESTANDING */
Project-owned freestanding C runtime ABI primitives.
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.
int strncmp(const char *s1, const char *s2, size_t n)
Compare two strings up to a specified length.
int strcmp(const char *s1, const char *s2)
Compare two null-terminated strings.
char * strcpy(char *dst, const char *src)
Copy string to destination buffer.
char * strncpy(char *dst, const char *src, size_t n)
Copy bounded string to destination buffer.
size_t strlen(const char *s)
Calculate string length.
char * strstr(const char *haystack, const char *needle)
Locate substring in string.
char * strchr(const char *s, int c)
Locate first occurrence of character in string.
char * strrchr(const char *s, int c)
Locate last occurrence of character in string.
size_t strnlen(const char *s, size_t maxlen)
Calculate bounded string length.