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).
ds_extend (&tmp, 64);
/* Prefix character may precede sign. */
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));
}
ss_ltrim (&i->input, ss_cstr (CC_SPACES));
}
}
/* Prefix character may follow sign. */
}
/* 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));
}
ss_ltrim (&i->input, ss_cstr (CC_SPACES));
}
}
/* Suffix character. */
}
/* 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))
{
if (!ss_is_empty (i->input))
{
data_out_pool (const union value *input, const char *encoding,
const struct fmt_spec *format, struct pool *pool)
{
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 *t ;
assert (fmt_check_output (format));
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);
converters[format->type] (input, format, output);
t = recode_string_pool (UTF8, encoding, output, format->w, pool);
the negative suffix, plus (if negative) the negative
prefix. */
width = rounder_width (r, decimals, &integer_digits, &add_neg_prefix);
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);
- width += ss_length (style->neg_prefix);
+ width += strlen (style->neg_prefix.s);
if (width > format->w)
continue;
if (width > format->w)
continue;
if (format->w > width)
p = mempset (p, ' ', format->w - width);
if (add_neg_prefix)
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);
- 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
if (!add_grouping)
p = mempcpy (p, magnitude, integer_digits);
else
p = mempcpy (p, &magnitude[integer_digits + 1], decimals);
}
if (add_affixes)
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);
- p = mempcpy (p, ss_data (style->neg_suffix),
- ss_length (style->neg_suffix));
+ p = stpcpy (p, style->neg_suffix.s);
- p = mempset (p, ' ', ss_length (style->neg_suffix));
+ p = mempset (p, ' ', strlen (style->neg_suffix.s));
assert (p == output + format->w);
return true;
assert (p == output + format->w);
return true;
char buf[64], *p;
/* Allocate minimum required space. */
char buf[64], *p;
/* Allocate minimum required space. */
- width = 6 + ss_length (style->neg_suffix);
+ width = 6 + strlen (style->neg_suffix.s);
- width += ss_length (style->neg_prefix);
+ width += strlen (style->neg_prefix.s);
if (width > format->w)
return false;
if (width > format->w)
return false;
if (width < format->w)
p = mempset (p, ' ', format->w - width);
if (number < 0)
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);
- 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
if (fraction_width > 0)
sprintf (p, "%#.*E", fraction_width - 1, fabs (number));
else
/* Add suffixes. */
p = strchr (p, '\0');
if (add_affixes)
/* 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);
- p = mempcpy (p, ss_data (style->neg_suffix),
- ss_length (style->neg_suffix));
+ p = stpcpy (p, style->neg_suffix.s);
- 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);
assert (p == buf + format->w);
memcpy (output, buf, format->w);
#include <ctype.h>
#include <stdlib.h>
#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)
#include "gettext.h"
#define _(msgid) gettext (msgid)
static int max_digits_for_bytes (int bytes);
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 *);
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 *);
fmt_number_style_destroy (style);
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;
}
style->decimal = decimal;
style->grouping = grouping;
}
return map[bytes - 1];
}
\f
return map[bytes - 1];
}
\f
+/* 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)
{
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;
}
style->decimal = '.';
style->grouping = 0;
}
fmt_number_style_clone (struct fmt_number_style *new,
const struct fmt_number_style *old)
{
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;
}
new->decimal = old->decimal;
new->grouping = old->grouping;
}
- 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);
int
fmt_affix_width (const 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
}
/* Returns the total width of the negative prefix and suffix for
int
fmt_neg_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. */
}
/* Returns the struct fmt_desc for the given format TYPE. */
/* PSPP - a program for statistical analysis.
/* 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
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
const char *neg_prefix, const char *prefix,
const char *suffix, const char *neg_suffix);
\f
const char *neg_prefix, const char *prefix,
const char *suffix, const char *neg_suffix);
\f
+/* 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
{
/* 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 *);
};
int fmt_affix_width (const struct fmt_number_style *);
/* PSPP - a program for statistical analysis.
/* 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
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
-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))
- char c = ss_get_byte (&in);
if (c == grouping || c == '\'')
ds_put_byte (out, '\'');
else if (c == '"')
if (c == grouping || c == '\'')
ds_put_byte (out, '\'');
else if (c == '"')
struct string out;
ds_init_empty (&out);
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);
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);
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);
ds_put_byte (&out, cc->grouping);
- format_cc (&out, cc->neg_suffix, cc->grouping);
+ format_cc (&out, cc->neg_suffix.s, cc->grouping);