Nugget
Bare-metal libraries and examples for the original PlayStation
Loading...
Searching...
No Matches
string.h
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2020 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <stddef.h>
30#include <stdint.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36void *memcpy(void *s1, const void *s2, size_t n);
37void *memmove(void *s1, const void *s2, size_t n);
38int memcmp(const void *s1, const void *s2, size_t n);
39void *memset(void *s, int c, size_t n);
40
41#ifdef __cplusplus
42}
43#endif
44
45static __inline__ const void *memchr(const void *s_, int c, size_t n) {
46 const uint8_t *s = (const uint8_t *)s_;
47 for (size_t i = 0; i < n; i++, s++) {
48 if (*s == c) return s;
49 }
50 return NULL;
51}
52
53static __inline__ char *strcat(char *s1, const char *s2) {
54 char *r = s1;
55 char c;
56 while (*s1) s1++;
57 while ((c = *s2++)) *s1++ = c;
58 *s1 = 0;
59 return r;
60}
61
62static __inline__ char *strcpy(char *s1, const char *s2) {
63 char *r = s1;
64 while ((*s1++ = *s2++));
65 return r;
66}
67
68static __inline__ char *strncpy(char *s1, const char *s2, size_t n) {
69 char *r = s1;
70 char c;
71 int done = 0;
72 for (size_t i = 0; i < n; i++) {
73 if (!done && (c = *s2++)) {
74 *s1++ = c;
75 } else {
76 *s1++ = 0;
77 done = 1;
78 }
79 }
80 return r;
81}
82
83static __inline__ char *strchr(const char *s, int c) {
84 char b;
85 while ((b = *s)) {
86 if (b == c) return (char *)s;
87 s++;
88 }
89 return NULL;
90}
91
92static __inline__ char *strrchr(const char *s, int c) {
93 const char *r = NULL;
94 char b;
95 while ((b = *s)) {
96 if (b == c) r = s;
97 s++;
98 }
99 return (char *)r;
100}
101
102static __inline__ size_t strlen(const char *s) {
103 size_t r = 0;
104 while (*s++) r++;
105 return r;
106}
107
108static __inline__ char *strncat(char *s1, const char *s2, size_t n) {
109 char *r = s1;
110 char c;
111 while (*s1) s1++;
112 while (n-- && ((c = *s2++))) *s1++ = c;
113 *s1 = 0;
114 return r;
115}
116
117static __inline__ int strcmp(const char *s1, const char *s2) {
118 int c1, c2;
119 while (1) {
120 if (((c1 = *s1++) != (c2 = *s2++)) || !c1 || !c2) break;
121 }
122 return c1 - c2;
123}
124
125static __inline__ int strncmp(const char *s1, const char *s2, size_t n) {
126 int c1, c2;
127 while (n--) {
128 if (((c1 = *s1++) != (c2 = *s2++)) || !c1 || !c2) return c1 - c2;
129 }
130 return 0;
131}
132
133static __inline__ char *strstr(const char *s1, const char *s2) {
134 size_t l = strlen(s2);
135 while (*s1) {
136 if (!strncmp(s1, s2, l)) return (char *)s1;
137 s1++;
138 }
139 return NULL;
140}
141
142static __attribute__((always_inline)) void* safeMemZero(void* ptr_, int size) {
143 uint8_t* ptr = (uint8_t*)ptr_;
144 if (!ptr || size <= 0) return NULL;
145 uint8_t* orig = ptr;
146 for (; size > 0; ptr++) {
147 size--;
148 *ptr = 0;
149 }
150 return orig;
151}
__attribute__((weak))
Definition clz.c:56
void * memset(void *s, int c, size_t n)
static int size
Definition string.h:142
void * memcpy(void *s1, const void *s2, size_t n)
int memcmp(const void *s1, const void *s2, size_t n)
uint8_t * orig
Definition string.h:145
void * memmove(void *s1, const void *s2, size_t n)
volatile uint32_t * ptr
Definition cop0.c:80
uint32_t r
Definition cpu.c:222
uint8_t b
Definition gte-depthcue.c:39
int i
Definition gte-regio.c:297
volatile void * c1
Definition load-timings.c:522
n
Definition gentable.py:73
char * s
Definition string.c:48
static const char * s2
Definition syscalls.h:92
static int c
Definition syscalls.h:122