[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[formatting i18n 09/14] format: Create a new "struct fmt_affix" for pref
From: |
Ben Pfaff |
Subject: |
[formatting i18n 09/14] format: Create a new "struct fmt_affix" for prefix and suffix strings. |
Date: |
Sat, 19 Feb 2011 17:42:21 -0800 |
This combines two changes: changing the string type for numeric
prefix and suffix strings from "struct substring" to plain "char *",
and putting the string inside a new structure. Both of these will
make more sense after the following commit, which adds another member
to the new structure and stops using the length of the string in so
many places (which is the reason that "struct substring" was a good
choice).
---
src/data/data-in.c | 12 +++---
src/data/data-out.c | 36 +++++++++----------
src/data/format.c | 79 ++++++++++++++++++++++++++----------------
src/data/format.h | 20 +++++++----
src/language/utilities/set.q | 16 ++++----
5 files changed, 93 insertions(+), 70 deletions(-)
diff --git a/src/data/data-in.c b/src/data/data-in.c
index b6fa516..e72d1b6 100644
--- a/src/data/data-in.c
+++ b/src/data/data-in.c
@@ -298,9 +298,9 @@ parse_number (struct data_in *i)
ds_extend (&tmp, 64);
/* Prefix character may precede sign. */
- if (!ss_is_empty (style->prefix))
+ if (style->prefix.s[0] != '\0')
{
- ss_match_byte (&i->input, ss_first (style->prefix));
+ ss_match_byte (&i->input, style->prefix.s[0]);
ss_ltrim (&i->input, ss_cstr (CC_SPACES));
}
@@ -317,9 +317,9 @@ parse_number (struct data_in *i)
}
/* Prefix character may follow sign. */
- if (!ss_is_empty (style->prefix))
+ if (style->prefix.s[0] != '\0')
{
- ss_match_byte (&i->input, ss_first (style->prefix));
+ ss_match_byte (&i->input, style->prefix.s[0]);
ss_ltrim (&i->input, ss_cstr (CC_SPACES));
}
@@ -366,8 +366,8 @@ parse_number (struct data_in *i)
}
/* Suffix character. */
- if (!ss_is_empty (style->suffix))
- ss_match_byte (&i->input, ss_first (style->suffix));
+ if (style->suffix.s[0] != '\0')
+ ss_match_byte (&i->input, style->suffix.s[0]);
if (!ss_is_empty (i->input))
{
diff --git a/src/data/data-out.c b/src/data/data-out.c
index 85e8eaa..a30e7e7 100644
--- a/src/data/data-out.c
+++ b/src/data/data-out.c
@@ -131,10 +131,12 @@ char *
data_out_pool (const union value *input, const char *encoding,
const struct fmt_spec *format, struct pool *pool)
{
- char *output = xmalloc (format->w + 1);
+ char *output;
char *t ;
assert (fmt_check_output (format));
+ output = xmalloc (format->w + 1);
+
converters[format->type] (input, format, output);
t = recode_string_pool (UTF8, encoding, output, format->w, pool);
@@ -600,9 +602,9 @@ output_decimal (const struct rounder *r, const struct
fmt_spec *format,
the negative suffix, plus (if negative) the negative
prefix. */
width = rounder_width (r, decimals, &integer_digits, &add_neg_prefix);
- width += ss_length (style->neg_suffix);
+ width += strlen (style->neg_suffix.s);
if (add_neg_prefix)
- width += ss_length (style->neg_prefix);
+ width += strlen (style->neg_prefix.s);
if (width > format->w)
continue;
@@ -632,10 +634,9 @@ output_decimal (const struct rounder *r, const struct
fmt_spec *format,
if (format->w > width)
p = mempset (p, ' ', format->w - width);
if (add_neg_prefix)
- p = mempcpy (p, ss_data (style->neg_prefix),
- ss_length (style->neg_prefix));
+ p = stpcpy (p, style->neg_prefix.s);
if (add_affixes)
- p = mempcpy (p, ss_data (style->prefix), ss_length (style->prefix));
+ p = stpcpy (p, style->prefix.s);
if (!add_grouping)
p = mempcpy (p, magnitude, integer_digits);
else
@@ -654,12 +655,11 @@ output_decimal (const struct rounder *r, const struct
fmt_spec *format,
p = mempcpy (p, &magnitude[integer_digits + 1], decimals);
}
if (add_affixes)
- p = mempcpy (p, ss_data (style->suffix), ss_length (style->suffix));
+ p = stpcpy (p, style->suffix.s);
if (add_neg_prefix)
- p = mempcpy (p, ss_data (style->neg_suffix),
- ss_length (style->neg_suffix));
+ p = stpcpy (p, style->neg_suffix.s);
else
- p = mempset (p, ' ', ss_length (style->neg_suffix));
+ p = mempset (p, ' ', strlen (style->neg_suffix.s));
assert (p == output + format->w);
return true;
@@ -681,9 +681,9 @@ output_scientific (double number, const struct fmt_spec
*format,
char buf[64], *p;
/* Allocate minimum required space. */
- width = 6 + ss_length (style->neg_suffix);
+ width = 6 + strlen (style->neg_suffix.s);
if (number < 0)
- width += ss_length (style->neg_prefix);
+ width += strlen (style->neg_prefix.s);
if (width > format->w)
return false;
@@ -706,10 +706,9 @@ output_scientific (double number, const struct fmt_spec
*format,
if (width < format->w)
p = mempset (p, ' ', format->w - width);
if (number < 0)
- p = mempcpy (p, ss_data (style->neg_prefix),
- ss_length (style->neg_prefix));
+ p = stpcpy (p, style->neg_prefix.s);
if (add_affixes)
- p = mempcpy (p, ss_data (style->prefix), ss_length (style->prefix));
+ p = stpcpy (p, style->prefix.s);
if (fraction_width > 0)
sprintf (p, "%#.*E", fraction_width - 1, fabs (number));
else
@@ -736,12 +735,11 @@ output_scientific (double number, const struct fmt_spec
*format,
/* Add suffixes. */
p = strchr (p, '\0');
if (add_affixes)
- p = mempcpy (p, ss_data (style->suffix), ss_length (style->suffix));
+ p = stpcpy (p, style->suffix.s);
if (number < 0)
- p = mempcpy (p, ss_data (style->neg_suffix),
- ss_length (style->neg_suffix));
+ p = stpcpy (p, style->neg_suffix.s);
else
- p = mempset (p, ' ', ss_length (style->neg_suffix));
+ p = mempset (p, ' ', strlen (style->neg_suffix.s));
assert (p == buf + format->w);
memcpy (output, buf, format->w);
diff --git a/src/data/format.c b/src/data/format.c
index 4ec3dc5..d3c6880 100644
--- a/src/data/format.c
+++ b/src/data/format.c
@@ -21,18 +21,19 @@
#include <ctype.h>
#include <stdlib.h>
-#include <data/identifier.h>
-#include <data/settings.h>
-#include <data/value.h>
-#include <data/variable.h>
-#include <libpspp/assertion.h>
-#include <libpspp/compiler.h>
-#include <libpspp/message.h>
-#include <libpspp/misc.h>
-#include <libpspp/str.h>
-
-#include "minmax.h"
-#include "xalloc.h"
+#include "data/identifier.h"
+#include "data/settings.h"
+#include "data/value.h"
+#include "data/variable.h"
+#include "libpspp/assertion.h"
+#include "libpspp/cast.h"
+#include "libpspp/compiler.h"
+#include "libpspp/message.h"
+#include "libpspp/misc.h"
+#include "libpspp/str.h"
+
+#include "gl/minmax.h"
+#include "gl/xalloc.h"
#include "gettext.h"
#define _(msgid) gettext (msgid)
@@ -48,6 +49,9 @@ static bool valid_width (enum fmt_type, int width, bool
for_input);
static int max_digits_for_bytes (int bytes);
+static void fmt_affix_set (struct fmt_affix *, const char *);
+static void fmt_affix_free (struct fmt_affix *);
+
static void fmt_number_style_init (struct fmt_number_style *);
static void fmt_number_style_clone (struct fmt_number_style *,
const struct fmt_number_style *);
@@ -124,10 +128,10 @@ fmt_settings_set_style (struct fmt_settings *settings,
enum fmt_type type,
fmt_number_style_destroy (style);
- ss_alloc_substring (&style->neg_prefix, ss_cstr (neg_prefix));
- ss_alloc_substring (&style->prefix, ss_cstr (prefix));
- ss_alloc_substring (&style->suffix, ss_cstr (suffix));
- ss_alloc_substring (&style->neg_suffix, ss_cstr (neg_suffix));
+ fmt_affix_set (&style->neg_prefix, neg_prefix);
+ fmt_affix_set (&style->prefix, prefix);
+ fmt_affix_set (&style->suffix, suffix);
+ fmt_affix_set (&style->neg_suffix, neg_suffix);
style->decimal = decimal;
style->grouping = grouping;
}
@@ -930,13 +934,28 @@ max_digits_for_bytes (int bytes)
return map[bytes - 1];
}
+/* Sets AFFIX's string value to S. */
+static void
+fmt_affix_set (struct fmt_affix *affix, const char *s)
+{
+ affix->s = s[0] == '\0' ? CONST_CAST (char *, "") : xstrdup (s);
+}
+
+/* Frees data in AFFIX. */
+static void
+fmt_affix_free (struct fmt_affix *affix)
+{
+ if (affix->s[0])
+ free (affix->s);
+}
+
static void
fmt_number_style_init (struct fmt_number_style *style)
{
- style->neg_prefix = ss_empty ();
- style->prefix = ss_empty ();
- style->suffix = ss_empty ();
- style->neg_suffix = ss_empty ();
+ fmt_affix_set (&style->neg_prefix, "");
+ fmt_affix_set (&style->prefix, "");
+ fmt_affix_set (&style->suffix, "");
+ fmt_affix_set (&style->neg_suffix, "");
style->decimal = '.';
style->grouping = 0;
}
@@ -945,10 +964,10 @@ static void
fmt_number_style_clone (struct fmt_number_style *new,
const struct fmt_number_style *old)
{
- ss_alloc_substring (&new->neg_prefix, old->neg_prefix);
- ss_alloc_substring (&new->prefix, old->prefix);
- ss_alloc_substring (&new->suffix, old->suffix);
- ss_alloc_substring (&new->neg_suffix, old->neg_suffix);
+ fmt_affix_set (&new->neg_prefix, old->neg_prefix.s);
+ fmt_affix_set (&new->prefix, old->prefix.s);
+ fmt_affix_set (&new->suffix, old->suffix.s);
+ fmt_affix_set (&new->neg_suffix, old->neg_suffix.s);
new->decimal = old->decimal;
new->grouping = old->grouping;
}
@@ -959,10 +978,10 @@ fmt_number_style_destroy (struct fmt_number_style *style)
{
if (style != NULL)
{
- ss_dealloc (&style->neg_prefix);
- ss_dealloc (&style->prefix);
- ss_dealloc (&style->suffix);
- ss_dealloc (&style->neg_suffix);
+ fmt_affix_free (&style->neg_prefix);
+ fmt_affix_free (&style->prefix);
+ fmt_affix_free (&style->suffix);
+ fmt_affix_free (&style->neg_suffix);
}
}
@@ -971,7 +990,7 @@ fmt_number_style_destroy (struct fmt_number_style *style)
int
fmt_affix_width (const struct fmt_number_style *style)
{
- return ss_length (style->prefix) + ss_length (style->suffix);
+ return strlen (style->prefix.s) + strlen (style->suffix.s);
}
/* Returns the total width of the negative prefix and suffix for
@@ -979,7 +998,7 @@ fmt_affix_width (const struct fmt_number_style *style)
int
fmt_neg_affix_width (const struct fmt_number_style *style)
{
- return ss_length (style->neg_prefix) + ss_length (style->neg_suffix);
+ return strlen (style->neg_prefix.s) + strlen (style->neg_suffix.s);
}
/* Returns the struct fmt_desc for the given format TYPE. */
diff --git a/src/data/format.h b/src/data/format.h
index 568dd3d..55643ab 100644
--- a/src/data/format.h
+++ b/src/data/format.h
@@ -1,5 +1,5 @@
/* PSPP - a program for statistical analysis.
- Copyright (C) 1997-9, 2000, 2006, 2010 Free Software Foundation, Inc.
+ Copyright (C) 1997-9, 2000, 2006, 2010, 2011 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -143,15 +143,21 @@ void fmt_settings_set_style (struct fmt_settings *, enum
fmt_type,
const char *neg_prefix, const char *prefix,
const char *suffix, const char *neg_suffix);
+/* A prefix or suffix for a numeric output format. */
+struct fmt_affix
+ {
+ char *s; /* String contents of affix. */
+ };
+
/* A numeric output style. */
struct fmt_number_style
{
- struct substring neg_prefix; /* Negative prefix. */
- struct substring prefix; /* Prefix. */
- struct substring suffix; /* Suffix. */
- struct substring neg_suffix; /* Negative suffix. */
- char decimal; /* Decimal point: '.' or ','. */
- char grouping; /* Grouping character: ',', '.', or 0. */
+ struct fmt_affix neg_prefix; /* Negative prefix. */
+ struct fmt_affix prefix; /* Prefix. */
+ struct fmt_affix suffix; /* Suffix. */
+ struct fmt_affix neg_suffix; /* Negative suffix. */
+ char decimal; /* Decimal point: '.' or ','. */
+ char grouping; /* Grouping character: ',', '.', or 0. */
};
int fmt_affix_width (const struct fmt_number_style *);
diff --git a/src/language/utilities/set.q b/src/language/utilities/set.q
index 5fa7406..5527470 100644
--- a/src/language/utilities/set.q
+++ b/src/language/utilities/set.q
@@ -1,5 +1,5 @@
/* PSPP - a program for statistical analysis.
- Copyright (C) 1997-9, 2000, 2006, 2009, 2010 Free Software Foundation, Inc.
+ Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011 Free Software
Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -536,11 +536,11 @@ show_blanks (const struct dataset *ds UNUSED)
}
static void
-format_cc (struct string *out, struct substring in, char grouping)
+format_cc (struct string *out, const char *in, char grouping)
{
- while (!ss_is_empty (in))
+ while (*in != '\0')
{
- char c = ss_get_byte (&in);
+ char c = *in++;
if (c == grouping || c == '\'')
ds_put_byte (out, '\'');
else if (c == '"')
@@ -556,13 +556,13 @@ show_cc (enum fmt_type type)
struct string out;
ds_init_empty (&out);
- format_cc (&out, cc->neg_prefix, cc->grouping);
+ format_cc (&out, cc->neg_prefix.s, cc->grouping);
ds_put_byte (&out, cc->grouping);
- format_cc (&out, cc->prefix, cc->grouping);
+ format_cc (&out, cc->prefix.s, cc->grouping);
ds_put_byte (&out, cc->grouping);
- format_cc (&out, cc->suffix, cc->grouping);
+ format_cc (&out, cc->suffix.s, cc->grouping);
ds_put_byte (&out, cc->grouping);
- format_cc (&out, cc->neg_suffix, cc->grouping);
+ format_cc (&out, cc->neg_suffix.s, cc->grouping);
return ds_cstr (&out);
}
--
1.7.2.3
- [formatting i18n 01/14] Use new Gnulib function dtoastr() to format short, accurate real numbers., (continued)
- [formatting i18n 01/14] Use new Gnulib function dtoastr() to format short, accurate real numbers., Ben Pfaff, 2011/02/19
- [formatting i18n 04/14] i18n: New function recode_byte()., Ben Pfaff, 2011/02/19
- [formatting i18n 10/14] format: Count prefix and suffix width in terms of display columns., Ben Pfaff, 2011/02/19
- [formatting i18n 08/14] data-out: Make each converter responsible for storing null terminator., Ben Pfaff, 2011/02/19
- [formatting i18n 12/14] pool: Support NULL pool argument to pool_alloc_unaligned()., Ben Pfaff, 2011/02/19
- [formatting i18n 14/14] data-out: Add test for non-ASCII custom currency formats., Ben Pfaff, 2011/02/19
- [formatting i18n 11/14] data-out: Reorganize output_Z() to be more easily understood., Ben Pfaff, 2011/02/19
- [formatting i18n 02/14] CROSSTABS: Eliminate redundant data copying., Ben Pfaff, 2011/02/19
- [formatting i18n 06/14] legacy-encoding: Remove., Ben Pfaff, 2011/02/19
- [formatting i18n 07/14] format: Increase abstraction of fmt_number_style., Ben Pfaff, 2011/02/19
- [formatting i18n 09/14] format: Create a new "struct fmt_affix" for prefix and suffix strings.,
Ben Pfaff <=
- [formatting i18n 05/14] i18n: Introduce C_ENCODING as replacement for LEGACY_NATIVE., Ben Pfaff, 2011/02/19
- [formatting i18n 13/14] data-out: Optimize and fix some bad assumptions., Ben Pfaff, 2011/02/19
- Re: [formatting i18n 00/14] Fix i18n of formatted data, Ben Pfaff, 2011/02/23