From: Ben Pfaff Date: Sun, 11 Sep 2022 18:40:02 +0000 (-0700) Subject: str: Replace ss_alloc_substring() by ss_clone() and similarly with a pool. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=96e7b2185df6cafec262a1bec7a976f6901bf95b;p=pspp str: Replace ss_alloc_substring() by ss_clone() and similarly with a pool. This makes it possible to use in an initializer. --- diff --git a/src/language/data-io/data-parser.c b/src/language/data-io/data-parser.c index 3f19fb6262..0b3e061921 100644 --- a/src/language/data-io/data-parser.c +++ b/src/language/data-io/data-parser.c @@ -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); } diff --git a/src/language/expressions/parse.c b/src/language/expressions/parse.c index f1800f3775..397121f913 100644 --- a/src/language/expressions/parse.c +++ b/src/language/expressions/parse.c @@ -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); diff --git a/src/language/lexer/lexer.c b/src/language/lexer/lexer.c index 7d268647e6..24f901d3c0 100644 --- a/src/language/lexer/lexer.c +++ b/src/language/lexer/lexer.c @@ -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 diff --git a/src/language/lexer/macro.c b/src/language/lexer/macro.c index 8e599f4dde..9536727a0f 100644 --- a/src/language/lexer/macro.c +++ b/src/language/lexer/macro.c @@ -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 diff --git a/src/language/lexer/scan.c b/src/language/lexer/scan.c index f2dcebca15..e4fe405d47 100644 --- a/src/language/lexer/scan.c +++ b/src/language/lexer/scan.c @@ -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: diff --git a/src/language/lexer/token.c b/src/language/lexer/token.c index 71e98f2d7d..124d14638d 100644 --- a/src/language/lexer/token.c +++ b/src/language/lexer/token.c @@ -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. */ diff --git a/src/libpspp/i18n.c b/src/libpspp/i18n.c index 3faadcbb87..dc36c73dc5 100644 --- a/src/libpspp/i18n.c +++ b/src/libpspp/i18n.c @@ -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; diff --git a/src/libpspp/pool.c b/src/libpspp/pool.c index 5309c729ef..377ca9d853 100644 --- a/src/libpspp/pool.c +++ b/src/libpspp/pool.c @@ -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); diff --git a/src/libpspp/pool.h b/src/libpspp/pool.h index ebc00c5f0f..4d2003e540 100644 --- a/src/libpspp/pool.h +++ b/src/libpspp/pool.h @@ -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 *, ...) diff --git a/src/libpspp/str.c b/src/libpspp/str.c index 2a592b9064..e851bac1f3 100644 --- a/src/libpspp/str.c +++ b/src/libpspp/str.c @@ -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. */ diff --git a/src/libpspp/str.h b/src/libpspp/str.h index c40915d939..b8bb6ffca2 100644 --- a/src/libpspp/str.h +++ b/src/libpspp/str.h @@ -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 *);