str: Replace ss_alloc_substring() by ss_clone() and similarly with a pool.
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 11 Sep 2022 18:40:02 +0000 (11:40 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 11 Sep 2022 18:41:19 +0000 (11:41 -0700)
This makes it possible to use in an initializer.

src/language/data-io/data-parser.c
src/language/expressions/parse.c
src/language/lexer/lexer.c
src/language/lexer/macro.c
src/language/lexer/scan.c
src/language/lexer/token.c
src/libpspp/i18n.c
src/libpspp/pool.c
src/libpspp/pool.h
src/libpspp/str.c
src/libpspp/str.h

index 3f19fb6262e97975ec64e4b76da859cb6d90a6b4..0b3e061921f9000b6a1f49e174c44201b2ef3638 100644 (file)
@@ -84,26 +84,16 @@ struct data_parser *
 data_parser_create (void)
 {
   struct data_parser *parser = xmalloc (sizeof *parser);
-
-  parser->type = DP_FIXED;
-  parser->skip_records = 0;
-
-  parser->fields = NULL;
-  parser->n_fields = 0;
-  parser->field_allocated = 0;
-
-  parser->span = true;
-  parser->empty_line_has_field = false;
-  parser->warn_missing_fields = true;
-  ss_alloc_substring (&parser->quotes, ss_cstr ("\"'"));
-  parser->quote_escape = false;
-  ss_alloc_substring (&parser->soft_seps, ss_cstr (CC_SPACES));
-  ss_alloc_substring (&parser->hard_seps, ss_cstr (","));
-  ds_init_empty (&parser->any_sep);
+  *parser = (struct data_parser) {
+    .type = DP_FIXED,
+    .span = true,
+    .warn_missing_fields = true,
+    .quotes = ss_clone (ss_cstr ("\"'")),
+    .soft_seps = ss_clone (ss_cstr (CC_SPACES)),
+    .hard_seps = ss_clone (ss_cstr (",")),
+  };
   set_any_sep (parser);
 
-  parser->records_per_case = 0;
-
   return parser;
 }
 
@@ -215,7 +205,7 @@ void
 data_parser_set_quotes (struct data_parser *parser, struct substring quotes)
 {
   ss_dealloc (&parser->quotes);
-  ss_alloc_substring (&parser->quotes, quotes);
+  parser->quotes = ss_clone (quotes);
 }
 
 /* If ESCAPE is false (the default setting), a character used for
@@ -245,7 +235,7 @@ data_parser_set_soft_delimiters (struct data_parser *parser,
                                  struct substring delimiters)
 {
   ss_dealloc (&parser->soft_seps);
-  ss_alloc_substring (&parser->soft_seps, delimiters);
+  parser->soft_seps = ss_clone (delimiters);
   set_any_sep (parser);
 }
 
@@ -261,7 +251,7 @@ data_parser_set_hard_delimiters (struct data_parser *parser,
                                  struct substring delimiters)
 {
   ss_dealloc (&parser->hard_seps);
-  ss_alloc_substring (&parser->hard_seps, delimiters);
+  parser->hard_seps = ss_clone (delimiters);
   set_any_sep (parser);
 }
 
index f1800f3775afeca8078d9916c608d8f5100fe0c9..397121f913c51427d64258b41af4c39a8a83710e 100644 (file)
@@ -863,9 +863,7 @@ expr_date (struct expression *e, int year_digits)
                : xasprintf ("%02d-%s-%04d", time->tm_mday, months[time->tm_mon],
                             time->tm_year + 1900));
 
-  struct substring s;
-  ss_alloc_substring_pool (&s, ss_cstr (tmp), e->expr_pool);
-
+  struct substring s = ss_clone_pool (ss_cstr (tmp), e->expr_pool);
   free (tmp);
 
   return expr_allocate_string (e, s);
index 7d268647e6266f1b66efdc1f5da61fb1c7cfc15d..24f901d3c04569eec6af73a3a1cfe5efdebc3647 100644 (file)
@@ -2596,9 +2596,7 @@ lex_reader_for_substring_nocopy (struct substring s, const char *encoding)
 struct lex_reader *
 lex_reader_for_string (const char *s, const char *encoding)
 {
-  struct substring ss;
-  ss_alloc_substring (&ss, ss_cstr (s));
-  return lex_reader_for_substring_nocopy (ss, encoding);
+  return lex_reader_for_substring_nocopy (ss_clone (ss_cstr (s)), encoding);
 }
 
 /* Formats FORMAT as a printf()-like format string and creates and returns a
index 8e599f4ddeaa4e449f67bc279bfd16ec51e45a8d..9536727a0f78d08b047feaeb1b639597462934cc 100644 (file)
@@ -136,7 +136,7 @@ void
 macro_token_copy (struct macro_token *dst, const struct macro_token *src)
 {
   token_copy (&dst->token, &src->token);
-  ss_alloc_substring (&dst->syntax, src->syntax);
+  dst->syntax = ss_clone (src->syntax);
 }
 
 void
index f2dcebca15ea562543d5fde64b7cac95ec96ab4b..e4fe405d47c340963666604f3c43a4059fd2a456 100644 (file)
@@ -334,8 +334,7 @@ token_from_segment (enum segment_type type, struct substring s,
     case SEG_DOCUMENT:
     case SEG_MACRO_BODY:
     case SEG_MACRO_NAME:
-      *token = (struct token) { .type = T_STRING };
-      ss_alloc_substring (&token->string, s);
+      *token = (struct token) { .type = T_STRING, .string = ss_clone (s) };
       return TOKENIZE_TOKEN;
 
     case SEG_RESERVED_WORD:
@@ -343,19 +342,17 @@ token_from_segment (enum segment_type type, struct substring s,
       return TOKENIZE_TOKEN;
 
     case SEG_IDENTIFIER:
-      *token = (struct token) { .type = T_ID };
-      ss_alloc_substring (&token->string, s);
+      *token = (struct token) { .type = T_ID, .string = ss_clone (s) };
       return TOKENIZE_TOKEN;
 
     case SEG_MACRO_ID:
-      *token = (struct token) { .type = T_MACRO_ID };
-      ss_alloc_substring (&token->string, s);
+      *token = (struct token) { .type = T_MACRO_ID, .string = ss_clone (s)};
       return TOKENIZE_TOKEN;
 
     case SEG_PUNCT:
       *token = (struct token) { .type = scan_punct__ (s) };
       if (token->type == T_MACRO_PUNCT)
-        ss_alloc_substring (&token->string, s);
+        token->string = ss_clone (s);
       return TOKENIZE_TOKEN;
 
     case SEG_SHBANG:
@@ -366,8 +363,10 @@ token_from_segment (enum segment_type type, struct substring s,
       return TOKENIZE_EMPTY;
 
     case SEG_START_DOCUMENT:
-      *token = (struct token) { .type = T_ID };
-      ss_alloc_substring (&token->string, ss_cstr ("DOCUMENT"));
+      *token = (struct token) {
+        .type = T_ID,
+        .string = ss_clone (ss_cstr ("DOCUMENT"))
+      };
       return TOKENIZE_TOKEN;
 
     case SEG_START_COMMAND:
index 71e98f2d7de44369f4648b498be2569d85978c37..124d14638d8f3be3e527173e02dc6f332daff39f 100644 (file)
@@ -37,8 +37,8 @@ token_copy (struct token *dst, const struct token *src)
   *dst = (struct token) {
     .type = src->type,
     .number = src->number,
+    .string = ss_clone (src->string),
   };
-  ss_alloc_substring (&dst->string, src->string);
 }
 
 /* Frees the string that TOKEN contains. */
index 3faadcbb87c770395f5ce0c86fdef509df50c612..dc36c73dc552130009fa4d0475d6c3d5b2d51bf4 100644 (file)
@@ -1068,9 +1068,9 @@ get_encoding_info (struct encoding_info *e, const char *name)
       ss_dealloc (&cr);
       ss_dealloc (&lf);
       ss_dealloc (&space);
-      ss_alloc_substring (&cr, ss_cstr ("\r"));
-      ss_alloc_substring (&lf, ss_cstr ("\n"));
-      ss_alloc_substring (&space, ss_cstr (" "));
+      cr = ss_clone (ss_cstr ("\r"));
+      lf = ss_clone (ss_cstr ("\n"));
+      space = ss_clone (ss_cstr (" "));
     }
 
   e->unit = cr.length;
