diff --git a/libc/src/__support/StringUtil/error_to_string.cpp b/libc/src/__support/StringUtil/error_to_string.cpp --- a/libc/src/__support/StringUtil/error_to_string.cpp +++ b/libc/src/__support/StringUtil/error_to_string.cpp @@ -37,9 +37,7 @@ constexpr size_t ERR_BUFFER_SIZE = max_buff_size(); thread_local char error_buffer[ERR_BUFFER_SIZE]; -constexpr size_t RAW_ARRAY_LEN = PLATFORM_ERRORS.size(); -constexpr size_t TOTAL_STR_LEN = - total_str_len(PLATFORM_ERRORS.data(), RAW_ARRAY_LEN); +constexpr size_t TOTAL_STR_LEN = total_str_len(PLATFORM_ERRORS); // Since the StringMappings array is a map from error numbers to their // corresponding strings, we have to have an array large enough we can use the @@ -47,11 +45,10 @@ // the maximum value being 133 (41 and 58 are skipped). If other platforms use // negative numbers or discontiguous ranges, then the array should be turned // into a proper hashmap. -constexpr size_t ERR_ARRAY_SIZE = - max_key_val(PLATFORM_ERRORS.data(), RAW_ARRAY_LEN) + 1; +constexpr size_t ERR_ARRAY_SIZE = max_key_val(PLATFORM_ERRORS) + 1; -static constexpr MessageMapper - error_mapper(PLATFORM_ERRORS.data(), RAW_ARRAY_LEN); +constexpr MessageMapper + error_mapper(PLATFORM_ERRORS); cpp::string_view build_error_string(int err_num, cpp::span buffer) { // if the buffer can't hold "Unknown error" + ' ' + num_str, then just diff --git a/libc/src/__support/StringUtil/message_mapper.h b/libc/src/__support/StringUtil/message_mapper.h --- a/libc/src/__support/StringUtil/message_mapper.h +++ b/libc/src/__support/StringUtil/message_mapper.h @@ -29,20 +29,22 @@ } }; -constexpr size_t total_str_len(const MsgMapping *array, size_t len) { +template using MsgTable = cpp::array; + +template constexpr size_t total_str_len(const MsgTable &table) { size_t total = 0; - for (size_t i = 0; i < len; ++i) { + for (size_t i = 0; i < table.size(); ++i) { // add 1 for the null terminator. - total += array[i].msg.size() + 1; + total += table[i].msg.size() + 1; } return total; } -constexpr size_t max_key_val(const MsgMapping *array, size_t len) { +template constexpr size_t max_key_val(const MsgTable &table) { int max = 0; - for (size_t i = 0; i < len; ++i) { - if (array[i].num > max) { - max = array[i].num; + for (size_t i = 0; i < table.size(); ++i) { + if (table[i].num > max) { + max = table[i].num; } } // max will never be negative since the starting value is 0. This is good, @@ -55,10 +57,10 @@ char string_array[TOTAL_STR_LEN] = {'\0'}; public: - constexpr MessageMapper(const MsgMapping raw_array[], size_t raw_array_len) { + template constexpr MessageMapper(const MsgTable &table) { cpp::string_view string_mappings[ARR_SIZE] = {""}; - for (size_t i = 0; i < raw_array_len; ++i) - string_mappings[raw_array[i].num] = raw_array[i].msg; + for (size_t i = 0; i < table.size(); ++i) + string_mappings[table[i].num] = table[i].msg; int string_array_index = 0; for (size_t cur_num = 0; cur_num < ARR_SIZE; ++cur_num) { @@ -86,8 +88,6 @@ } }; -template using MsgTable = cpp::array; - template constexpr MsgTable operator+(const MsgTable &t1, const MsgTable &t2) { diff --git a/libc/src/__support/StringUtil/signal_to_string.cpp b/libc/src/__support/StringUtil/signal_to_string.cpp --- a/libc/src/__support/StringUtil/signal_to_string.cpp +++ b/libc/src/__support/StringUtil/signal_to_string.cpp @@ -34,15 +34,12 @@ constexpr size_t SIG_BUFFER_SIZE = max_buff_size(); thread_local char signal_buffer[SIG_BUFFER_SIZE]; -constexpr size_t RAW_ARRAY_LEN = PLATFORM_SIGNALS.size(); -constexpr size_t TOTAL_STR_LEN = - total_str_len(PLATFORM_SIGNALS.data(), RAW_ARRAY_LEN); +constexpr size_t TOTAL_STR_LEN = total_str_len(PLATFORM_SIGNALS); -constexpr size_t SIG_ARRAY_SIZE = - max_key_val(PLATFORM_SIGNALS.data(), RAW_ARRAY_LEN) + 1; +constexpr size_t SIG_ARRAY_SIZE = max_key_val(PLATFORM_SIGNALS) + 1; -static constexpr MessageMapper - signal_mapper(PLATFORM_SIGNALS.data(), RAW_ARRAY_LEN); +constexpr MessageMapper + signal_mapper(PLATFORM_SIGNALS); cpp::string_view build_signal_string(int sig_num, cpp::span buffer) { cpp::string_view base_str;