Skip to content

Commit

Permalink
🙃 This is honestly the most I can handle right now.
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePhD committed Aug 15, 2021
1 parent 911a31d commit 8cb532e
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 85 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,19 @@ if (NOT TARGET Iconv::Iconv AND ZTD_TEXT_USE_LIBICONV)
endif()
# define generator expressions for each moment
string(CONCAT ztd-text-libiconv-define
$<IF:$<BOOL:ZTD_TEXT_USE_LIBICONV>,
$<IF:$<BOOL:${ZTD_TEXT_USE_LIBICONV}>,
ZTD_LIBICONV=1,
ZTD_LIBICONV=0
>
)
string(CONCAT ztd-text-libiconv-load-define
$<IF:$<AND:$<BOOL:Iconv_FOUND>,$<BOOL:ZTD_TEXT_USE_LIBICONV>>,
$<IF:$<AND:$<BOOL:${Iconv_FOUND}>,$<BOOL:${ZTD_TEXT_USE_LIBICONV}>>,
ZTD_LIBICONV_LOAD=0,
ZTD_LIBICONV_LOAD=1
>
)
string(CONCAT ztd-text-libiconv-header-define
$<IF:$<AND:$<BOOL:Iconv_FOUND>,$<BOOL:ZTD_TEXT_USE_LIBICONV>>,
$<IF:$<AND:$<BOOL:${Iconv_FOUND}>,$<BOOL:${ZTD_TEXT_USE_LIBICONV}>>,
ZTD_ICONV_H=1,
ZTD_ICONV_H=0
>
Expand All @@ -172,7 +172,7 @@ string(CONCAT ztd-text-libiconv-dl
)
string(CONCAT ztd-text-static-libiconv-define
$<IF:
$<AND:$<BOOL:Iconv_FOUND>, $<BOOL:ZTD_TEXT_USE_STATIC_LIBICONV>,
$<AND:$<BOOL:${Iconv_FOUND}>, $<BOOL:${ZTD_TEXT_USE_STATIC_LIBICONV}>,
$<STREQUAL:
$<$<BOOL:$<TARGET_NAME_IF_EXISTS:Iconv::Iconv>>:$<TARGET_PROPERTY:Iconv::Iconv,TYPE>>,
STATIC_LIBRARY
Expand Down
196 changes: 141 additions & 55 deletions include/ztd/text/basic_iconv.hpp

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions include/ztd/text/detail/encoding_name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
#include <ztd/text/encoding_scheme.hpp>
#include <ztd/text/ascii.hpp>
#include <ztd/text/no_encoding.hpp>
#include <ztd/text/iconv_names.hpp>

#include <ztd/idk/detail/encoding_name.hpp>
#include <ztd/idk/type_traits.hpp>

#include <string_view>

Expand Down Expand Up @@ -90,6 +92,26 @@ namespace ztd { namespace text {
}
}

template <typename _CodePoint>
c_string_view __platform_utf_name() {
constexpr ::std::size_t __bits = sizeof(_CodePoint) * CHAR_BIT;
if constexpr (__bits <= 8) {
return iconv_utf8_name;
}
else if constexpr (__bits <= 16) {
return iconv_utf16_name;
}
else if constexpr (__bits <= 32) {
return iconv_utf32_name;
}
else {
static_assert(always_false_v<_CodePoint>,
"[ztd.text] There is no good known default platform encoding for the given bit size in this "
"platform. File a bug and tell us about your needs!");
return iconv_locale_name;
}
}

} // namespace __txt_detail

ZTD_TEXT_INLINE_ABI_NAMESPACE_CLOSE_I_
Expand Down
58 changes: 58 additions & 0 deletions include/ztd/text/iconv_names.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// =============================================================================
//
// ztd.text
// Copyright © 2021 JeanHeyd "ThePhD" Meneide and Shepherd's Oasis, LLC
// Contact: opensource@soasis.org
//
// Commercial License Usage
// Licensees holding valid commercial ztd.text licenses may use this file in
// accordance with the commercial license agreement provided with the
// Software or, alternatively, in accordance with the terms contained in
// a written agreement between you and Shepherd's Oasis, LLC.
// For licensing terms and conditions see your agreement. For
// further information contact opensource@soasis.org.
//
// Apache License Version 2 Usage
// Alternatively, this file may be used under the terms of Apache License
// Version 2.0 (the "License") for non-commercial use; you may not use this
// file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ============================================================================>

#pragma once

#ifndef ZTD_TEXT_ICONV_NAMES_HPP
#define ZTD_TEXT_ICONV_NAMES_HPP

#include <ztd/text/version.hpp>

#include <ztd/text/c_string_view.hpp>

#include <ztd/idk/endian.hpp>

#include <ztd/prologue.hpp>

namespace ztd { namespace text {
ZTD_TEXT_INLINE_ABI_NAMESPACE_OPEN_I_

inline constexpr c_string_view iconv_locale_name = "";
inline constexpr c_string_view iconv_wide_locale_name = "wchar_t";
inline constexpr c_string_view iconv_utf8_name = "UTF-8";
inline constexpr c_string_view iconv_utf16_name = (endian::native == endian::big ? "UTF-16BE" : "UTF-16LE");
inline constexpr c_string_view iconv_utf32_name = (endian::native == endian::big ? "UTF-32BE" : "UTF-32LE");

ZTD_TEXT_INLINE_ABI_NAMESPACE_CLOSE_I_
}} // namespace ztd::text

#include <ztd/epilogue.hpp>

#endif // ZTD_TEXT_ICONV_NAMES_HPP
3 changes: 2 additions & 1 deletion include/ztd/text/impl/execution_iconv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <ztd/text/version.hpp>

#include <ztd/text/basic_iconv.hpp>
#include <ztd/text/iconv_names.hpp>

#if ZTD_IS_ON(ZTD_LIBICONV_I_)

Expand All @@ -51,7 +52,7 @@ namespace ztd { namespace text {
using __base_t = basic_iconv<char, unicode_code_point>;

public:
__execution_iconv() noexcept : __base_t("", "UTF-32") {
__execution_iconv() noexcept : __base_t(iconv_locale_name.base(), iconv_utf32_name.base()) {
}
};

Expand Down
3 changes: 2 additions & 1 deletion include/ztd/text/impl/wide_execution_iconv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <ztd/text/version.hpp>

#include <ztd/text/basic_iconv.hpp>
#include <ztd/text/iconv_names.hpp>

#if ZTD_IS_ON(ZTD_LIBICONV_I_)

Expand All @@ -51,7 +52,7 @@ namespace ztd { namespace text {
using __base_t = basic_iconv<wchar_t, unicode_code_point>;

public:
__wide_execution_iconv() noexcept : __base_t("wchar_t", "UTF-32") {
__wide_execution_iconv() noexcept : __base_t(iconv_wide_locale_name.base(), iconv_utf32_name.base()) {
}
};

Expand Down
24 changes: 0 additions & 24 deletions tests/iconv/source/iconv.transcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,4 @@ TEST_CASE("text/transcode/iconv", "iconv transcode can roundtrip") {
check_roundtrip(encoding, ztd::tests::w_unicode_sequence_truth_native_endian);
}
}
SECTION("utf8") {
ztd::text::basic_iconv<ztd::uchar8_t> encoding("UTF-8");
check_roundtrip(encoding, ztd::tests::u8_basic_source_character_set);

if (ztd::text::contains_unicode_encoding(encoding)) {
check_roundtrip(encoding, ztd::tests::u8_unicode_sequence_truth_native_endian);
}
}
SECTION("utf16") {
ztd::text::basic_iconv<char16_t> encoding("UTF-16");
check_roundtrip(encoding, ztd::tests::u16_basic_source_character_set);

if (ztd::text::contains_unicode_encoding(encoding)) {
check_roundtrip(encoding, ztd::tests::u16_unicode_sequence_truth_native_endian);
}
}
SECTION("utf32") {
ztd::text::basic_iconv<char32_t> encoding("UTF-32");
check_roundtrip(encoding, ztd::tests::u32_basic_source_character_set);

if (ztd::text::contains_unicode_encoding(encoding)) {
check_roundtrip(encoding, ztd::tests::u32_unicode_sequence_truth_native_endian);
}
}
}
31 changes: 31 additions & 0 deletions tests/inclusion/source/ztd/text/iconv_names.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// =============================================================================
//
// ztd.text
// Copyright © 2021 JeanHeyd "ThePhD" Meneide and Shepherd's Oasis, LLC
// Contact: opensource@soasis.org
//
// Commercial License Usage
// Licensees holding valid commercial ztd.text licenses may use this file in
// accordance with the commercial license agreement provided with the
// Software or, alternatively, in accordance with the terms contained in
// a written agreement between you and Shepherd's Oasis, LLC.
// For licensing terms and conditions see your agreement. For
// further information contact opensource@soasis.org.
//
// Apache License Version 2 Usage
// Alternatively, this file may be used under the terms of Apache License
// Version 2.0 (the "License") for non-commercial use; you may not use this
// file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ============================================================================>

#include <ztd/text/iconv_names.hpp>

0 comments on commit 8cb532e

Please sign in to comment.