string.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stddef.h>
  18. char* strcpy(char* dest, char const* src)
  19. {
  20. int i = 0;
  21. while (0 != src[i])
  22. {
  23. dest[i] = src[i];
  24. i = i + 1;
  25. }
  26. dest[i] = 0;
  27. return dest;
  28. }
  29. char* strncpy(char* dest, char const* src, size_t count)
  30. {
  31. if(0 == count) return dest;
  32. size_t i = 0;
  33. while(0 != src[i])
  34. {
  35. dest[i] = src[i];
  36. i = i + 1;
  37. if(count == i) return dest;
  38. }
  39. while(i <= count)
  40. {
  41. dest[i] = 0;
  42. i = i + 1;
  43. }
  44. return dest;
  45. }
  46. char* strcat(char* dest, char const* src)
  47. {
  48. int i = 0;
  49. int j = 0;
  50. while(0 != dest[i]) i = i + 1;
  51. while(0 != src[j])
  52. {
  53. dest[i] = src[j];
  54. i = i + 1;
  55. j = j + 1;
  56. }
  57. dest[i] = 0;
  58. return dest;
  59. }
  60. char* strncat(char* dest, char const* src, size_t count)
  61. {
  62. size_t i = 0;
  63. size_t j = 0;
  64. while(0 != dest[i]) i = i + 1;
  65. while(0 != src[j])
  66. {
  67. if(count == j)
  68. {
  69. dest[i] = 0;
  70. return dest;
  71. }
  72. dest[i] = src[j];
  73. i = i + 1;
  74. j = j + 1;
  75. }
  76. dest[i] = 0;
  77. return dest;
  78. }
  79. size_t strlen(char const* str )
  80. {
  81. size_t i = 0;
  82. while(0 != str[i]) i = i + 1;
  83. return i;
  84. }
  85. size_t strnlen_s(char const* str, size_t strsz )
  86. {
  87. size_t i = 0;
  88. while(0 != str[i])
  89. {
  90. if(strsz == i) return i;
  91. i = i + 1;
  92. }
  93. return i;
  94. }
  95. int strcmp(char const* lhs, char const* rhs )
  96. {
  97. int i = 0;
  98. while(0 != lhs[i])
  99. {
  100. if(lhs[i] != rhs[i]) return lhs[i] - rhs[i];
  101. i = i + 1;
  102. }
  103. return lhs[i] - rhs[i];
  104. }
  105. int strncmp(char const* lhs, char const* rhs, size_t count)
  106. {
  107. if(count == 0) return 0;
  108. size_t i = 0;
  109. while(0 != lhs[i])
  110. {
  111. if(lhs[i] != rhs[i]) return lhs[i] - rhs[i];
  112. i = i + 1;
  113. if(count <= i) return 0;
  114. }
  115. return lhs[i] - rhs[i];
  116. }
  117. char* strchr(char const* str, int ch)
  118. {
  119. char* p = str;
  120. while(ch != p[0])
  121. {
  122. if(0 == p[0]) return NULL;
  123. p = p + 1;
  124. }
  125. if(0 == p[0]) return NULL;
  126. return p;
  127. }
  128. char* strrchr(char const* str, int ch)
  129. {
  130. char* p = str;
  131. int i = 0;
  132. while(0 != p[i]) i = i + 1;
  133. while(ch != p[i])
  134. {
  135. if(0 == i) return NULL;
  136. i = i - 1;
  137. }
  138. return (p + i);
  139. }
  140. size_t strspn(char const* dest, char const* src)
  141. {
  142. if(0 == dest[0]) return 0;
  143. int i = 0;
  144. while(NULL != strchr(src, dest[i])) i = i + 1;
  145. return i;
  146. }
  147. size_t strcspn(char const* dest, char const* src)
  148. {
  149. int i = 0;
  150. while(NULL == strchr(src, dest[i])) i = i + 1;
  151. return i;
  152. }
  153. char* strpbrk(char const* dest, char const* breakset)
  154. {
  155. char* p = dest;
  156. char* s;
  157. while(0 != p[0])
  158. {
  159. s = strchr(breakset, p[0]);
  160. if(NULL != s) return strchr(p, s[0]);
  161. p = p + 1;
  162. }
  163. return p;
  164. }
  165. void* memset(void* dest, int ch, size_t count)
  166. {
  167. if(NULL == dest) return dest;
  168. size_t i = 0;
  169. char* s = dest;
  170. while(i < count)
  171. {
  172. s[i] = ch;
  173. i = i + 1;
  174. }
  175. return dest;
  176. }
  177. void* memcpy(void* dest, void const* src, size_t count)
  178. {
  179. if(NULL == dest) return dest;
  180. if(NULL == src) return NULL;
  181. char* s1 = dest;
  182. char const* s2 = src;
  183. size_t i = 0;
  184. while(i < count)
  185. {
  186. s1[i] = s2[i];
  187. i = i + 1;
  188. }
  189. return dest;
  190. }
  191. void* memmove(void* dest, void const* src, size_t count)
  192. {
  193. if (dest < src) return memcpy (dest, src, count);
  194. char *p = dest;
  195. char const *q = src;
  196. count = count - 1;
  197. while (count >= 0)
  198. {
  199. p[count] = q[count];
  200. count = count - 1;
  201. }
  202. return dest;
  203. }
  204. int memcmp(void const* lhs, void const* rhs, size_t count)
  205. {
  206. if(0 == count) return 0;
  207. size_t i = 0;
  208. count = count - 1;
  209. char const* s1 = lhs;
  210. char const* s2 = rhs;
  211. while(i < count)
  212. {
  213. if(s1[i] != s2[i]) break;
  214. i = i + 1;
  215. }
  216. return (s1[i] - s2[i]);
  217. }
  218. char* strstr(char* haystack, char* needle)
  219. {
  220. int hl = strlen(haystack);
  221. int sl = strlen(needle);
  222. int i = 0;
  223. int max = hl - sl;
  224. if(hl < sl) return NULL;
  225. else if(hl == sl)
  226. {
  227. if(0 == strncmp(haystack, needle, hl)) return haystack;
  228. return NULL;
  229. }
  230. else
  231. {
  232. while(i <= max)
  233. {
  234. if(0 == strncmp(haystack+i, needle, hl)) return haystack+i;
  235. i = i + 1;
  236. }
  237. return NULL;
  238. }
  239. }