index 5309c729efa77c2f5ce66cf1e9521a64fc16489d..377ca9d853a44ca900e02da8bc1e1ac6da5cccab 100644 (file)
@@ -382,7 +382,7 @@ pool_strdup (struct pool *pool, const char *string)
    strings, because the returned pointere may not be aligned
    properly for other types. */
 char *
-pool_strdup0 (struct pool *pool, const char *string, size_t size)
+pool_memdup0 (struct pool *pool, const char *string, size_t size)
 {
   char *new_string = pool_alloc_unaligned (pool, size + 1);
   memcpy (new_string, string, size);
index ebc00c5f0fa6c433d2c1a0200633dc653b87ed57..4d2003e540ad074b47224cdfd6e2309e146b00b2 100644 (file)
@@ -62,7 +62,7 @@ void *pool_clone (struct pool *, const void *, size_t) MALLOC_LIKE;
 void *pool_alloc_unaligned (struct pool *, size_t) MALLOC_LIKE;
 void *pool_clone_unaligned (struct pool *, const void *, size_t) MALLOC_LIKE;
 char *pool_strdup (struct pool *, const char *) MALLOC_LIKE;
-char *pool_strdup0 (struct pool *, const char *, size_t) MALLOC_LIKE;
+char *pool_memdup0 (struct pool *, const char *, size_t) MALLOC_LIKE;
 char *pool_vasprintf (struct pool *, const char *, va_list)
      MALLOC_LIKE PRINTF_FORMAT (2, 0);
 char *pool_asprintf (struct pool *, const char *, ...)
index 2a592b9064d88368547216404cc759cb9da21b8b..e851bac1f3f7cef17b35e134434be77f8a84570d 100644 (file)
@@ -376,13 +376,15 @@ ss_tail (struct substring ss, size_t n)
     return ss;
 }
 
