string: fix, make it compile on non-SSE2 machines
[libjh.git] / string.c
index 5ef61b9..76aa69e 100644 (file)
--- a/string.c
+++ b/string.c
@@ -2,11 +2,20 @@
 // This code is licensed under the AGPLv3.
 
 #include <string.h>
 // This code is licensed under the AGPLv3.
 
 #include <string.h>
-#include <emmintrin.h>
 HEADER #include <stdint.h>
 
 HEADER #define streq(a,b) (!strcmp((a),(b)))
 
 HEADER #include <stdint.h>
 
 HEADER #define streq(a,b) (!strcmp((a),(b)))
 
+HEADER #define TPRINTF(name, ...)                                               \
+HEADER   char *name;                                                            \
+HEADER   do {                                                                   \
+HEADER     int __tprintf_size = snprintf(NULL, 0, __VA_ARGS__);                 \
+HEADER     assert(__tprintf_size != -1);                                        \
+HEADER     name = alloca(__tprintf_size+1);                                     \
+HEADER     int __tprintf_size2 = snprintf(name, __tprintf_size+1, __VA_ARGS__); \
+HEADER     assert(__tprintf_size == __tprintf_size2);                           \
+HEADER   } while (0); //////////////////////////////////////////////////////////
+
 PUBLIC_FN int count_char_occurences(char *s, char c) {
   int n=0;
   while (*s) {
 PUBLIC_FN int count_char_occurences(char *s, char c) {
   int n=0;
   while (*s) {
@@ -145,3 +154,23 @@ PUBLIC_FN int ends_with(char *str, char *sub) {
   if (sub_len>str_len) return 0;
   return streq(str+str_len-sub_len, sub);
 }
   if (sub_len>str_len) return 0;
   return streq(str+str_len-sub_len, sub);
 }
+
+PUBLIC_FN char **buf_to_linearray(char *buf, ssize_t buflen) {
+  if (buflen == -1) buflen = strlen(buf);
+  size_t linecount = count_char_occurences_in_buf(buf, buflen, '\n')+1;
+  char **ret = malloc(linecount * sizeof(char*) + 1);
+  ret[linecount] = NULL;
+  if (ret == NULL) return NULL;
+  char **r = ret;
+  *(r++) = buf; /* first line starts at byte zero */
+  char *b = buf;
+  char *be = buf+buflen;
+  while (b<be) {
+    if (*b == '\n') {
+      *b = '\0';
+      *(r++) = b+1;
+    }
+    b++;
+  }
+  return ret;
+}