parse_error.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * Copyright (C) 2015 Topology LP
  3. * All rights reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to
  7. * deal in the Software without restriction, including without limitation the
  8. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. * sell copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. */
  23. #ifndef CPPCODEC_PARSE_ERROR
  24. #define CPPCODEC_PARSE_ERROR
  25. #include <stdexcept>
  26. #include <string>
  27. namespace cppcodec {
  28. namespace detail {
  29. // <*stream> headers include a lot of code and noticeably increase compile times.
  30. // The only thing we want from them really is a char-to-string conversion.
  31. // That's easy to implement with many less lines of code, so let's do it ourselves.
  32. template <int N>
  33. static void uctoa(unsigned char n, char (&s)[N])
  34. {
  35. static_assert(N >= 4, "need at least 4 bytes to convert an unsigned char to string safely");
  36. int i = sizeof(s) - 1;
  37. int num_chars = 1;
  38. s[i--] = '\0';
  39. do { // generate digits in reverse order
  40. s[i--] = n % 10 + '0'; // get next digit
  41. ++num_chars;
  42. } while ((n /= 10) > 0); // delete it
  43. if (num_chars == sizeof(s)) {
  44. return;
  45. }
  46. for (i = 0; i < num_chars; ++i) { // move chars to front of string
  47. s[i] = s[i + (sizeof(s) - num_chars)];
  48. }
  49. }
  50. } // end namespace detail
  51. class parse_error : public std::domain_error
  52. {
  53. public:
  54. using std::domain_error::domain_error;
  55. };
  56. // Avoids memory allocation, so it can be used in constexpr functions.
  57. class symbol_error : public parse_error
  58. {
  59. public:
  60. symbol_error(char c)
  61. : parse_error(symbol_error::make_error_message(c))
  62. , m_symbol(c)
  63. {
  64. }
  65. symbol_error(const symbol_error&) = default;
  66. char symbol() const noexcept { return m_symbol; }
  67. private:
  68. static std::string make_error_message(char c)
  69. {
  70. char s[4];
  71. detail::uctoa(*reinterpret_cast<unsigned char*>(&c), s);
  72. return std::string("parse error: character [") + &(s[0]) + " '" + c + "'] out of bounds";
  73. }
  74. private:
  75. char m_symbol;
  76. };
  77. class invalid_input_length : public parse_error
  78. {
  79. public:
  80. using parse_error::parse_error;
  81. };
  82. class padding_error : public invalid_input_length
  83. {
  84. public:
  85. padding_error()
  86. : invalid_input_length("parse error: codec expects padded input string but padding was invalid")
  87. {
  88. }
  89. padding_error(const padding_error&) = default;
  90. };
  91. } // namespace cppcodec
  92. #endif // CPPCODEC_PARSE_ERROR