Xyris  0.5
string.cpp
Go to the documentation of this file.
1 /**
2  * @file string.cpp
3  * @author Keeton Feavel ([email protected])
4  * @brief Standard string and memory utility library
5  * @version 0.3
6  * @date 2020-06-17
7  *
8  * @copyright Copyright the Xyris Contributors (c) 2020
9  *
10  */
11 
12 #include <Library/string.hpp>
13 
14 int strlen(const char* s)
15 {
16  int i = 0;
17  while (s[i] != '\0') {
18  ++i;
19  }
20  return i;
21 }
22 
23 char* strcat(char* dest, const char* src)
24 {
25  const size_t len_dest = strlen(dest);
26  const size_t len_src = strlen(src);
27  memcpy(dest + len_dest, src, len_src + 1);
28  return dest;
29 }
30 
31 void strcpy(char* dest, const char* src)
32 {
33  while (*src) {
34  *dest = *src;
35  dest++;
36  src++;
37  }
38  *dest = '\0';
39 }
40 
41 void strncpy(char* dest, const char* src, size_t len)
42 {
43  while (*src && len--) {
44  *dest = *src;
45  dest++;
46  src++;
47  }
48  *dest = '\0';
49 }
50 
51 int strcmp(const char *s1, const char *s2)
52 {
53  while (*s1)
54  {
55  if (*s1 != *s2)
56  break;
57 
58  s1++;
59  s2++;
60  }
61 
62  return (int)*s1 - (int)*s2;
63 }
64 
65 const char* strstr(const char* haystack, const char* needle)
66 {
67  while (*haystack)
68  {
69  if ((*haystack == *needle) && (strcmp(haystack, needle) == 0)) {
70  return haystack;
71  }
72  haystack++;
73  }
74 
75  return NULL;
76 }
77 
78 void reverse(char* s)
79 {
80  int c;
81  int j = strlen(s) - 1;
82  for (int i = 0; i < j; i++, j--) {
83  c = s[i];
84  s[i] = s[j];
85  s[j] = (char)c;
86  }
87 }
88 
89 void itoa(int n, char str[])
90 {
91  int i, sign;
92  if ((sign = n) < 0)
93  n = -n;
94  i = 0;
95  do {
96  str[i++] = (char)(n % 10 + (int)'0');
97  } while ((n /= 10) > 0);
98 
99  if (sign < 0)
100  str[i++] = '-';
101  str[i] = '\0';
102 
103  reverse(str);
104 }
105 
106 void* memset(void* bufptr, int value, size_t size)
107 {
108  unsigned char* buf = (unsigned char*)bufptr;
109  for (size_t i = 0; i < size; i++)
110  buf[i] = (unsigned char)value;
111  return bufptr;
112 }
113 
114 int memcmp(const void* ptr1, const void* ptr2, size_t num)
115 {
116  const unsigned char* a = (const unsigned char*)ptr1;
117  const unsigned char* b = (const unsigned char*)ptr2;
118  for (size_t i = 0; i < num; i++) {
119  if (a[i] < b[i])
120  return (int)-i;
121  else if (a[i] > b[i])
122  return (int)i;
123  }
124  return 0;
125 }
126 
127 void* memmove(void* destptr, const void* srcptr, size_t size)
128 {
129  unsigned char* dst = (unsigned char*)destptr;
130  const unsigned char* src = (const unsigned char*)srcptr;
131  if (dst < src) {
132  for (size_t i = 0; i < size; i++)
133  dst[i] = src[i];
134  } else {
135  for (size_t i = size; i != 0; i--)
136  dst[i - 1] = src[i - 1];
137  }
138  return destptr;
139 }
140 
141 void* memcpy(void* dstptr, const void* srcptr, size_t size)
142 {
143  unsigned char* dst = (unsigned char*)dstptr;
144  const unsigned char* src = (const unsigned char*)srcptr;
145  for (size_t i = 0; i < size; i++)
146  dst[i] = src[i];
147  return dstptr;
148 }
size
uint16_t size
Definition: regs.hpp:2
strcat
char * strcat(char *dest, const char *src)
Concatanates source onto destination.
Definition: string.cpp:23
strstr
const char * strstr(const char *haystack, const char *needle)
Locates a substring (needle) within a containing string (haystack)
Definition: string.cpp:65
memcmp
int memcmp(const void *ptr1, const void *ptr2, size_t num)
Compares a given number of bytes in memory at pointer A to pointer B.
Definition: string.cpp:114
strlen
int strlen(const char *s)
Returns the length of a string.
Definition: string.cpp:14
string.hpp
Standard string and memory utility library.
strcmp
int strcmp(const char *s1, const char *s2)
Compares two strings.
Definition: string.cpp:51
reverse
void reverse(char *s)
Reverses the inputted string.
Definition: string.cpp:78
strcpy
void strcpy(char *dest, const char *src)
Copys a string from the source to the destination.
Definition: string.cpp:31
memcpy
void * memcpy(void *dstptr, const void *srcptr, size_t size)
Definition: string.cpp:141
itoa
void itoa(int n, char str[])
Converts an integer into its ASCII representation. (This does not have a standard,...
Definition: string.cpp:89
memmove
void * memmove(void *destptr, const void *srcptr, size_t size)
Moves a given number of bytes from the source to the destination.
Definition: string.cpp:127
memset
void * memset(void *bufptr, int value, size_t size)
Sets the number of bytes in memory at ptr to the value.
Definition: string.cpp:106
strncpy
void strncpy(char *dest, const char *src, size_t len)
Copys a string from the source to the destination.
Definition: string.cpp:41