diff --git a/include/internal/basic_csv_parser.hpp b/include/internal/basic_csv_parser.hpp index d76b2d9..7463c09 100644 --- a/include/internal/basic_csv_parser.hpp +++ b/include/internal/basic_csv_parser.hpp @@ -28,19 +28,11 @@ namespace csv { * the CSVReader::ParseFlags enum */ HEDLEY_CONST CONSTEXPR_17 ParseFlagMap make_parse_flags(char delimiter) { - std::array ret = {}; - for (int i = -128; i < 128; i++) { - const int arr_idx = i + 128; - char ch = char(i); - - if (ch == delimiter) - ret[arr_idx] = ParseFlags::DELIMITER; - else if (ch == '\r' || ch == '\n') - ret[arr_idx] = ParseFlags::NEWLINE; - else - ret[arr_idx] = ParseFlags::NOT_SPECIAL; - } - + std::array ret = {}; + for (auto& e: ret) { e = ParseFlags::NOT_SPECIAL; } + ret[delimiter + 128] = ParseFlags::DELIMITER; + ret['\r' + 128] = ParseFlags::NEWLINE; + ret['\n' + 128] = ParseFlags::NEWLINE; return ret; } @@ -49,7 +41,7 @@ namespace csv { * the CSVReader::ParseFlags enum */ HEDLEY_CONST CONSTEXPR_17 ParseFlagMap make_parse_flags(char delimiter, char quote_char) { - std::array ret = make_parse_flags(delimiter); + std::array ret = make_parse_flags(delimiter); ret[(size_t)quote_char + 128] = ParseFlags::QUOTE; return ret; } @@ -59,19 +51,11 @@ namespace csv { * c is a whitespace character */ HEDLEY_CONST CONSTEXPR_17 WhitespaceMap make_ws_flags(const char* ws_chars, size_t n_chars) { - std::array ret = {}; - for (int i = -128; i < 128; i++) { - const int arr_idx = i + 128; - char ch = char(i); - ret[arr_idx] = false; - - for (size_t j = 0; j < n_chars; j++) { - if (ws_chars[j] == ch) { - ret[arr_idx] = true; - } - } + std::array ret = {}; + for (auto& e: ret) { e = false; } + for (int i = 0; i < n_chars; i++) { + ret[ws_chars[i] + 128] = true; } - return ret; } diff --git a/include/internal/common.hpp b/include/internal/common.hpp index 7e3ae18..2459db7 100644 --- a/include/internal/common.hpp +++ b/include/internal/common.hpp @@ -197,10 +197,10 @@ namespace csv { STATIC_ASSERT(quote_escape_flag(ParseFlags::NEWLINE, true) == ParseFlags::NOT_SPECIAL); /** An array which maps ASCII chars to a parsing flag */ - using ParseFlagMap = std::array; + using ParseFlagMap = std::array; /** An array which maps ASCII chars to a flag indicating if it is whitespace */ - using WhitespaceMap = std::array; + using WhitespaceMap = std::array; } /** Integer indicating a requested column wasn't found. */ diff --git a/single_include/csv.hpp b/single_include/csv.hpp index 811c8e1..830c1b6 100644 --- a/single_include/csv.hpp +++ b/single_include/csv.hpp @@ -4838,10 +4838,10 @@ namespace csv { STATIC_ASSERT(quote_escape_flag(ParseFlags::NEWLINE, true) == ParseFlags::NOT_SPECIAL); /** An array which maps ASCII chars to a parsing flag */ - using ParseFlagMap = std::array; + using ParseFlagMap = std::array; /** An array which maps ASCII chars to a flag indicating if it is whitespace */ - using WhitespaceMap = std::array; + using WhitespaceMap = std::array; } /** Integer indicating a requested column wasn't found. */ @@ -5867,19 +5867,11 @@ namespace csv { * the CSVReader::ParseFlags enum */ HEDLEY_CONST CONSTEXPR_17 ParseFlagMap make_parse_flags(char delimiter) { - std::array ret = {}; - for (int i = -128; i < 128; i++) { - const int arr_idx = i + 128; - char ch = char(i); - - if (ch == delimiter) - ret[arr_idx] = ParseFlags::DELIMITER; - else if (ch == '\r' || ch == '\n') - ret[arr_idx] = ParseFlags::NEWLINE; - else - ret[arr_idx] = ParseFlags::NOT_SPECIAL; - } - + std::array ret = {}; + for (auto& e: ret) { e = ParseFlags::NOT_SPECIAL; } + ret[delimiter + 128] = ParseFlags::DELIMITER; + ret['\r' + 128] = ParseFlags::NEWLINE; + ret['\n' + 128] = ParseFlags::NEWLINE; return ret; } @@ -5888,7 +5880,7 @@ namespace csv { * the CSVReader::ParseFlags enum */ HEDLEY_CONST CONSTEXPR_17 ParseFlagMap make_parse_flags(char delimiter, char quote_char) { - std::array ret = make_parse_flags(delimiter); + std::array ret = make_parse_flags(delimiter); ret[(size_t)quote_char + 128] = ParseFlags::QUOTE; return ret; } @@ -5898,19 +5890,11 @@ namespace csv { * c is a whitespace character */ HEDLEY_CONST CONSTEXPR_17 WhitespaceMap make_ws_flags(const char* ws_chars, size_t n_chars) { - std::array ret = {}; - for (int i = -128; i < 128; i++) { - const int arr_idx = i + 128; - char ch = char(i); - ret[arr_idx] = false; - - for (size_t j = 0; j < n_chars; j++) { - if (ws_chars[j] == ch) { - ret[arr_idx] = true; - } - } + std::array ret = {}; + for (auto& e: ret) { e = false; } + for (int i = 0; i < n_chars; i++) { + ret[ws_chars[i] + 128] = true; } - return ret; } diff --git a/single_include_test/csv.hpp b/single_include_test/csv.hpp index 811c8e1..830c1b6 100644 --- a/single_include_test/csv.hpp +++ b/single_include_test/csv.hpp @@ -4838,10 +4838,10 @@ namespace csv { STATIC_ASSERT(quote_escape_flag(ParseFlags::NEWLINE, true) == ParseFlags::NOT_SPECIAL); /** An array which maps ASCII chars to a parsing flag */ - using ParseFlagMap = std::array; + using ParseFlagMap = std::array; /** An array which maps ASCII chars to a flag indicating if it is whitespace */ - using WhitespaceMap = std::array; + using WhitespaceMap = std::array; } /** Integer indicating a requested column wasn't found. */ @@ -5867,19 +5867,11 @@ namespace csv { * the CSVReader::ParseFlags enum */ HEDLEY_CONST CONSTEXPR_17 ParseFlagMap make_parse_flags(char delimiter) { - std::array ret = {}; - for (int i = -128; i < 128; i++) { - const int arr_idx = i + 128; - char ch = char(i); - - if (ch == delimiter) - ret[arr_idx] = ParseFlags::DELIMITER; - else if (ch == '\r' || ch == '\n') - ret[arr_idx] = ParseFlags::NEWLINE; - else - ret[arr_idx] = ParseFlags::NOT_SPECIAL; - } - + std::array ret = {}; + for (auto& e: ret) { e = ParseFlags::NOT_SPECIAL; } + ret[delimiter + 128] = ParseFlags::DELIMITER; + ret['\r' + 128] = ParseFlags::NEWLINE; + ret['\n' + 128] = ParseFlags::NEWLINE; return ret; } @@ -5888,7 +5880,7 @@ namespace csv { * the CSVReader::ParseFlags enum */ HEDLEY_CONST CONSTEXPR_17 ParseFlagMap make_parse_flags(char delimiter, char quote_char) { - std::array ret = make_parse_flags(delimiter); + std::array ret = make_parse_flags(delimiter); ret[(size_t)quote_char + 128] = ParseFlags::QUOTE; return ret; } @@ -5898,19 +5890,11 @@ namespace csv { * c is a whitespace character */ HEDLEY_CONST CONSTEXPR_17 WhitespaceMap make_ws_flags(const char* ws_chars, size_t n_chars) { - std::array ret = {}; - for (int i = -128; i < 128; i++) { - const int arr_idx = i + 128; - char ch = char(i); - ret[arr_idx] = false; - - for (size_t j = 0; j < n_chars; j++) { - if (ws_chars[j] == ch) { - ret[arr_idx] = true; - } - } + std::array ret = {}; + for (auto& e: ret) { e = false; } + for (int i = 0; i < n_chars; i++) { + ret[ws_chars[i] + 128] = true; } - return ret; }