string_check.hpp 940 B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright Takatoshi Kondo 2015
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #if !defined(MQTT_STRING_CHECK_HPP)
  7. #define MQTT_STRING_CHECK_HPP
  8. #include <mqtt/namespace.hpp>
  9. #include <mqtt/utf8encoded_strings.hpp>
  10. #include <mqtt/exception.hpp>
  11. #include <mqtt/string_view.hpp>
  12. #include <mqtt/const_buffer_util.hpp>
  13. namespace as = boost::asio;
  14. namespace MQTT_NS {
  15. inline void utf8string_check(string_view str) {
  16. if (!utf8string::is_valid_length(str)) throw utf8string_length_error();
  17. auto r = utf8string::validate_contents(str);
  18. if (r != utf8string::validation::well_formed) {
  19. throw utf8string_contents_error(r);
  20. }
  21. }
  22. inline void utf8string_check(as::const_buffer str) {
  23. utf8string_check(string_view(get_pointer(str), get_size(str)));
  24. }
  25. } // namespace MQTT_NS
  26. #endif // MQTT_STRING_CHECK_HPP