Browse Source

move nfc stuff into namespace pEp.

master
roker 4 years ago
parent
commit
cf27e646b6
  1. 18
      src/nfc.cc
  2. 24
      src/nfc.hh
  3. 3
      src/string_view.hh

18
src/nfc.cc

@ -89,7 +89,7 @@ namespace
}; };
std::string escape(sv s) std::string escape(pEp::string_view s)
{ {
std::string ret; ret.reserve(s.size() + 16 ); std::string ret; ret.reserve(s.size() + 16 );
for(char c : s) for(char c : s)
@ -176,6 +176,8 @@ namespace
} // end of anonymous namespace } // end of anonymous namespace
namespace pEp {
std::ostream& operator<<(std::ostream& o, IsNFC is_nfc) std::ostream& operator<<(std::ostream& o, IsNFC is_nfc)
{ {
switch(is_nfc) switch(is_nfc)
@ -309,7 +311,7 @@ std::string toUtf8(const std::u32string& u32)
} }
illegal_utf8::illegal_utf8( sv s, unsigned position, const std::string& reason) illegal_utf8::illegal_utf8( string_view s, unsigned position, const std::string& reason)
: std::runtime_error( "Illegal UTF-8 string \"" + escape(s) + "\" at position " + std::to_string(position) + ": " + reason ) : std::runtime_error( "Illegal UTF-8 string \"" + escape(s) + "\" at position " + std::to_string(position) + ": " + reason )
{} {}
@ -319,7 +321,7 @@ illegal_utf8::illegal_utf8( const std::string& msg )
{} {}
void assert_utf8(sv s) void assert_utf8(string_view s)
{ {
const char* begin = s.data(); const char* begin = s.data();
const char* const end = s.data() + s.size(); const char* const end = s.data() + s.size();
@ -339,7 +341,7 @@ void assert_utf8(sv s)
// creates a NFD string from s // creates a NFD string from s
std::u32string fromUtf8_decompose(sv s) std::u32string fromUtf8_decompose(string_view s)
{ {
std::u32string u32s; std::u32string u32s;
u32s.reserve( static_cast<std::size_t>(s.size()*1.25) ); u32s.reserve( static_cast<std::size_t>(s.size()*1.25) );
@ -423,7 +425,7 @@ std::u32string createNFC(std::u32string nfd)
} }
IsNFC isNFC_quick_check(sv s) IsNFC isNFC_quick_check(string_view s)
{ {
const char* begin = s.data(); const char* begin = s.data();
const char* const end = s.data() + s.size(); const char* const end = s.data() + s.size();
@ -452,7 +454,7 @@ IsNFC isNFC_quick_check(sv s)
} }
bool isNFC(sv s) bool isNFC(string_view s)
{ {
switch( isNFC_quick_check(s) ) switch( isNFC_quick_check(s) )
{ {
@ -480,7 +482,7 @@ try{
} }
// s is ''moved'' to the return value if possible so no copy is done here. // s is ''moved'' to the return value if possible so no copy is done here.
std::string toNFC(sv s) std::string toNFC(string_view s)
{ {
if(isNFC_quick_check(s)==IsNFC::Yes) if(isNFC_quick_check(s)==IsNFC::Yes)
return std::string{s}; return std::string{s};
@ -503,3 +505,5 @@ std::map< std::pair<unsigned, unsigned>, unsigned> generate_nfc_compose()
return m; return m;
} }
} // end of namespace pEp

24
src/nfc.hh

@ -1,14 +1,16 @@
// This file is under GNU General Public License 3.0 // This file is under GNU General Public License 3.0
// see LICENSE.txt // see LICENSE.txt
#ifndef JSON_ADAPTER_NFC_HH #ifndef LIBPEPDATATYPES_NFC_HH
#define JSON_ADAPTER_NFC_HH #define LIBPEPDATATYPES_NFC_HH
#include "config.hh" // to switch between std::string_view or boost::string_view.hh #include "string_view.hh" // to switch between std::string_view or boost::string_view.hh
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
#include <iosfwd> #include <iosfwd>
namespace pEp {
enum class IsNFC enum class IsNFC
{ {
No=0, // contains a character that cannot occur in NFC No=0, // contains a character that cannot occur in NFC
@ -22,7 +24,7 @@ std::ostream& operator<<(std::ostream& o, IsNFC is_nfc);
class illegal_utf8 : public std::runtime_error class illegal_utf8 : public std::runtime_error
{ {
public: public:
illegal_utf8(sv, unsigned position, const std::string& reason); illegal_utf8(string_view, unsigned position, const std::string& reason);
protected: protected:
explicit illegal_utf8(const std::string& message); explicit illegal_utf8(const std::string& message);
}; };
@ -35,26 +37,28 @@ uint32_t parseUtf8(const char*& c, const char* end);
void toUtf8(const char32_t c, std::string& ret); void toUtf8(const char32_t c, std::string& ret);
// throws illegal_utf8 exception if s is not valid UTF-8 // throws illegal_utf8 exception if s is not valid UTF-8
void assert_utf8(sv s); void assert_utf8(string_view s);
// creates an NFD u32string from UTF-8 input string s // creates an NFD u32string from UTF-8 input string s
std::u32string fromUtf8_decompose(sv s); std::u32string fromUtf8_decompose(string_view s);
// convert NFD to NFC // convert NFD to NFC
std::u32string createNFC(std::u32string nfd_string); std::u32string createNFC(std::u32string nfd_string);
// return No or Maybe, if at least one character with NFC_Quickcheck class is "No" or "Maybe" // return No or Maybe, if at least one character with NFC_Quickcheck class is "No" or "Maybe"
// might throw illegal_utf8 exception // might throw illegal_utf8 exception
IsNFC isNFC_quick_check(sv s); IsNFC isNFC_quick_check(string_view s);
// runs first quick check and a deep test if quick check returns "Maybe". // runs first quick check and a deep test if quick check returns "Maybe".
bool isNFC(sv s); bool isNFC(string_view s);
// returns true if the sequence is valid UTF-8 // returns true if the sequence is valid UTF-8
bool isUtf8(const char* begin, const char* end); bool isUtf8(const char* begin, const char* end);
// converts a C++ string (in UTF-8) into NFC form // converts a C++ string (in UTF-8) into NFC form
// s is ''moved'' to the return value if possible so no copy is done here. // s is ''moved'' to the return value if possible so no copy is done here.
std::string toNFC(sv s); std::string toNFC(string_view s);
} // end of namespace pEp
#endif // JSON_ADAPTER_NFC_HH #endif // LIBPEPDATATYPES_NFC_HH

3
src/string_view.hh

@ -33,7 +33,8 @@ namespace pEp
return s += v; return s += v;
} }
using ::operator+=; // using ::std::operator+=;
// using ::boost::operator+=;
} // end of namespace pEp } // end of namespace pEp
#endif // C++17 switch #endif // C++17 switch

Loading…
Cancel
Save