String.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "memory/Allocator.h"
  16. #include "util/Bits.h"
  17. #include <stdio.h>
  18. #include <stdarg.h>
  19. #include "benc/String.h"
  20. /** @see Object.h */
  21. String* String_new(const char* bytes, struct Allocator* allocator)
  22. {
  23. return String_newBinary(bytes, CString_strlen(bytes), allocator);
  24. }
  25. /** @see Object.h */
  26. String* String_newBinary(const char* bytes, uintptr_t length, struct Allocator* allocator)
  27. {
  28. char* copy = Allocator_malloc(allocator, length + 1);
  29. // Make the string null terminated so it will print nicely.
  30. copy[length] = '\0';
  31. if (bytes != NULL) {
  32. Bits_memcpy(copy, bytes, length);
  33. } else {
  34. Bits_memset(copy, '\0', length);
  35. }
  36. String* string = Allocator_malloc(allocator, sizeof(String));
  37. string->len = length;
  38. string->bytes = copy;
  39. return string;
  40. }
  41. String* String_vprintf(struct Allocator* allocator, const char* format, va_list args)
  42. {
  43. #define String_BUFFER_SZ 1024
  44. char buff[String_BUFFER_SZ];
  45. vsnprintf(buff, String_BUFFER_SZ, format, args);
  46. size_t length = CString_strlen(buff);
  47. return String_newBinary(buff, length, allocator);
  48. #undef String_BUFFER_SZ
  49. }
  50. String* String_printf(struct Allocator* allocator, const char* format, ...)
  51. {
  52. va_list args;
  53. va_start(args, format);
  54. String* out = String_vprintf(allocator, format, args);
  55. va_end(args);
  56. return out;
  57. }
  58. int String_compare(const String* a, const String* b)
  59. {
  60. if (a == NULL || b == NULL) {
  61. return (a == NULL) - (b == NULL);
  62. }
  63. size_t i;
  64. int d;
  65. for (i = 0; i < (a->len < b->len ? a->len : b->len); i++)
  66. {
  67. d = a->bytes[i] - b->bytes[i];
  68. if (0 != d) {
  69. return d;
  70. }
  71. }
  72. return a->len - b->len;
  73. }
  74. int String_equals(const String* a, const String* b)
  75. {
  76. if (a == NULL || b == NULL) {
  77. return a == NULL && b == NULL;
  78. }
  79. return a->len == b->len && (Bits_memcmp(a->bytes, b->bytes, a->len) == 0);
  80. }