string_p.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. size_t i = 0;
  108. while(count > i)
  109. {
  110. if(0 == lhs[i]) break;
  111. if(lhs[i] != rhs[i]) return lhs[i] - rhs[i];
  112. i = i + 1;
  113. }
  114. return 0;
  115. }
  116. char* strchr(char const* str, int ch)
  117. {
  118. char* p = str;
  119. while(ch != p[0])
  120. {
  121. if(0 == p[0]) return NULL;
  122. p = p + 1;
  123. }
  124. if(0 == p[0]) return NULL;
  125. return p;
  126. }
  127. char* strrchr(char const* str, int ch)
  128. {
  129. char* p = str;
  130. int i = 0;
  131. while(0 != p[i]) i = i + 1;
  132. while(ch != p[i])
  133. {
  134. if(0 == i) return NULL;
  135. i = i - 1;
  136. }
  137. return (p + i);
  138. }
  139. size_t strspn(char const* dest, char const* src)
  140. {
  141. if(0 == dest[0]) return 0;
  142. int i = 0;
  143. while(NULL != strchr(src, dest[i])) i = i + 1;
  144. return i;
  145. }
  146. size_t strcspn(char const* dest, char const* src)
  147. {
  148. int i = 0;
  149. while(NULL == strchr(src, dest[i])) i = i + 1;
  150. return i;
  151. }
  152. char* strpbrk(char const* dest, char const* breakset)
  153. {
  154. char* p = dest;
  155. char* s;
  156. while(0 != p[0])
  157. {
  158. s = strchr(breakset, p[0]);
  159. if(NULL != s) return strchr(p, s[0]);
  160. p = p + 1;
  161. }
  162. return p;
  163. }
  164. void* memset(void* dest, int ch, size_t count)
  165. {
  166. if(NULL == dest) return dest;
  167. size_t i = 0;
  168. char* s = dest;
  169. while(i < count)
  170. {
  171. s[i] = ch;
  172. i = i + 1;
  173. }
  174. return dest;
  175. }
  176. void* memcpy(void* dest, void const* src, size_t count)
  177. {
  178. if(NULL == dest) return dest;
  179. if(NULL == src) return NULL;
  180. char* s1 = dest;
  181. char const* s2 = src;
  182. size_t i = 0;
  183. while(i < count)
  184. {
  185. s1[i] = s2[i];
  186. i = i + 1;
  187. }
  188. return dest;
  189. }
  190. void* memmove(void* dest, void const* src, size_t count)
  191. {
  192. if (dest < src) return memcpy (dest, src, count);
  193. char *p = dest;
  194. char const *q = src;
  195. count = count - 1;
  196. while (count >= 0)
  197. {
  198. p[count] = q[count];
  199. count = count - 1;
  200. }
  201. return dest;
  202. }
  203. int memcmp(void const* lhs, void const* rhs, size_t count)
  204. {
  205. if(0 == count) return 0;
  206. size_t i = 0;
  207. count = count - 1;
  208. char const* s1 = lhs;
  209. char const* s2 = rhs;
  210. while(i < count)
  211. {
  212. if(s1[i] != s2[i]) break;
  213. i = i + 1;
  214. }
  215. return (s1[i] - s2[i]);
  216. }