string.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* ----- string.c ----- */
  2. #define strjoin(j, ...) strjoin_(j, PP_NARG(__VA_ARGS__), __VA_ARGS__)
  3. char* strjoin_(char* joiner, size_t nargs, ...);
  4. #define strcatdup(...) strcatdup_(PP_NARG(__VA_ARGS__), __VA_ARGS__)
  5. char* strcatdup_(size_t nargs, ...);
  6. size_t list_len(char** list);
  7. char* join_str_list(char* list[], char* joiner);
  8. char* sprintfdup(char* fmt, ...);
  9. #define path_join(...) path_join_(PP_NARG(__VA_ARGS__), __VA_ARGS__)
  10. char* path_join_(size_t nargs, ...);
  11. #define concat_lists(...) concat_lists_(PP_NARG(__VA_ARGS__), __VA_ARGS__)
  12. char** concat_lists_(int nargs, ...);
  13. size_t span_path(char* s) {
  14. size_t n = 0;
  15. for(; *s; s++, n++) {
  16. if(isspace(*s)) break;
  17. if(*s == '\\') {
  18. s++;
  19. n++;
  20. }
  21. }
  22. return n;
  23. }
  24. // splits on whitespace
  25. char** strsplit(char* splitters, char* in) {
  26. char* e;
  27. int alloc = 32;
  28. int len = 0;
  29. char** list = malloc(alloc * sizeof(*list));
  30. for(char* s = in; *s;) {
  31. e = strpbrk(s, splitters);
  32. if(!e) e = s + strlen(s);
  33. if(len >= alloc - 1) {
  34. alloc *= 2;
  35. list = realloc(list, alloc* sizeof(*list));
  36. }
  37. list[len++] = strndup(s, e - s);
  38. e += strspn(e, splitters);
  39. s = e;
  40. }
  41. list[len] = NULL;
  42. return list;
  43. }
  44. static inline char* strskip(char* s, char* skip) {
  45. return s + strspn(s, skip);
  46. }
  47. // concatenate all argument strings together in a new buffer
  48. char* strcatdup_(size_t nargs, ...) {
  49. size_t total = 0;
  50. char* out, *end;
  51. if(nargs == 0) return NULL;
  52. // calculate total buffer len
  53. va_list va;
  54. va_start(va, nargs);
  55. for(size_t i = 0; i < nargs; i++) {
  56. char* s = va_arg(va, char*);
  57. if(s) total += strlen(s);
  58. }
  59. va_end(va);
  60. out = malloc((total + 1) * sizeof(char*));
  61. end = out;
  62. va_start(va, nargs);
  63. for(size_t i = 0; i < nargs; i++) {
  64. char* s = va_arg(va, char*);
  65. if(s) {
  66. strcpy(end, s); // not exactly the ost efficient, but maybe faster than
  67. end += strlen(s); // a C version. TODO: test the speed
  68. };
  69. }
  70. va_end(va);
  71. *end = 0;
  72. return out;
  73. }
  74. // concatenate all argument strings together in a new buffer,
  75. // with the given joining string between them
  76. char* strjoin_(char* joiner, size_t nargs, ...) {
  77. size_t total = 0;
  78. char* out, *end;
  79. size_t j_len;
  80. if(nargs == 0) return NULL;
  81. // calculate total buffer len
  82. va_list va;
  83. va_start(va, nargs);
  84. for(size_t i = 0; i < nargs; i++) {
  85. char* s = va_arg(va, char*);
  86. if(s) total += strlen(s);
  87. }
  88. va_end(va);
  89. j_len = strlen(joiner);
  90. total += j_len * (nargs - 1);
  91. out = malloc((total + 1) * sizeof(char*));
  92. end = out;
  93. va_start(va, nargs);
  94. for(size_t i = 0; i < nargs; i++) {
  95. char* s = va_arg(va, char*);
  96. if(s) {
  97. if(i > 0) {
  98. strcpy(end, joiner);
  99. end += j_len;
  100. }
  101. strcpy(end, s); // not exactly the ost efficient, but maybe faster than
  102. end += strlen(s); // a C version. TODO: test the speed
  103. };
  104. }
  105. va_end(va);
  106. *end = 0;
  107. return out;
  108. }
  109. // allocates a new buffer and calls sprintf with it
  110. // why isn't this a standard function?
  111. char* sprintfdup(char* fmt, ...) {
  112. va_list va;
  113. va_start(va, fmt);
  114. size_t n = vsnprintf(NULL, 0, fmt, va);
  115. char* buf = malloc(n + 1);
  116. va_end(va);
  117. va_start(va, fmt);
  118. vsnprintf(buf, n + 1, fmt, va);
  119. va_end(va);
  120. return buf;
  121. }
  122. void free_strpp(char** l) {
  123. for(char** s = l; *s; s++) free(*s);
  124. free(l);
  125. }
  126. size_t list_len(char** list) {
  127. size_t total = 0;
  128. for(; *list; list++) total++;
  129. return total;
  130. }
  131. char** concat_lists_(int nargs, ...) {
  132. size_t total = 0;
  133. char** out, **end;
  134. if(nargs == 0) return NULL;
  135. // calculate total list length
  136. va_list va;
  137. va_start(va, nargs);
  138. for(size_t i = 0; i < nargs; i++) {
  139. char** s = va_arg(va, char**);
  140. if(s) total += list_len(s);
  141. }
  142. va_end(va);
  143. out = malloc((total + 1) * sizeof(char**));
  144. end = out;
  145. va_start(va, nargs);
  146. // concat lists
  147. for(size_t i = 0; i < nargs; i++) {
  148. char** s = va_arg(va, char**);
  149. size_t l = list_len(s);
  150. if(s) {
  151. memcpy(end, s, l * sizeof(*s));
  152. end += l;
  153. }
  154. }
  155. va_end(va);
  156. *end = 0;
  157. return out;
  158. }
  159. char* join_str_list(char* list[], char* joiner) {
  160. size_t list_len = 0;
  161. size_t total = 0;
  162. size_t jlen = strlen(joiner);
  163. // calculate total length
  164. for(int i = 0; list[i]; i++) {
  165. list_len++;
  166. total += strlen(list[i]);
  167. }
  168. if(total == 0) return strdup("");
  169. total += (list_len - 1) * jlen;
  170. char* out = malloc((total + 1) * sizeof(*out));
  171. char* end = out;
  172. for(int i = 0; list[i]; i++) {
  173. char* s = list[i];
  174. size_t l = strlen(s);
  175. if(i > 0) {
  176. memcpy(end, joiner, jlen);
  177. end += jlen;
  178. }
  179. if(s) {
  180. memcpy(end, s, l);
  181. end += l;
  182. }
  183. total += strlen(list[i]);
  184. }
  185. *end = 0;
  186. return out;
  187. }
  188. char* path_join_(size_t nargs, ...) {
  189. size_t total = 0;
  190. char* out, *end;
  191. size_t j_len;
  192. char* joiner = "/";
  193. int escape;
  194. if(nargs == 0) return NULL;
  195. // calculate total buffer length
  196. va_list va;
  197. va_start(va, nargs);
  198. for(size_t i = 0; i < nargs; i++) {
  199. char* s = va_arg(va, char*);
  200. if(s) total += strlen(s);
  201. }
  202. va_end(va);
  203. j_len = strlen(joiner);
  204. total += j_len * (nargs - 1);
  205. out = malloc((total + 1) * sizeof(char*));
  206. end = out;
  207. va_start(va, nargs);
  208. for(size_t i = 0; i < nargs; i++) {
  209. char* s = va_arg(va, char*);
  210. size_t l = strlen(s);
  211. if(s) {
  212. if(l > 1) {
  213. escape = s[l-2] == '\\' ? 1 : 0;
  214. }
  215. if(i > 0 && (s[0] == joiner[0])) {
  216. s++;
  217. l--;
  218. }
  219. if(i > 0 && i != nargs-1 && !escape && (s[l-1] == joiner[0])) {
  220. l--;
  221. }
  222. if(i > 0) {
  223. strcpy(end, joiner);
  224. end += j_len;
  225. }
  226. // should be strncpy, but GCC is so fucking stupid that it
  227. // has a warning about using strncpy to do exactly what
  228. // strncpy does if you read the fucking man page.
  229. // fortunately, we are already terminating our strings
  230. // manually so memcpy is a drop-in replacement here.
  231. memcpy(end, s, l);
  232. end += l;
  233. }
  234. }
  235. va_end(va);
  236. *end = 0;
  237. return out;
  238. }
  239. /* -END- string.c ----- */