Compare commits

...

5 commits

Author SHA1 Message Date
Nemanja Trifunovic
65701fe007 Compile time checks for size of UTF-16 code units 2025-01-11 11:09:19 -05:00
root
b26a5f718f Release 4.0.6 2024-11-03 13:03:52 -05:00
root
10b7895211 Release 4.0.5 2024-11-03 13:02:26 -05:00
root
77870cc80b Fix Wsign-conversion warnings 2024-11-02 20:38:03 -04:00
root
cd191b6c45 Tests: use add_compile_options() to add warnings to tests 2024-11-02 19:42:46 -04:00
4 changed files with 26 additions and 14 deletions

View file

@ -3,7 +3,7 @@
cmake_minimum_required (VERSION 3.14...3.27)
project (utf8cpp
VERSION 4.0.4
VERSION 4.0.6
LANGUAGES CXX
DESCRIPTION "C++ portable library for working with utf-8 encoding")

View file

@ -43,9 +43,14 @@ DEALINGS IN THE SOFTWARE.
#if UTF_CPP_CPLUSPLUS >= 201103L // C++ 11 or later
#define UTF_CPP_OVERRIDE override
#define UTF_CPP_NOEXCEPT noexcept
#define UTF_CPP_STATIC_ASSERT(condition) static_assert(condition, "UTFCPP static assert");
#else // C++ 98/03
#define UTF_CPP_OVERRIDE
#define UTF_CPP_NOEXCEPT throw()
// Simulate static_assert:
template <bool Condition> struct StaticAssert {static void assert() {int static_assert_impl[(Condition ? 1 : -1)];} };
template <> struct StaticAssert<true> {static void assert() {}};
#define UTF_CPP_STATIC_ASSERT(condition) StaticAssert<condition>::assert();
#endif // C++ 11 or later
@ -215,7 +220,7 @@ namespace internal
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
code_point += (*it) & 0x3f;
code_point = static_cast<utfchar32_t>(code_point + ((*it) & 0x3f));
return UTF8_OK;
}
@ -234,11 +239,11 @@ namespace internal
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
code_point += (utf8::internal::mask8(*it) << 6) & 0xfff;
code_point = static_cast<utfchar32_t>(code_point + ((utf8::internal::mask8(*it) << 6) & 0xfff));
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
code_point += (*it) & 0x3f;
code_point = static_cast<utfchar32_t>(code_point + ((*it) & 0x3f));
return UTF8_OK;
}
@ -308,6 +313,10 @@ namespace internal
template <typename word_iterator>
utf_error validate_next16(word_iterator& it, word_iterator end, utfchar32_t& code_point)
{
// Make sure the iterator dereferences a large enough type
typedef typename std::iterator_traits<word_iterator>::value_type word_type;
UTF_CPP_STATIC_ASSERT(sizeof(word_type) >= sizeof(utfchar16_t));
// Check the edge case:
if (it == end)
return NOT_ENOUGH_ROOM;
// Save the original value of it so we can go back in case of failure
@ -327,7 +336,7 @@ namespace internal
else if (is_lead_surrogate(first_word)) {
const utfchar16_t second_word = *it++;
if (is_trail_surrogate(second_word)) {
code_point = (first_word << 10) + second_word + SURROGATE_OFFSET;
code_point = static_cast<utfchar32_t>(first_word << 10) + second_word + SURROGATE_OFFSET;
return UTF8_OK;
} else
err = INCOMPLETE_SEQUENCE;
@ -395,6 +404,7 @@ namespace internal
// the word_type.
template <typename word_iterator, typename word_type>
word_iterator append16(utfchar32_t cp, word_iterator result) {
UTF_CPP_STATIC_ASSERT(sizeof(word_type) >= sizeof(utfchar16_t));
if (is_in_bmp(cp))
*(result++) = static_cast<word_type>(cp);
else {

View file

@ -115,15 +115,15 @@ namespace utf8
++it;
cp = ((cp << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff);
++it;
cp += (*it) & 0x3f;
cp = static_cast<utfchar32_t>(cp + ((*it) & 0x3f));
break;
case 4:
++it;
cp = ((cp << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
++it;
cp += (utf8::internal::mask8(*it) << 6) & 0xfff;
cp = static_cast<utfchar32_t>(cp + ((utf8::internal::mask8(*it) << 6) & 0xfff));
++it;
cp += (*it) & 0x3f;
cp = static_cast<utfchar32_t>(cp + ((*it) & 0x3f));
break;
}
++it;

View file

@ -6,6 +6,14 @@ add_library(${PROJECT_NAME} INTERFACE)
include_directories("${PROJECT_SOURCE_DIR}/../source")
if (MSVC)
# warning level 4
add_compile_options(/W4)
else()
# additional warnings
add_compile_options(-Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion)
endif()
add_executable(negative negative.cpp)
add_executable(cpp11 test_cpp11.cpp)
add_executable(cpp17 test_cpp17.cpp)
@ -14,12 +22,6 @@ add_executable(apitests apitests.cpp)
add_executable(noexceptionstests noexceptionstests.cpp)
target_compile_options(${PROJECT_NAME} INTERFACE
$<$<CXX_COMPILER_ID:MSVC>:/W4>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Wextra -Wpedantic -Wconversion>)
target_compile_options(noexceptionstests PUBLIC -fno-exceptions)
set_target_properties(negative apitests noexceptionstests