PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/dl/string.c

https://github.com/nickbjohnson4224/rhombus
C | 164 lines | 86 code | 29 blank | 49 comment | 37 complexity | aa74b86f9777535a89eea5b3eda3470d MD5 | raw file
Possible License(s): 0BSD
  1. /*
  2. * Copyright (C) 2011 Nick Johnson <nickbjohnson4224 at gmail.com>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <string.h>
  17. void *memcpy(void *dst, const void *src, size_t size) {
  18. size_t i;
  19. uint8_t *d = dst;
  20. const uint8_t *s = src;
  21. for (i = 0; i < size; i++) {
  22. d[i] = s[i];
  23. }
  24. return dst;
  25. }
  26. void *memclr(void *ptr, size_t size) {
  27. size_t i;
  28. uint8_t *a = ptr;
  29. for (i = 0; i < size; i++) {
  30. a[i] = 0;
  31. }
  32. return ptr;
  33. }
  34. char *strcpy(char *dst, const char *src) {
  35. size_t i;
  36. for (i = 0; src[i] != '\0'; i++) {
  37. dst[i] = src[i];
  38. }
  39. dst[i] = '\0';
  40. return dst;
  41. }
  42. int strcmp(const char *s1, const char *s2) {
  43. size_t i;
  44. for (i = 0;; i++) {
  45. if (s1[i] == s2[i]) {
  46. if (s1[i] == '\0') return 0;
  47. continue;
  48. }
  49. if (s1[i] == '\0') return -1;
  50. if (s2[i] == '\0') return 1;
  51. if (s1[i] < s2[i]) return -1;
  52. else return 1;
  53. }
  54. }
  55. size_t strlen(const char *str) {
  56. size_t i;
  57. for (i = 0; str[i]; i++);
  58. return i;
  59. }
  60. /*
  61. * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
  62. *
  63. * Permission to use, copy, modify, and distribute this software for any
  64. * purpose with or without fee is hereby granted, provided that the above
  65. * copyright notice and this permission notice appear in all copies.
  66. *
  67. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  68. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  69. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  70. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  71. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  72. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  73. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  74. */
  75. /****************************************************************************
  76. * strlcpy
  77. *
  78. * Copy src to string dst of size siz. At most siz-1 characters
  79. * will be copied. Always NUL terminates (unless siz == 0).
  80. * Returns strlen(src); if retval >= siz, truncation occurred.
  81. */
  82. size_t strlcpy(char *dst, const char *src, size_t siz) {
  83. char *d = dst;
  84. const char *s = src;
  85. size_t n = siz;
  86. /* Copy as many bytes as will fit */
  87. if (n != 0) {
  88. while (--n != 0) {
  89. if ((*d++ = *s++) == '\0') {
  90. break;
  91. }
  92. }
  93. }
  94. /* Not enough room in dst, add NUL and traverse rest of src */
  95. if (n == 0) {
  96. if (siz != 0) {
  97. *d = '\0'; /* NUL-terminate dst */
  98. }
  99. while (*s++);
  100. }
  101. return (s - src - 1); /* count does not include NUL */
  102. }
  103. /****************************************************************************
  104. * strlcat
  105. *
  106. * Appends src to string dst of size siz (unlike strncat, siz is the
  107. * full size of dst, not space left). At most siz-1 characters
  108. * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
  109. * Returns strlen(src) + MIN(siz, strlen(initial dst)).
  110. * If retval >= siz, truncation occurred.
  111. */
  112. size_t strlcat(char *dst, const char *src, size_t siz) {
  113. char *d = dst;
  114. const char *s = src;
  115. size_t n = siz;
  116. size_t dlen;
  117. /* Find the end of dst and adjust bytes left but don't go past end */
  118. while (n-- != 0 && *d != '\0') {
  119. d++;
  120. }
  121. dlen = d - dst;
  122. n = siz - dlen;
  123. if (n == 0) {
  124. return (dlen + strlen(s));
  125. }
  126. while (*s != '\0') {
  127. if (n != 1) {
  128. *d++ = *s;
  129. n--;
  130. }
  131. s++;
  132. }
  133. *d = '\0';
  134. return (dlen + (s - src)); /* count does not include NUL */
  135. }