Skip to content

Commit

Permalink
✨ decode_one, encode_one, and transcode_one functionality to make lif…
Browse files Browse the repository at this point in the history
…e easier
  • Loading branch information
ThePhD committed Jun 16, 2022
1 parent 88324df commit 3960fda
Show file tree
Hide file tree
Showing 19 changed files with 755 additions and 267 deletions.
91 changes: 48 additions & 43 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# file except in compliance with the License. You may obtain a copy of the
# License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# 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,
Expand All @@ -30,6 +30,7 @@

cmake_minimum_required(VERSION 3.16.0)
cmake_policy(VERSION 3.16)

# # Project kickstart
# Includes a bunch of basic flags and utilities shared across projects
# See more at the github repository link below
Expand All @@ -49,7 +50,7 @@ project(ztd.text
HOMEPAGE_URL "https://ztdtext.readthedocs.io/en/latest/"
LANGUAGES C CXX)

if (ZTD_TEXT_READTHEDOCS)
if(ZTD_TEXT_READTHEDOCS)
# ReadTheDocs seems unable to handle the include at the project level: something must be going wrong?
include(CheckCXXCompilerFlag)
include(CheckCCompilerFlag)
Expand All @@ -72,8 +73,8 @@ else()
endif()

# Modify bad flags / change defaults if we are the top level
if (ZTD_TEXT_IS_TOP_LEVEL_PROJECT)
if (CMAKE_SIZEOF_VOID_P EQUAL 4)
if(ZTD_TEXT_IS_TOP_LEVEL_PROJECT)
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/x86/${CMAKE_BUILD_TYPE}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/x86/${CMAKE_BUILD_TYPE}/bin")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/x86/${CMAKE_BUILD_TYPE}/bin")
Expand All @@ -83,26 +84,27 @@ if (ZTD_TEXT_IS_TOP_LEVEL_PROJECT)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/x64/${CMAKE_BUILD_TYPE}/bin")
endif()

if (NOT DEFINED CMAKE_CXX_STANDARD)
if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()

if (NOT DEFINED CMAKE_C_STANDARD)
if(NOT DEFINED CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 11)
endif()

if (ZTD_TEXT_BENCHMARKS OR ZTD_TEXT_EXAMPLES OR ZTD_TEXT_TESTS OR ZTD_TEXT_SCRATCH)
if(ZTD_TEXT_BENCHMARKS OR ZTD_TEXT_EXAMPLES OR ZTD_TEXT_TESTS OR ZTD_TEXT_SCRATCH)
# normal flags
check_compiler_flag(disable-permissive MSVC /permissive- GCC -pedantic)

