2 Commits

2 changed files with 66 additions and 1 deletions
Split View
  1. +5
    -1
      src/nfc.hh
  2. +61
    -0
      test/unittest_nfcstring.cc

+ 5
- 1
src/nfc.hh View File

@ -98,7 +98,6 @@ public:
static const size_t npos = String::npos;
explicit nfc_string(StringView src);
explicit nfc_string(String && src);
@ -111,6 +110,7 @@ public:
: nfc_string{ StringView{src, length} }
{}
nfc_string() = default;
nfc_string(const nfc_string& src) = default;
nfc_string( nfc_string&& src) = default;
@ -119,7 +119,11 @@ public:
nfc_string& assign(StringView src);
nfc_string& assign(String && src);
nfc_string& assign(const CharT* src) { return this->assign(StringView{src}); }
nfc_string& operator=(StringView src) { return this->assign(src); }
nfc_string& operator=(String && src) { return this->assign(std::move(src)); }
nfc_string& operator=(const CharT* src) { return this->assign(StringView{src}); }
/// read-only: shares representation
operator const String&() const noexcept { return s; }


+ 61
- 0
test/unittest_nfcstring.cc View File

@ -0,0 +1,61 @@
#include <gtest/gtest.h>
#include "../src/nfc.hh" // for illegal_utf8 exception
#include <vector>
using namespace pEp;
using std::string_view;
namespace {
struct TestEntry
{
string_view input;
string_view nfc;
};
typedef TestEntry TE;
std::ostream& operator<<(std::ostream& o, const TestEntry& tt)
{
return o << "input=«" << tt.input << "», nfc=«" << tt.nfc << "» ";
}
} // end of anonymous namespace
TEST( NfcTestString, Simple )
{
pEp::nfc_string s;
EXPECT_TRUE( s.empty() );
EXPECT_EQ( s.size(), 0 );
s.reserve(1000);
EXPECT_GE(s.capacity(), 1000);
s = "Ha\u0308user"; // non-NFC input will be normalized.
EXPECT_NE( s.get(), std::string("Ha\u0308user") );
EXPECT_EQ( s.get(), std::string("Häuser") );
s.insert(0, "U\u0308ber");
EXPECT_EQ( s.get(), std::string("ÜberHäuser") );
s += "\u0328\u030c"; // COMBINING OGONEK (below), COMBINING CARON (above)
// r absorbs the caron into U+0159, the combining ogonek remains separate
EXPECT_EQ( s.get(), std::string("ÜberHäuse\u0159\u0328") );
EXPECT_EQ( s.size(), 15);
EXPECT_THROW( s.substr(1), pEp::illegal_utf );
EXPECT_THROW( s.substr(2, 10), pEp::illegal_utf );
// 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" );
}
TEST( NfcTestString, Exceptions )
{
pEp::nfc_string s;
EXPECT_THROW( s = "Meep\xc0\x80.", pEp::illegal_utf );
}

Loading…
Cancel
Save