Skip to content

Commit

Permalink
last inserted \0 (null characters) shouldn't be ignored (fix #328)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanickadot committed Sep 21, 2024
1 parent 068c626 commit 69c6f62
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
5 changes: 0 additions & 5 deletions include/ctll/fixed_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ CTLL_EXPORT template <size_t N> struct fixed_string {
#ifdef CTRE_STRING_IS_UTF8
size_t out{0};
for (size_t i{0}; i < N; ++i) {
if ((i == (N-1)) && (input[i] == 0)) break;
length_value_t info = length_and_value_of_utf8_code_point(input[i]);
switch (info.length) {
case 6:
Expand Down Expand Up @@ -85,15 +84,13 @@ CTLL_EXPORT template <size_t N> struct fixed_string {
#else
for (size_t i{0}; i < N; ++i) {
content[i] = static_cast<uint8_t>(input[i]);
if ((i == (N-1)) && (input[i] == 0)) break;
real_size++;
}
#endif
#if __cpp_char8_t
} else if constexpr (std::is_same_v<T, char8_t>) {
size_t out{0};
for (size_t i{0}; i < N; ++i) {
if ((i == (N-1)) && (input[i] == 0)) break;
length_value_t info = length_and_value_of_utf8_code_point(input[i]);
switch (info.length) {
case 6:
Expand Down Expand Up @@ -135,15 +132,13 @@ CTLL_EXPORT template <size_t N> struct fixed_string {
}
}
} else {
if ((i == (N-1)) && (input[i] == 0)) break;
content[out++] = info.value;
}
}
real_size = out;
} else if constexpr (std::is_same_v<T, wchar_t> || std::is_same_v<T, char32_t>) {
for (size_t i{0}; i < N; ++i) {
content[i] = static_cast<char32_t>(input[i]);
if ((i == (N-1)) && (input[i] == 0)) break;
real_size++;
}
}
Expand Down
25 changes: 25 additions & 0 deletions tests/string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <ctll/fixed_string.hpp>

template <ctll::fixed_string Str> struct helper {
consteval bool correct() const noexcept {
return Str.correct();
}
consteval size_t size() const noexcept {
return Str.size();
}
};

constexpr auto a = helper<"\0\0">{};

static_assert(a.correct());
static_assert(a.size() == 2u);

constexpr auto b = helper<"\0">{};

static_assert(b.correct());
static_assert(b.size() == 1u);

constexpr auto c = helper<"">{};

static_assert(c.correct());
static_assert(c.size() == 0u);

0 comments on commit 69c6f62

Please sign in to comment.