You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 regels
3.6 KiB

  1. #ifndef LSFS_STRING_H
  2. #define LSFS_STRING_H
  3. #include <stdbool.h>
  4. typedef struct lsfs_string {
  5. bool dynamic;
  6. int length;
  7. char *chars;
  8. } lsfs_string;
  9. typedef struct lsfs_string_array {
  10. int length;
  11. lsfs_string *strings;
  12. } lsfs_string_array;
  13. static inline lsfs_string lsfs_make_id_string(uint64_t *id) {
  14. return (lsfs_string){
  15. .dynamic = false,
  16. .length = sizeof(*id),
  17. .chars = (char *)id
  18. };
  19. }
  20. static inline lsfs_string lsfs_create_id_string(uint64_t id) {
  21. uint64_t *id_ = malloc(sizeof(id));
  22. *id_ = id;
  23. return (lsfs_string){
  24. .dynamic = true,
  25. .length = sizeof(id),
  26. .chars = (char *)id_
  27. };
  28. }
  29. static inline lsfs_string lsfs_make_string(int length, const char *chars) {
  30. return (lsfs_string){
  31. .dynamic = false,
  32. .length = length,
  33. .chars = (char *)chars
  34. };
  35. }
  36. static inline lsfs_string lsfs_make_string_c(const char *cstring) {
  37. return lsfs_make_string(strlen(cstring), cstring);
  38. }
  39. static inline lsfs_string lsfs_create_string(int length, const char *chars) {
  40. char *copy = malloc(length + 1); // Space for null terminator
  41. memcpy(copy, chars, length);
  42. copy[length] = '\0';
  43. return (lsfs_string){
  44. .dynamic = true,
  45. .length = length,
  46. .chars = copy
  47. };
  48. }
  49. static inline lsfs_string lsfs_clone_string(lsfs_string string) {
  50. return lsfs_create_string(string.length, string.chars);
  51. }
  52. static inline void lsfs_destroy_string(lsfs_string string) {
  53. if(string.dynamic) free(string.chars);
  54. }
  55. static inline bool lsfs_string_equal(lsfs_string a, lsfs_string b) {
  56. if (a.length != b.length) return false;
  57. return strncmp(a.chars, b.chars, b.length) == 0;
  58. }
  59. lsfs_string_array lsfs_create_string_array(size_t array_size) {
  60. lsfs_string_array result;
  61. result.length = array_size;
  62. result.strings = malloc(array_size * sizeof(lsfs_string));
  63. return result;
  64. }
  65. static inline void lsfs_destroy_string_array(lsfs_string_array array) {
  66. for (int i = 0; i < array.length; ++i) {
  67. lsfs_destroy_string(array.strings[i]);
  68. }
  69. free(array.strings);
  70. }
  71. lsfs_string_array lsfs_string_split(lsfs_string string, char delim, bool keep_delim) {
  72. int i;
  73. int last;
  74. int count = 0;
  75. i = 0;
  76. last = 0;
  77. while(i < string.length) {
  78. if (string.chars[i] == delim) {
  79. if (i > last+1) {
  80. ++count;
  81. }
  82. last = i;
  83. }
  84. ++i;
  85. }
  86. if (i > last+1) {
  87. ++count;
  88. }
  89. lsfs_string_array result = lsfs_create_string_array(count);
  90. int insert_index = 0;
  91. int k = keep_delim ? 0 : 1;
  92. i = 0;
  93. last = 0;
  94. while(i < string.length) {
  95. if (string.chars[i] == delim) {
  96. if (i > last+1) {
  97. result.strings[insert_index++] = lsfs_create_string(i-(last+k), string.chars+(last+k));
  98. }
  99. last = i;
  100. }
  101. ++i;
  102. }
  103. if (i > last+1) {
  104. result.strings[insert_index++] = lsfs_create_string(i-(last+k), string.chars+(last+k));
  105. }
  106. return result;
  107. }
  108. static inline lsfs_string_array lsfs_string_split_c(const char *string, char delim, bool keep_delim) {
  109. return lsfs_string_split(lsfs_make_string_c(string), delim, keep_delim);
  110. }
  111. char *dbg_strarr(lsfs_string_array strings) {
  112. static char temp[8192];
  113. memset(temp, 0, sizeof(temp));
  114. int where = 0;
  115. where += sprintf(temp+where, "{");
  116. for (int i = 0; i < strings.length; ++i) {
  117. where += sprintf(temp+where, "<'%.*s'>", strings.strings[i].length, strings.strings[i].chars);
  118. }
  119. where += sprintf(temp+where, "}");
  120. return temp; // @Leak
  121. }
  122. #endif