-/* Makes a malloc()'d, null-terminated copy of the contents of OLD
-   and stores it in NEW. */
-void
-ss_alloc_substring (struct substring *new, struct substring old)
+/* Returns a malloc()'d, null-terminated copy of the contents of OLD.  The
+  caller owns the returned string and must eventually free it. */
+struct substring
+ss_clone (struct substring old)
 {
-  new->string = xmemdup0 (old.string, old.length);
-  new->length = old.length;
+  return (struct substring) {
+    .string = xmemdup0 (old.string, old.length),
+    .length = old.length,
+  };
 }
 
 /* Allocates room for a N-byte string in NEW. */
@@ -399,16 +401,15 @@ ss_realloc (struct substring *ss, size_t size)
   ss->string = xrealloc (ss->string, size);
 }
 
-/* Makes a pool_alloc_unaligned()'d, null-terminated copy of the contents of
-   OLD in POOL, and stores it in NEW. */
-void
-ss_alloc_substring_pool (struct substring *new, struct substring old,
-                         struct pool *pool)
+/* Returns a pool_alloc_unaligned()'d, null-terminated copy of the contents of
+  OLD in POOL.  The pool owns the returned string. */
+struct substring
+ss_clone_pool (struct substring old, struct pool *pool)
 {
-  new->string = pool_alloc_unaligned (pool, old.length + 1);
-  new->length = old.length;
-  memcpy (new->string, old.string, old.length);
-  new->string[old.length] = '\0';
+  return (struct substring) {
+    .string = pool_memdup0 (pool, old.string, old.length),
+    .length = old.length
+  };
 }
 
 /* Allocates room for a N-byte string in NEW in POOL. */
index c40915d939a3e3a9cd874dd985e5e0774f629ec7..b8bb6ffca2e43077d44dae9ae26ed6ec65e0e09e 100644 (file)
@@ -92,11 +92,10 @@ struct substring ss_tail (struct substring, size_t);
 /* Constructors and destructor that allocate and deallocate
    memory. */
 struct pool;
-void ss_alloc_substring (struct substring *, struct substring);
+struct substring ss_clone (struct substring);
+struct substring ss_clone_pool (struct substring, struct pool *);
 void ss_alloc_uninit (struct substring *, size_t);
 void ss_realloc (struct substring *, size_t);
-void ss_alloc_substring_pool (struct substring *, struct substring,
-                              struct pool *);
 void ss_alloc_uninit_pool (struct substring *, size_t, struct pool *);
 void ss_dealloc (struct substring *);