From 50de5b3a5a70d3a139672bb1bac5ce003af7ff2d Mon Sep 17 00:00:00 2001 From: ado Date: Thu, 14 Mar 2024 04:40:07 +0100 Subject: [PATCH] Add const where fitting, make splitter class members private, add #pragma once to ssp.hpp --- include/ss/common.hpp | 6 ++-- include/ss/converter.hpp | 8 ++--- include/ss/extract.hpp | 8 ++--- include/ss/parser.hpp | 16 +++++----- include/ss/splitter.hpp | 11 +++++-- script/single_header_generator.py | 1 + ssp.hpp | 50 +++++++++++++++++-------------- 7 files changed, 56 insertions(+), 44 deletions(-) diff --git a/include/ss/common.hpp b/include/ss/common.hpp index 046e4e5..e507994 100644 --- a/include/ss/common.hpp +++ b/include/ss/common.hpp @@ -55,7 +55,7 @@ using ssize_t = intptr_t; std::array buff; if (lineptr == nullptr || n < sizeof(buff)) { - size_t new_n = sizeof(buff); + const size_t new_n = sizeof(buff); lineptr = static_cast(strict_realloc(lineptr, new_n)); n = new_n; } @@ -68,7 +68,7 @@ using ssize_t = intptr_t; size_t buff_used = std::strlen(buff.data()); if (n <= buff_used + line_used) { - size_t new_n = n * 2; + const size_t new_n = n * 2; lineptr = static_cast(strict_realloc(lineptr, new_n)); n = new_n; } @@ -105,7 +105,7 @@ using ssize_t = intptr_t; size_t line_used = 0; while (curr_char < csv_data_size) { if (line_used + 1 >= n) { - size_t new_n = n * 2; + const size_t new_n = n * 2; char* new_lineptr = static_cast(strict_realloc(lineptr, new_n)); diff --git a/include/ss/converter.hpp b/include/ss/converter.hpp index ca99122..06e67d2 100644 --- a/include/ss/converter.hpp +++ b/include/ss/converter.hpp @@ -122,7 +122,7 @@ public: line_ptr_type line, const std::string& delim = default_delimiter) { split(line, delim); if (splitter_.valid()) { - return convert(splitter_.split_data_); + return convert(splitter_.get_split_data()); } else { handle_error_bad_split(); return {}; @@ -164,7 +164,7 @@ public: // same as above, but uses cached split line template [[nodiscard]] no_void_validator_tup_t convert() { - return convert(splitter_.split_data_); + return convert(splitter_.get_split_data()); } [[nodiscard]] bool valid() const { @@ -190,9 +190,9 @@ public: // contain the beginnings and the ends of each column of the string const split_data& split(line_ptr_type line, const std::string& delim = default_delimiter) { - splitter_.split_data_.clear(); + splitter_.clear_split_data(); if (line[0] == '\0') { - return splitter_.split_data_; + return splitter_.get_split_data(); } return splitter_.split(line, delim); diff --git a/include/ss/extract.hpp b/include/ss/extract.hpp index 3f662c7..58cea4e 100644 --- a/include/ss/extract.hpp +++ b/include/ss/extract.hpp @@ -48,7 +48,7 @@ to_num(const char* const begin, const char* const end) { constexpr static auto buff_max = 64; std::array short_buff; - size_t string_range = std::distance(begin, end); + const size_t string_range = std::distance(begin, end); std::string long_buff; char* buff = nullptr; @@ -88,10 +88,10 @@ struct numeric_wrapper { using type = T; numeric_wrapper() = default; - numeric_wrapper(numeric_wrapper&&) = default; + numeric_wrapper(numeric_wrapper&&) noexcept = default; numeric_wrapper(const numeric_wrapper&) = default; - numeric_wrapper& operator=(numeric_wrapper&&) = default; + numeric_wrapper& operator=(numeric_wrapper&&) noexcept = default; numeric_wrapper& operator=(const numeric_wrapper&) = default; ~numeric_wrapper() = default; @@ -226,7 +226,7 @@ template <> } else { constexpr static auto true_size = 4; constexpr static auto false_size = 5; - size_t size = end - begin; + const size_t size = end - begin; if (size == true_size && std::strncmp(begin, "true", size) == 0) { value = true; } else if (size == false_size && diff --git a/include/ss/parser.hpp b/include/ss/parser.hpp index 112fe1d..856182b 100644 --- a/include/ss/parser.hpp +++ b/include/ss/parser.hpp @@ -67,8 +67,8 @@ public: } } - parser(parser&& other) = default; - parser& operator=(parser&& other) = default; + parser(parser&& other) noexcept = default; + parser& operator=(parser&& other) noexcept = default; ~parser() = default; parser() = delete; @@ -114,8 +114,6 @@ public: template [[nodiscard]] no_void_validator_tup_t get_next() { - std::optional error; - if (!eof_) { if constexpr (throw_on_error) { try { @@ -181,7 +179,7 @@ public: } std::vector split_header; - for (const auto& [begin, end] : splitter.split_data_) { + for (const auto& [begin, end] : splitter.get_split_data()) { split_header.emplace_back(begin, end); } @@ -267,11 +265,11 @@ public: } iterator(const iterator& other) = default; - iterator(iterator&& other) = default; + iterator(iterator&& other) noexcept = default; ~iterator() = default; iterator& operator=(const iterator& other) = delete; - iterator& operator=(iterator&& other) = delete; + iterator& operator=(iterator&& other) noexcept = delete; [[nodiscard]] value& operator*() { return value_; @@ -562,7 +560,7 @@ private: return; } - for (const auto& [begin, end] : splitter.split_data_) { + for (const auto& [begin, end] : splitter.get_split_data()) { std::string field{begin, end}; if (field.empty()) { handle_error_duplicate_header_field(field); @@ -841,7 +839,7 @@ private: std::free(helper_buffer_); if (file_) { - std::fclose(file_); + std::ignore = std::fclose(file_); } } diff --git a/include/ss/splitter.hpp b/include/ss/splitter.hpp index da15a9d..0f06499 100644 --- a/include/ss/splitter.hpp +++ b/include/ss/splitter.hpp @@ -55,6 +55,14 @@ public: return split_impl_select_delim(delimiter); } + [[nodiscard]] const split_data& get_split_data() const { + return split_data_; + } + + void clear_split_data() { + split_data_.clear(); + } + private: //////////////// // resplit @@ -84,7 +92,7 @@ private: } const auto [old_line, old_begin] = *std::prev(split_data_.end()); - size_t begin = old_begin - old_line - 1; + const size_t begin = old_begin - old_line - 1; // safety measure if (new_size != -1 && static_cast(new_size) < begin) { @@ -461,7 +469,6 @@ private: // members //////////////// -public: error_type error_{}; bool unterminated_quote_{false}; bool done_{true}; diff --git a/script/single_header_generator.py b/script/single_header_generator.py index 0fd5c2c..7aa9bfc 100755 --- a/script/single_header_generator.py +++ b/script/single_header_generator.py @@ -37,6 +37,7 @@ for header in headers: includes = sorted(set(includes)) +print('#pragma once') print('\n'.join(includes)) print('#define SSP_DISABLE_FAST_FLOAT') print('\n'.join(combined_file)) diff --git a/ssp.hpp b/ssp.hpp index 52e313e..94227ff 100644 --- a/ssp.hpp +++ b/ssp.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include #include @@ -671,7 +672,7 @@ using ssize_t = intptr_t; std::array buff; if (lineptr == nullptr || n < sizeof(buff)) { - size_t new_n = sizeof(buff); + const size_t new_n = sizeof(buff); lineptr = static_cast(strict_realloc(lineptr, new_n)); n = new_n; } @@ -684,7 +685,7 @@ using ssize_t = intptr_t; size_t buff_used = std::strlen(buff.data()); if (n <= buff_used + line_used) { - size_t new_n = n * 2; + const size_t new_n = n * 2; lineptr = static_cast(strict_realloc(lineptr, new_n)); n = new_n; } @@ -721,7 +722,7 @@ using ssize_t = intptr_t; size_t line_used = 0; while (curr_char < csv_data_size) { if (line_used + 1 >= n) { - size_t new_n = n * 2; + const size_t new_n = n * 2; char* new_lineptr = static_cast(strict_realloc(lineptr, new_n)); @@ -1109,6 +1110,14 @@ public: return split_impl_select_delim(delimiter); } + [[nodiscard]] const split_data& get_split_data() const { + return split_data_; + } + + void clear_split_data() { + split_data_.clear(); + } + private: //////////////// // resplit @@ -1138,7 +1147,7 @@ private: } const auto [old_line, old_begin] = *std::prev(split_data_.end()); - size_t begin = old_begin - old_line - 1; + const size_t begin = old_begin - old_line - 1; // safety measure if (new_size != -1 && static_cast(new_size) < begin) { @@ -1515,7 +1524,6 @@ private: // members //////////////// -public: error_type error_{}; bool unterminated_quote_{false}; bool done_{true}; @@ -1570,7 +1578,7 @@ to_num(const char* const begin, const char* const end) { constexpr static auto buff_max = 64; std::array short_buff; - size_t string_range = std::distance(begin, end); + const size_t string_range = std::distance(begin, end); std::string long_buff; char* buff = nullptr; @@ -1610,10 +1618,10 @@ struct numeric_wrapper { using type = T; numeric_wrapper() = default; - numeric_wrapper(numeric_wrapper&&) = default; + numeric_wrapper(numeric_wrapper&&) noexcept = default; numeric_wrapper(const numeric_wrapper&) = default; - numeric_wrapper& operator=(numeric_wrapper&&) = default; + numeric_wrapper& operator=(numeric_wrapper&&) noexcept = default; numeric_wrapper& operator=(const numeric_wrapper&) = default; ~numeric_wrapper() = default; @@ -1748,7 +1756,7 @@ template <> } else { constexpr static auto true_size = 4; constexpr static auto false_size = 5; - size_t size = end - begin; + const size_t size = end - begin; if (size == true_size && std::strncmp(begin, "true", size) == 0) { value = true; } else if (size == false_size && @@ -1898,7 +1906,7 @@ public: line_ptr_type line, const std::string& delim = default_delimiter) { split(line, delim); if (splitter_.valid()) { - return convert(splitter_.split_data_); + return convert(splitter_.get_split_data()); } else { handle_error_bad_split(); return {}; @@ -1940,7 +1948,7 @@ public: // same as above, but uses cached split line template [[nodiscard]] no_void_validator_tup_t convert() { - return convert(splitter_.split_data_); + return convert(splitter_.get_split_data()); } [[nodiscard]] bool valid() const { @@ -1966,9 +1974,9 @@ public: // contain the beginnings and the ends of each column of the string const split_data& split(line_ptr_type line, const std::string& delim = default_delimiter) { - splitter_.split_data_.clear(); + splitter_.clear_split_data(); if (line[0] == '\0') { - return splitter_.split_data_; + return splitter_.get_split_data(); } return splitter_.split(line, delim); @@ -2333,8 +2341,8 @@ public: } } - parser(parser&& other) = default; - parser& operator=(parser&& other) = default; + parser(parser&& other) noexcept = default; + parser& operator=(parser&& other) noexcept = default; ~parser() = default; parser() = delete; @@ -2380,8 +2388,6 @@ public: template [[nodiscard]] no_void_validator_tup_t get_next() { - std::optional error; - if (!eof_) { if constexpr (throw_on_error) { try { @@ -2447,7 +2453,7 @@ public: } std::vector split_header; - for (const auto& [begin, end] : splitter.split_data_) { + for (const auto& [begin, end] : splitter.get_split_data()) { split_header.emplace_back(begin, end); } @@ -2533,11 +2539,11 @@ public: } iterator(const iterator& other) = default; - iterator(iterator&& other) = default; + iterator(iterator&& other) noexcept = default; ~iterator() = default; iterator& operator=(const iterator& other) = delete; - iterator& operator=(iterator&& other) = delete; + iterator& operator=(iterator&& other) noexcept = delete; [[nodiscard]] value& operator*() { return value_; @@ -2828,7 +2834,7 @@ private: return; } - for (const auto& [begin, end] : splitter.split_data_) { + for (const auto& [begin, end] : splitter.get_split_data()) { std::string field{begin, end}; if (field.empty()) { handle_error_duplicate_header_field(field); @@ -3107,7 +3113,7 @@ private: std::free(helper_buffer_); if (file_) { - std::fclose(file_); + std::ignore = std::fclose(file_); } }