Browse Source

Move potentially useful vectorize_ntargs from app_parameters.cpp to .hpp; general cleanup

master
Jessica James 3 years ago
parent
commit
ac78b7ffb8
  1. 40
      src/common/app_parameters.cpp
  2. 50
      src/include/jessilib/app_parameters.hpp
  3. 2
      src/include/jessilib/unicode.hpp

40
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 * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -22,53 +22,23 @@
namespace jessilib { namespace jessilib {
template<typename CharT,
std::enable_if_t<std::is_same_v<std::remove_cvref_t<CharT>, char>>* = nullptr>
std::vector<app_parameters::string_type> vectorize(CharT** in_ntarg_array) {
std::vector<app_parameters::string_type> result;
if (in_ntarg_array == nullptr) {
return result;
}
for (auto argv = in_ntarg_array; *argv != nullptr; ++argv) {
result.emplace_back(mbstring_to_ustring<char8_t>(*argv).second);
}
return result;
}
template<typename CharT,
std::enable_if_t<std::is_same_v<std::remove_cvref_t<CharT>, wchar_t>>* = nullptr>
std::vector<app_parameters::string_type> vectorize(CharT** in_ntarg_array) {
std::vector<app_parameters::string_type> result;
if (in_ntarg_array == nullptr) {
return result;
}
for (auto argv = in_ntarg_array; *argv != nullptr; ++argv) {
result.emplace_back(jessilib::string_cast<char8_t>(std::wstring_view{ *argv }));
}
return result;
}
app_parameters::app_parameters(int, char** in_argv, char** in_envp) 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 // Empty ctor body
} }
app_parameters::app_parameters(int, const char** in_argv, const char** in_envp) 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 // Empty ctor body
} }
app_parameters::app_parameters(int, wchar_t** in_argv, wchar_t** in_envp) 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 // Empty ctor body
} }
app_parameters::app_parameters(int, const wchar_t** in_argv, const wchar_t** in_envp) 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 // Empty ctor body
} }

50
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 * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -17,7 +17,7 @@
*/ */
#include "object.hpp" #include "object.hpp"
#include "unicode_compare.hpp" #include "unicode.hpp"
namespace jessilib { namespace jessilib {
@ -61,4 +61,50 @@ private:
map_type m_values; 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<typename OutCharT = char8_t, typename InCharT,
std::enable_if_t<std::is_same_v<std::remove_cvref_t<InCharT>, char>>* = nullptr>
std::vector<std::basic_string<OutCharT>> vectorize_ntargs(InCharT** in_ntarg_array) {
std::vector<std::basic_string<OutCharT>> result;
if (in_ntarg_array == nullptr) {
return result;
}
for (auto argv = in_ntarg_array; *argv != nullptr; ++argv) {
result.emplace_back(mbstring_to_ustring<OutCharT>(*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<typename OutCharT = char8_t, typename InCharT,
std::enable_if_t<std::is_same_v<std::remove_cvref_t<InCharT>, wchar_t>>* = nullptr>
std::vector<std::basic_string<OutCharT>> vectorize_ntargs(InCharT** in_ntarg_array) {
std::vector<std::basic_string<OutCharT>> result;
if (in_ntarg_array == nullptr) {
return result;
}
for (auto argv = in_ntarg_array; *argv != nullptr; ++argv) {
result.emplace_back(jessilib::string_cast<OutCharT>(std::wstring_view{ *argv }));
}
return result;
}
} // namespace jessilib } // namespace jessilib

2
src/include/jessilib/unicode.hpp

@ -198,7 +198,7 @@ std::pair<bool, std::basic_string<CharT>> mbstring_to_ustring(std::string_view i
return result; 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); bytes_read = std::max(size_t{1}, bytes_read);
in_mbstring.remove_prefix(bytes_read); in_mbstring.remove_prefix(bytes_read);
encode_codepoint(result.second, codepoint); encode_codepoint(result.second, codepoint);

Loading…
Cancel
Save