From b8df0024af1ed271b0a5c280b97148bc9f8c6eef Mon Sep 17 00:00:00 2001 From: roker Date: Fri, 8 Oct 2021 18:32:15 +0200 Subject: [PATCH] test the remaining nfc_string member functions. --- src/nfc.cc | 17 ++++++++++++----- src/nfc.hh | 3 ++- test/unittest_nfcstring.cc | 13 +++++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/nfc.cc b/src/nfc.cc index 6c0821f..ae741a8 100644 --- a/src/nfc.cc +++ b/src/nfc.cc @@ -565,11 +565,18 @@ bool UTF::is_safe_NFC_start(std::basic_string_view s) const CharT* begin = s.data(); const CharT* const end = s.data() + s.size(); - const uint32_t u = parse(begin, end); - if(NFC_No.count(u)) return false; - if(NFC_Maybe.count(u)) return false; - - return true; + try + { + const uint32_t u = parse(begin, end); + if(NFC_No.count(u)) return false; + if(NFC_Maybe.count(u)) return false; + + return true; + } + catch(const utf_exception& ue) + { + throw illegal_utf(s, begin-s.data(), ue.reason()); + } } diff --git a/src/nfc.hh b/src/nfc.hh index 40ec38e..c2c0d94 100644 --- a/src/nfc.hh +++ b/src/nfc.hh @@ -96,7 +96,8 @@ public: /// only forward iterator. Does a backward_iterator make sense in UTF-encoded strings? typedef typename String::const_iterator const_iterator; - static const size_t npos = String::npos; + static + constexpr size_t npos = String::npos; explicit nfc_string(StringView src); explicit nfc_string(String && src); diff --git a/test/unittest_nfcstring.cc b/test/unittest_nfcstring.cc index 9382c95..eb5b4f9 100644 --- a/test/unittest_nfcstring.cc +++ b/test/unittest_nfcstring.cc @@ -51,6 +51,17 @@ TEST( NfcTestString, Simple ) // removal of the r-with-caron let the remaining ogonek combine with the e to U+0119 (E WITH OGONEK) EXPECT_EQ( s.erase(11, 2).get(), "ÜberHäus\u0119" ); + + EXPECT_TRUE ( s.starts_with("Üb") ); + EXPECT_FALSE( s.starts_with("Üx") ); + EXPECT_TRUE ( s.ends_with("s\u0119") ); + EXPECT_FALSE( s.ends_with("ss\u0119") ); + + EXPECT_EQ( s.find("Über"), 0u ); + EXPECT_EQ( s.find("ber") , 2u ); + EXPECT_EQ( s.find("über"), UTF8::nfc_string::npos ); + EXPECT_EQ( s.find("Übel"), UTF8::nfc_string::npos ); + } @@ -58,4 +69,6 @@ TEST( NfcTestString, Exceptions ) { pEp::nfc_string s; EXPECT_THROW( s = "Meep\xc0\x80.", pEp::illegal_utf ); + EXPECT_THROW( s += '\377', pEp::illegal_utf ); + }