# Warning flags
check_compiler_flag(warn-pedantic MSVC /permissive- GCC -pedantic)
check_compiler_flag(warn-all MSVC /W4 GCC -Wall)
check_compiler_flag(warn-errors MSVC /WX GCC -Werror)
check_compiler_flag(warn-extra GCC -Wextra CLANG -Wextra)
check_compiler_flag(warn-extra GCC -Wextra Clang -Wextra)
check_compiler_flag(utf8-literal-encoding MSVC /execution-charset:utf-8 GCC -fexec-charset=utf-8)
check_compiler_flag(utf8-source-encoding MSVC /source-charset:utf-8 GCC -finput-charset=utf-8)
check_compiler_flag(extra-constexpr-depth MSVC /constexpr:depth2147483647 GCC -fconstexpr-depth=2147483647 CLANG -fconstexpr-depth=2147483647)
check_compiler_flag(extra-constexpr-steps MSVC /constexpr:steps2147483647 GCC -fconstexpr-ops-limit=2147483647 CLANG -fconstexpr-steps=2147483647)
check_compiler_flag(extra-constexpr-depth MSVC /constexpr:depth2147483647 GCC -fconstexpr-depth=2147483647 Clang -fconstexpr-depth=2147483647)
check_compiler_flag(extra-constexpr-steps MSVC /constexpr:steps2147483647 GCC -fconstexpr-ops-limit=2147483647 Clang -fconstexpr-steps=2147483647)
check_compiler_flag(template-debugging-mode GCC -ftemplate-backtrace-limit=0)
endif()
endif()
Expand Down Expand Up @@ -134,7 +136,7 @@ FetchContent_Declare(ztd.fixed_container
FetchContent_MakeAvailable(ztd.fixed_container)

# ztd.cuneicode
if (ZTD_TEXT_USE_CUNEICODE)
if(ZTD_TEXT_USE_CUNEICODE)
FetchContent_Declare(ztd.cuneicode
GIT_REPOSITORY https://github.com/soasis/cuneicode.git
GIT_TAG main)
Expand All @@ -143,29 +145,30 @@ if (ZTD_TEXT_USE_CUNEICODE)
endif()

# iconv - static and dynamic
if (NOT TARGET Iconv::Iconv AND ZTD_TEXT_USE_LIBICONV)
if(NOT TARGET Iconv::Iconv AND ZTD_TEXT_USE_LIBICONV)
# because of iconv's license, using it as a static dependency
# is tricky: therefore, it's only enabled for folks who explicitly asked for it,
# regardless of whether it is available or not
find_package(Iconv)
endif()

# define generator expressions for each moment
string(CONCAT ztd-text-libiconv-define
$<IF:$<BOOL:${ZTD_TEXT_USE_LIBICONV}>,
ZTD_LIBICONV=1,
ZTD_LIBICONV=0
ZTD_LIBICONV=1,
ZTD_LIBICONV=0
>
)
string(CONCAT ztd-text-libiconv-load-define
$<IF:$<AND:$<BOOL:${Iconv_FOUND}>,$<BOOL:${ZTD_TEXT_USE_LIBICONV}>>,
ZTD_LIBICONV_LOAD=0,
ZTD_LIBICONV_LOAD=1
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}>>,
ZTD_ICONV_H=1,
ZTD_ICONV_H=0
ZTD_ICONV_H=1,
ZTD_ICONV_H=0
>
)
string(CONCAT ztd-text-libiconv
Expand All @@ -176,26 +179,26 @@ string(CONCAT ztd-text-libiconv-dl
)
string(CONCAT ztd-text-static-libiconv-define
$<IF:
$<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
>
>,
ZTD_LIBICONV_STATIC=1,
ZTD_LIBICONV_STATIC=0
$<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
>
>,
ZTD_LIBICONV_STATIC=1,
ZTD_LIBICONV_STATIC=0
>
)
string(CONCAT ztd-text-dynamic-libiconv-define
$<IF:
$<AND:$<BOOL:Iconv_FOUND>, $<BOOL:ZTD_TEXT_USE_LIBICONV>,
$<STREQUAL:
$<$<BOOL:$<TARGET_NAME_IF_EXISTS:Iconv::Iconv>>:$<TARGET_PROPERTY:Iconv::Iconv,TYPE>>,
SHARED_LIBRARY
>
>,
ZTD_LIBICONV_DYNAMIC=1,
ZTD_LIBICONV_DYNAMIC=0
$<AND:$<BOOL:Iconv_FOUND>, $<BOOL:ZTD_TEXT_USE_LIBICONV>,
$<STREQUAL:
$<$<BOOL:$<TARGET_NAME_IF_EXISTS:Iconv::Iconv>>:$<TARGET_PROPERTY:Iconv::Iconv,TYPE>>,
SHARED_LIBRARY
>
>,
ZTD_LIBICONV_DYNAMIC=1,
ZTD_LIBICONV_DYNAMIC=0
>
)

Expand All @@ -206,8 +209,8 @@ add_library(ztd.text INTERFACE)
add_library(ztd::text ALIAS ztd.text)
target_include_directories(ztd.text
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_sources(ztd.text INTERFACE ${ztd.text.includes})
target_compile_definitions(ztd.text
INTERFACE
Expand Down Expand Up @@ -242,30 +245,32 @@ export(TARGETS ztd.text
FILE
"${CMAKE_CURRENT_BINARY_DIR}/cmake/ztd.text/ztd.text-targets.cmake")


if (ZTD_TEXT_GENERATE_SINGLE)
if(ZTD_TEXT_GENERATE_SINGLE)
add_subdirectory(single)
endif()

# # Benchmarks, Tests, Examples and Documentation
if (ZTD_TEXT_TESTS)
if(ZTD_TEXT_TESTS)
include(CTest)
add_subdirectory(tests)
endif()

if (ZTD_TEXT_DOCUMENTATION)
if(ZTD_TEXT_DOCUMENTATION)
add_subdirectory(documentation)
endif()
if (ZTD_TEXT_BENCHMARKS)

if(ZTD_TEXT_BENCHMARKS)
add_subdirectory(benchmarks)
endif()
if (ZTD_TEXT_EXAMPLES)

if(ZTD_TEXT_EXAMPLES)
add_subdirectory(examples)
endif()

# For quick debugging and development only: don't peek! 🙈
mark_as_advanced(ZTD_TEXT_SCRATCH)
if (ZTD_TEXT_SCRATCH)

if(ZTD_TEXT_SCRATCH)
add_executable(scratch main.cpp)
target_link_libraries(scratch PRIVATE ztd::text)
target_include_directories(scratch PRIVATE tests/shared/include)
Expand Down
81 changes: 60 additions & 21 deletions include/ztd/ranges/counted_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,46 +165,85 @@ namespace ztd { namespace ranges {
}

constexpr __counted_iterator operator--(int) noexcept(_S_operator_minusminus_noexcept()) {
auto __copy = (*this);
--(*this);
return *this;
return __copy;
}

template <typename _ItTy = _It,
::std::enable_if_t<is_iterator_random_access_iterator_v<_ItTy>>* = nullptr>
constexpr __counted_iterator operator+(difference_type __diff) const {
return __counted_iterator(this->_M_it + __diff, this->_M_count + __diff);
}

#if 0
constexpr __counted_iterator operator+(_ItDiff n) const requires random_­access_­iterator<_It>;
template <typename _ItTy = _It,
::std::enable_if_t<is_iterator_random_access_iterator_v<_ItTy>>* = nullptr>
friend constexpr __counted_iterator operator+(
_ItDiff n, const __counted_iterator& x) requires random_­access_­iterator<_It>;
constexpr __counted_iterator& operator+=(_ItDiff n) requires random_­access_­iterator<_It>;
difference_type __diff, const __counted_iterator& __right) {
return __counted_iterator(__right._M_it + __diff, __right._M_count + __diff);
}

template <typename _ItTy = _It,
::std::enable_if_t<is_iterator_random_access_iterator_v<_ItTy>>* = nullptr>
constexpr __counted_iterator& operator+=(difference_type __diff) {
this->_M_it += __diff;
this->_M_count += __diff;
return *this;
}

constexpr __counted_iterator operator-(_ItDiff n) const requires random_­access_­iterator<_It>;
template <typename _ItTy = _It,
::std::enable_if_t<is_iterator_random_access_iterator_v<_ItTy>>* = nullptr>
constexpr __counted_iterator operator-(difference_type __diff) const {
return __counted_iterator(this->_M_it - __diff, this->_M_count - __diff);
}

template <typename _ItRight>
friend constexpr iter_difference_t<_ItRight> operator-(const __counted_iterator& x, const __counted_iterator<_ItRight>& y);
constexpr __counted_iterator& operator-=(_ItDiff n) requires random_­access_­iterator<_It>;
template <typename _RightIt,
::std::enable_if_t< // cf-hack
is_iterator_random_access_iterator_v<_RightIt> // cf-hack
&& is_iterator_random_access_iterator_v<_It> // cf-hack
>* = nullptr>
friend constexpr iterator_difference_type_t<_RightIt> operator-(
const __counted_iterator& __left, const __counted_iterator<_RightIt>& __right) {
return __left._M_it - __right._M_it;
}

constexpr decltype(auto) operator[](_ItDiff n) const requires random_­access_­iterator<_It>;
#endif
template <typename _RightIt, typename _ItTy = _It,
::std::enable_if_t<is_iterator_random_access_iterator_v<_ItTy>>* = nullptr>
constexpr __counted_iterator& operator-=(difference_type __diff) {
this->_M_it -= __diff;
this->_M_count -= __diff;
return *this;
}

template <typename _ItTy = _It,
::std::enable_if_t<is_iterator_random_access_iterator_v<_ItTy>>* = nullptr>
constexpr decltype(auto) operator[](difference_type __index) const {
return this->_M_it[__index];
}

friend constexpr _ItDiff operator-(const __counted_iterator& __left, default_sentinel_t) noexcept {
friend constexpr difference_type operator-(
const __counted_iterator& __left, default_sentinel_t) noexcept {
return __left._M_count;
}

friend constexpr _ItDiff operator-(default_sentinel_t, const __counted_iterator& __right) noexcept {
friend constexpr difference_type operator-(
default_sentinel_t, const __counted_iterator& __right) noexcept {
return -__right._M_count;
}

template <typename _ItRight>
template <typename _RightIt>
friend constexpr bool operator==(
const __counted_iterator& __left, const __counted_iterator<_ItRight>& __right) noexcept {
const __counted_iterator& __left, const __counted_iterator<_RightIt>& __right) noexcept {
return __left._M_it == __right._M_it && __left._M_count == __right._M_count;
}

friend constexpr bool operator==(const __counted_iterator& __left, default_sentinel_t) noexcept {
return __left._M_count == static_cast<_ItDiff>(0);
}

template <typename _ItRight>
template <typename _RightIt>
friend constexpr bool operator!=(
const __counted_iterator& __left, const __counted_iterator<_ItRight>& __right) noexcept {
const __counted_iterator& __left, const __counted_iterator<_RightIt>& __right) noexcept {
return __left._M_it != __right._M_it || __left._M_count != __right._M_count;
}

Expand All @@ -217,16 +256,16 @@ namespace ztd { namespace ranges {
return ranges_adl::adl_iter_move(__it._M_it);
}

template <typename _ItRight>
template <typename _RightIt>
friend constexpr void
iter_swap(const __counted_iterator& x, const __counted_iterator<_ItRight>& y) noexcept(
iter_swap(const __counted_iterator& x, const __counted_iterator<_RightIt>& y) noexcept(
noexcept(ranges_adl::adl_iter_swap(x._M_it, y._M_it))) {
ranges_adl::adl_iter_swap(x._M_it, y._M_it);
}

private:
_It _M_it = _It();
_ItDiff _M_count = 0;
_It _M_it = _It();
difference_type _M_count = difference_type {};
};
} // namespace __rng_detail

Expand Down
4 changes: 4 additions & 0 deletions include/ztd/ranges/iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ namespace ztd { namespace ranges {
template <typename _Tag, typename _It>
inline constexpr bool is_iterator_concept_or_better_v = ::std::is_base_of_v<_Tag, iterator_concept_t<_It>>;

template <typename _It>
inline constexpr bool is_iterator_random_access_iterator_v
= is_iterator_concept_or_better_v<::std::random_access_iterator_tag, _It>;

template <typename _It>
inline constexpr bool is_iterator_contiguous_iterator_v = ::ztd::__idk_detail::__mark_contiguous_v<_It>
|| (
Expand Down
4 changes: 4 additions & 0 deletions include/ztd/ranges/range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ namespace ztd { namespace ranges {
template <typename _Range>
inline constexpr bool is_range_contiguous_range_v = is_iterator_contiguous_iterator_v<range_iterator_t<_Range>>;

template <typename _Range>
inline constexpr bool is_range_random_access_range_v
= is_range_iterator_concept_or_better_v<::std::random_access_iterator_tag, range_iterator_t<_Range>>;

template <typename _Range>
inline constexpr bool is_sized_range_v
= is_sized_sentinel_for_v<range_iterator_t<_Range>, range_sentinel_t<_Range>>;
Expand Down

0 comments on commit 3960fda

Please sign in to comment.