Skip to content

Commit

Permalink
use list initialization to construct xcsv char map table.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteven4 committed Nov 24, 2021
1 parent d62f479 commit f483fc3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
15 changes: 4 additions & 11 deletions xcsv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const QHash<QString, XcsvStyle::xcsv_token> XcsvStyle::xcsv_tokens {
};

/* a table of config file constants mapped to chars */
const XcsvStyle::char_map_t XcsvStyle::xcsv_char_table[] = {
const QHash<QString, QString> XcsvStyle::xcsv_char_table {
{ "COMMA", "," },
{ "COMMASPACE", ", " },
{ "SINGLEQUOTE", "'" },
Expand All @@ -172,22 +172,15 @@ const XcsvStyle::char_map_t XcsvStyle::xcsv_char_table[] = {
{ "SPACE", " " },
{ "HASH", "#" },
{ "WHITESPACE", "\\w" },
{ "PIPE", "|" },
{ nullptr, nullptr }
{ "PIPE", "|" }
};

// Given a keyword of "COMMASPACE", return ", ".
QString
XcsvStyle::xcsv_get_char_from_constant_table(const QString& key)
{
static QHash<QString, QString> substitutions;
if (substitutions.empty()) {
for (const char_map_t* cm = xcsv_char_table; !cm->key.isNull(); cm++) {
substitutions.insert(cm->key, cm->chars);
}
}
if (substitutions.contains(key)) {
return substitutions[key];
if (xcsv_char_table.contains(key)) {
return xcsv_char_table[key];
}
// No substitution found? Just return original.
return key;
Expand Down
8 changes: 1 addition & 7 deletions xcsv.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,6 @@ class XcsvStyle
private:
/* Types */

/* something to map config file constants to chars */
struct char_map_t {
const QString key;
const QString chars;
};

/* Member Functions */

static QString dequote(const QString& in);
Expand All @@ -264,7 +258,7 @@ class XcsvStyle
static const QHash<QString, xcsv_token> xcsv_tokens;

/* a table of config file constants mapped to chars */
static const char_map_t xcsv_char_table[];
static const QHash<QString, QString> xcsv_char_table;
};

class XcsvFormat : public Format
Expand Down

0 comments on commit f483fc3

Please sign in to comment.