string.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "string.h"
  2. #include <stdbool.h>
  3. size_t strlen(const char *str) {
  4. size_t size = 0;
  5. while (true) {
  6. if (str[size] == 0) {
  7. return size;
  8. }
  9. ++size;
  10. }
  11. return 0;
  12. }
  13. char *strcpy(char *destination, const char *source) {
  14. for (size_t i = 0;; ++i) {
  15. destination[i] = source[i];
  16. if (source[i] == 0) {
  17. return destination;
  18. }
  19. }
  20. return destination;
  21. }
  22. char *strncpy(char *destination, const char *source, size_t num) {
  23. for (size_t i = 0; i < num; ++i) {
  24. destination[i] = source[i];
  25. if (source[i] == 0) {
  26. return destination;
  27. }
  28. }
  29. return destination;
  30. }
  31. char *strcat(char *destination, const char *source) {
  32. size_t di = 0;
  33. while (destination[di] != 0) {
  34. ++di;
  35. }
  36. for (size_t si = 0;; ++si) {
  37. destination[di] = source[si];
  38. if (source[si] == 0) {
  39. return destination;
  40. }
  41. ++di;
  42. }
  43. return destination;
  44. }
  45. char *strstr(const char *str1, const char *str2) {
  46. for (size_t i1 = 0;; ++i1) {
  47. if (str1[i1] == 0) {
  48. return NULL;
  49. }
  50. for (size_t i2 = 0;; ++i2) {
  51. if (str2[i2] == 0) {
  52. return (char *)&str1[i1];
  53. }
  54. if (str1[i1 + i2] != str2[i2]) {
  55. break;
  56. }
  57. }
  58. }
  59. }
  60. int strcmp(const char *str1, const char *str2) {
  61. for (size_t i = 0;; ++i) {
  62. if (str1[i] != str2[i]) {
  63. return str1[i] - str2[i];
  64. }
  65. if (str1[i] == 0) {
  66. return 0;
  67. }
  68. }
  69. }
  70. int strncmp(const char *str1, const char *str2, size_t num) {
  71. for (size_t i = 0; i < num; ++i) {
  72. if (str1[i] != str2[i]) {
  73. return str1[i] - str2[i];
  74. }
  75. if (str1[i] == 0) {
  76. return 0;
  77. }
  78. }
  79. return 0;
  80. }
  81. size_t wcslen(const wchar_t *str) {
  82. size_t size = 0;
  83. while (true) {
  84. if (str[size] == 0) {
  85. return size;
  86. }
  87. ++size;
  88. }
  89. return 0;
  90. }
  91. wchar_t *wcscpy(wchar_t *destination, const wchar_t *source) {
  92. for (size_t i = 0;; ++i) {
  93. destination[i] = source[i];
  94. if (source[i] == 0) {
  95. return destination;
  96. }
  97. }
  98. return destination;
  99. }
  100. wchar_t *wcsncpy(wchar_t *destination, const wchar_t *source, size_t num) {
  101. for (size_t i = 0; i < num; ++i) {
  102. destination[i] = source[i];
  103. if (source[i] == 0) {
  104. return destination;
  105. }
  106. }
  107. return destination;
  108. }
  109. wchar_t *wcscat(wchar_t *destination, const wchar_t *source) {
  110. size_t di = 0;
  111. while (destination[di] != 0) {
  112. ++di;
  113. }
  114. for (size_t si = 0;; ++si) {
  115. destination[di] = source[si];
  116. if (source[si] == 0) {
  117. return destination;
  118. }
  119. ++di;
  120. }
  121. return destination;
  122. }
  123. wchar_t *wcsstr(wchar_t *str1, const wchar_t *str2) {
  124. for (size_t i1 = 0;; ++i1) {
  125. if (str1[i1] == 0) {
  126. return NULL;
  127. }
  128. for (size_t i2 = 0;; ++i2) {
  129. if (str2[i2] == 0) {
  130. return &str1[i1];
  131. }
  132. if (str1[i1 + i2] != str2[i2]) {
  133. break;
  134. }
  135. }
  136. }
  137. }
  138. int wcscmp(const wchar_t *str1, const wchar_t *str2) {
  139. for (size_t i = 0;; ++i) {
  140. if (str1[i] != str2[i]) {
  141. return str1[i] - str2[i];
  142. }
  143. if (str1[i] == 0) {
  144. return 0;
  145. }
  146. }
  147. }
  148. int wcsncmp(const wchar_t *str1, const wchar_t *str2, size_t num) {
  149. for (size_t i = 0; i < num; ++i) {
  150. if (str1[i] != str2[i]) {
  151. return str1[i] - str2[i];
  152. }
  153. if (str1[i] == 0) {
  154. return 0;
  155. }
  156. }
  157. return 0;
  158. }