A C++ wrapper for the basic C datatypes defined by the pEpEngine.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.8 KiB

  1. // This file is under GNU General Public License 3.0
  2. // see LICENSE.txt
  3. #ifndef JSON_ADAPTER_NFC_HH
  4. #define JSON_ADAPTER_NFC_HH
  5. #include "config.hh" // to switch between std::string_view or boost::string_view.hh
  6. #include <string>
  7. #include <stdexcept>
  8. #include <iosfwd>
  9. enum class IsNFC
  10. {
  11. No=0, // contains a character that cannot occur in NFC
  12. Maybe=1, // contains a character that is only allowed in certain positions in NFC
  13. Yes=2 // contains no invalid or partially valid character
  14. };
  15. std::ostream& operator<<(std::ostream& o, IsNFC is_nfc);
  16. class illegal_utf8 : public std::runtime_error
  17. {
  18. public:
  19. illegal_utf8(sv, unsigned position, const std::string& reason);
  20. protected:
  21. explicit illegal_utf8(const std::string& message);
  22. };
  23. // scans the char sequences and parses UTF-8 sequences. Detect UTF-8 errors and throws exceptions.
  24. uint32_t parseUtf8(const char*& c, const char* end);
  25. // converts 'c' into a UTF-8 sequence and adds it to 'ret'
  26. void toUtf8(const char32_t c, std::string& ret);
  27. // throws illegal_utf8 exception if s is not valid UTF-8
  28. void assert_utf8(sv s);
  29. // creates an NFD u32string from UTF-8 input string s
  30. std::u32string fromUtf8_decompose(sv s);
  31. // convert NFD to NFC
  32. std::u32string createNFC(std::u32string nfd_string);
  33. // return No or Maybe, if at least one character with NFC_Quickcheck class is "No" or "Maybe"
  34. // might throw illegal_utf8 exception
  35. IsNFC isNFC_quick_check(sv s);
  36. // runs first quick check and a deep test if quick check returns "Maybe".
  37. bool isNFC(sv s);
  38. // returns true if the sequence is valid UTF-8
  39. bool isUtf8(const char* begin, const char* end);
  40. // converts a C++ string (in UTF-8) into NFC form
  41. // s is ''moved'' to the return value if possible so no copy is done here.
  42. std::string toNFC(sv s);
  43. #endif // JSON_ADAPTER_NFC_HH