From ac78b7ffb82debb50c3d46655919315dfcaf9257 Mon Sep 17 00:00:00 2001 From: Jessica James Date: Tue, 7 Dec 2021 01:07:58 -0600 Subject: [PATCH] Move potentially useful vectorize_ntargs from app_parameters.cpp to .hpp; general cleanup --- src/common/app_parameters.cpp | 40 +++----------------- src/include/jessilib/app_parameters.hpp | 50 ++++++++++++++++++++++++- src/include/jessilib/unicode.hpp | 2 +- 3 files changed, 54 insertions(+), 38 deletions(-) diff --git a/src/common/app_parameters.cpp b/src/common/app_parameters.cpp index ed9ccae..3eaa3b3 100644 --- a/src/common/app_parameters.cpp +++ b/src/common/app_parameters.cpp @@ -1,5 +1,5 @@ /** - * Copyright (C) 2019 Jessica James. + * Copyright (C) 2019-2021 Jessica James. * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -22,53 +22,23 @@ namespace jessilib { -template, char>>* = nullptr> -std::vector vectorize(CharT** in_ntarg_array) { - std::vector result; - if (in_ntarg_array == nullptr) { - return result; - } - - for (auto argv = in_ntarg_array; *argv != nullptr; ++argv) { - result.emplace_back(mbstring_to_ustring(*argv).second); - } - - return result; -} - -template, wchar_t>>* = nullptr> -std::vector vectorize(CharT** in_ntarg_array) { - std::vector result; - if (in_ntarg_array == nullptr) { - return result; - } - - for (auto argv = in_ntarg_array; *argv != nullptr; ++argv) { - result.emplace_back(jessilib::string_cast(std::wstring_view{ *argv })); - } - - return result; -} - app_parameters::app_parameters(int, char** in_argv, char** in_envp) - : app_parameters{ vectorize(in_argv), vectorize(in_envp) } { + : app_parameters{ vectorize_ntargs(in_argv), vectorize_ntargs(in_envp) } { // Empty ctor body } app_parameters::app_parameters(int, const char** in_argv, const char** in_envp) - : app_parameters{ vectorize(in_argv), vectorize(in_envp) } { + : app_parameters{ vectorize_ntargs(in_argv), vectorize_ntargs(in_envp) } { // Empty ctor body } app_parameters::app_parameters(int, wchar_t** in_argv, wchar_t** in_envp) - : app_parameters{ vectorize(in_argv), vectorize(in_envp) } { + : app_parameters{ vectorize_ntargs(in_argv), vectorize_ntargs(in_envp) } { // Empty ctor body } app_parameters::app_parameters(int, const wchar_t** in_argv, const wchar_t** in_envp) - : app_parameters{ vectorize(in_argv), vectorize(in_envp) } { + : app_parameters{ vectorize_ntargs(in_argv), vectorize_ntargs(in_envp) } { // Empty ctor body } diff --git a/src/include/jessilib/app_parameters.hpp b/src/include/jessilib/app_parameters.hpp index f3b4ed3..94596d5 100644 --- a/src/include/jessilib/app_parameters.hpp +++ b/src/include/jessilib/app_parameters.hpp @@ -1,5 +1,5 @@ /** - * Copyright (C) 2019 Jessica James. + * Copyright (C) 2019-2021 Jessica James. * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -17,7 +17,7 @@ */ #include "object.hpp" -#include "unicode_compare.hpp" +#include "unicode.hpp" namespace jessilib { @@ -61,4 +61,50 @@ private: map_type m_values; }; +/** + * Converts null-terminated argument array of null-terminated strings to a vector of unicode strings + * + * @tparam OutCharT Unicode character data type + * @tparam InCharT Input character type (char for multi-byte string, or wchar_t for wide character strings) + * @param in_ntarg_array Null-terminated argument array to vectorize + * @return A vector of unicode strings recoded from the input + */ +template, char>>* = nullptr> +std::vector> vectorize_ntargs(InCharT** in_ntarg_array) { + std::vector> result; + if (in_ntarg_array == nullptr) { + return result; + } + + for (auto argv = in_ntarg_array; *argv != nullptr; ++argv) { + result.emplace_back(mbstring_to_ustring(*argv).second); + } + + return result; +} + +/** + * Converts null-terminated argument array of null-terminated strings to a vector of unicode strings + * + * @tparam OutCharT Unicode character data type + * @tparam InCharT Input character type (char for multi-byte string, or wchar_t for wide character strings) + * @param in_ntarg_array Null-terminated argument array to vectorize + * @return A vector of unicode strings recoded from the input + */ +template, wchar_t>>* = nullptr> +std::vector> vectorize_ntargs(InCharT** in_ntarg_array) { + std::vector> result; + if (in_ntarg_array == nullptr) { + return result; + } + + for (auto argv = in_ntarg_array; *argv != nullptr; ++argv) { + result.emplace_back(jessilib::string_cast(std::wstring_view{ *argv })); + } + + return result; +} + } // namespace jessilib diff --git a/src/include/jessilib/unicode.hpp b/src/include/jessilib/unicode.hpp index ec6f6e9..30277ce 100644 --- a/src/include/jessilib/unicode.hpp +++ b/src/include/jessilib/unicode.hpp @@ -198,7 +198,7 @@ std::pair> mbstring_to_ustring(std::string_view i return result; } - // bytes_read will never be 0 except for null characters, which are excluded from our view; here for future reuse + // bytes_read is 0 for null characters; ensure null characters are also removed from the view bytes_read = std::max(size_t{1}, bytes_read); in_mbstring.remove_prefix(bytes_read); encode_codepoint(result.second, codepoint);