Implemented support for very long strings a la spss v13/v14
[pspp-builds.git] / src / libpspp / str.h
index 22adc29fa030e0420819d3d716a268bf9ee2c6d6..aa93fb2b7b751bd0600e151d51f9b2ca8bfa6ba9 100644 (file)
 #if !str_h
 #define str_h 1
 
+#include <assert.h>
 #include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
 
+#include "compiler.h"
 #include "memcasecmp.h"
 #include "memmem.h"
 #include "snprintf.h"
 #define strrchr rindex
 #endif
 \f
-/* sprintf() wrapper functions for convenience. */
-
-/* spprintf() calls sprintf() and returns the address of the null
-   terminator in the resulting string.  It should be portable the way
-   it's been implemented. */
-#if __GNUC__
-  #if HAVE_GOOD_SPRINTF
-    #define spprintf(BUF, FORMAT, ARGS...)                     \
-           ((BUF) + sprintf ((BUF), (FORMAT) , ## ARGS))
-  #else
-    #define spprintf(BUF, FORMAT, ARGS...)             \
-           ({ sprintf ((BUF), (FORMAT) , ## ARGS);     \
-               strchr ((BUF), 0); })
-  #endif
-#else /* Not GNU C. */
-  char *spprintf (char *buf, const char *format, ...);
-#endif /* Not GNU C. */
-
-/* nsprintf() calls sprintf() and returns the strlen() of the
-   resulting string.  It should be portable the way it's been
-   implemented. */
-#if __GNUC__
-  #if HAVE_GOOD_SPRINTF
-    #define nsprintf(BUF, FORMAT, ARGS...)             \
-           (sprintf ((BUF), (FORMAT) , ## ARGS))
-    #define nvsprintf(BUF, FORMAT, ARGS)               \
-           (vsprintf ((BUF), (FORMAT), (ARGS)))
-  #else /* Not good sprintf(). */
-    #define nsprintf(BUF, FORMAT, ARGS...)             \
-           ({                                          \
-             char *pbuf = BUF;                         \
-             sprintf ((pbuf), (FORMAT) , ## ARGS);     \
-             strlen (pbuf);                            \
-           })
-    #define nvsprintf(BUF, FORMAT, ARGS)               \
-           ({                                          \
-             char *pbuf = BUF;                         \
-             vsprintf ((pbuf), (FORMAT), (ARGS));      \
-             strlen (pbuf);                            \
-           })
-  #endif /* Not good sprintf(). */
-#else /* Not GNU C. */
-  #if HAVE_GOOD_SPRINTF
-    #define nsprintf sprintf
-    #define nvsprintf vsprintf
-  #else /* Not good sprintf(). */
-    int nsprintf (char *buf, const char *format, ...);
-    int nvsprintf (char *buf, const char *format, va_list args);
-  #endif /* Not good sprintf(). */
-#endif /* Not GNU C. */
-\f
 /* Miscellaneous. */
 
 void buf_reverse (char *, size_t);
@@ -157,28 +108,45 @@ ls_end (const struct fixed_string *st)
 
 struct string
   {
+    char *string;       /* String data, not necessarily null terminated. */
     size_t length;      /* Length, not including a null terminator. */
     size_t capacity;    /* Allocated capacity, not including one
                            extra byte allocated for null terminator. */
-    char *string;       /* String data, not necessarily null
-                           terminated. */
   };
 
+#define DS_INITIALIZER {NULL, 0, 0}
+
 /* Constructors, destructors. */
-void ds_create (struct string *, const char *);
 void ds_init (struct string *, size_t);
+void ds_init_substring (struct string *,
+                        const struct string *src, size_t start, size_t cnt);
+void ds_create (struct string *, const char *);
 void ds_destroy (struct string *);
 void ds_swap (struct string *, struct string *);
 
-/* Copy, shrink, extend. */
-void ds_replace (struct string *, const char *);
+/* Replacement. */
+void ds_assign_string (struct string *, const struct string *);
+void ds_assign_substring (struct string *,
+                          const struct string *, size_t start, size_t cnt);
+void ds_assign_buffer (struct string *, const char *, size_t);
+void ds_assign_c_str (struct string *, const char *);
+
+/* Shrink, extend. */
 void ds_clear (struct string *);
 void ds_extend (struct string *, size_t);
 void ds_shrink (struct string *);
 void ds_truncate (struct string *, size_t);
+
+/* Padding, trimming. */
 void ds_rpad (struct string *, size_t length, char pad);
 int ds_rtrim_spaces (struct string *);
+int ds_ltrim_spaces (struct string *);
+void ds_trim_spaces (struct string *);
 bool ds_chomp (struct string *, char);
+bool ds_separate (const struct string *src, struct string *token,
+                  const char *delimiters, size_t *save_idx);
+bool ds_tokenize (const struct string *src, struct string *token,
+                  const char *delimiters, size_t *save_idx);
 
 /* Inspectors. */
 bool ds_is_empty (const struct string *);
@@ -187,16 +155,19 @@ char *ds_c_str (const struct string *);
 char *ds_data (const struct string *);
 char *ds_end (const struct string *);
 size_t ds_capacity (const struct string *);
+int ds_at (const struct string *, size_t idx);
 int ds_first (const struct string *);
 int ds_last (const struct string *);
+size_t ds_span (const struct string *st, size_t ofs, const char skip_set[]);
+size_t ds_cspan (const struct string *st, size_t ofs, const char stop_set[]);
 
 /* File input. */
-struct file_locator;
-int ds_gets (struct string *, FILE *);
-int ds_get_config_line (FILE *, struct string *, struct file_locator *);
+bool ds_gets (struct string *, FILE *);
+bool ds_get_config_line (FILE *, struct string *, int *line_number);
 
 /* Append. */
 void ds_putc (struct string *, int ch);
+void ds_putc_multiple (struct string *, int ch, size_t);
 void ds_puts (struct string *, const char *);
 void ds_concat (struct string *, const char *, size_t);
 void ds_vprintf (struct string *st, const char *, va_list);
@@ -218,13 +189,6 @@ ds_length (const struct string *st)
   return st->length;
 }
 
-extern inline char *
-ds_c_str (const struct string *st)
-{
-  ((char *) st->string)[st->length] = '\0';
-  return st->string;
-}
-
 extern inline char *
 ds_data (const struct string *st)
 {
@@ -238,4 +202,27 @@ ds_end (const struct string *st)
 }
 #endif
 
+#define nsprintf sprintf
+#define nvsprintf vsprintf
+
+/* Formats FORMAT into DST, as with sprintf(), and returns the
+   address of the terminating null written to DST. */
+static inline char *
+spprintf (char *dst, const char *format, ...) 
+{
+  va_list args;
+  int count;
+
+  va_start (args, format);
+  count = nvsprintf (dst, format, args);
+  va_end (args);
+
+  return dst + count;
+}
+
+
+char * ds_append_uninit(struct string *st, size_t incr);
+
+
+
 #endif /* str_h */