String.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #include "memory/Allocator.h"
  16. #include "benc/String.h"
  17. #include "util/Bits.h"
  18. #define string_strlen
  19. #include "util/platform/libc/string.h"
  20. #include <stdio.h>
  21. #include <stdarg.h>
  22. /** @see Object.h */
  23. String* String_new(const char* bytes, struct Allocator* allocator)
  24. {
  25. return String_newBinary(bytes, strlen(bytes), allocator);
  26. }
  27. /** @see Object.h */
  28. String* String_newBinary(const char* bytes, unsigned long length, struct Allocator* allocator)
  29. {
  30. char* copy = Allocator_malloc(allocator, length + 1);
  31. // Make the string null terminated so it will print nicely.
  32. copy[length] = '\0';
  33. if (bytes != NULL) {
  34. Bits_memcpy(copy, bytes, length);
  35. } else {
  36. Bits_memset(copy, '\0', length);
  37. }
  38. String* string = Allocator_malloc(allocator, sizeof(String));
  39. string->len = length;
  40. string->bytes = copy;
  41. return string;
  42. }
  43. String* String_vprintf(struct Allocator* allocator, const char* format, va_list args)
  44. {
  45. #define String_BUFFER_SZ 1024
  46. char buff[String_BUFFER_SZ];
  47. vsnprintf(buff, String_BUFFER_SZ, format, args);
  48. size_t length = strlen(buff);
  49. return String_newBinary(buff, length, allocator);
  50. #undef String_BUFFER_SZ
  51. }
  52. String* String_printf(struct Allocator* allocator, const char* format, ...)
  53. {
  54. va_list args;
  55. va_start(args, format);
  56. String* out = String_vprintf(allocator, format, args);
  57. va_end(args);
  58. return out;
  59. }
  60. int String_compare(const String* a, const String* b)
  61. {
  62. if (a == NULL || b == NULL) {
  63. return (a == NULL) - (b == NULL);
  64. }
  65. size_t i;
  66. int d;
  67. for (i = 0; i < (a->len < b->len ? a->len : b->len); i++)
  68. {
  69. d = a->bytes[i] - b->bytes[i];
  70. if (0 != d) {
  71. return d;
  72. }
  73. }
  74. return a->len - b->len;
  75. }
  76. int String_equals(const String* a, const String* b)
  77. {
  78. if (a == NULL || b == NULL) {
  79. return a == NULL && b == NULL;
  80. }
  81. return a->len == b->len && (Bits_memcmp(a->bytes, b->bytes, a->len) == 0);
  82. }