@brief An exception class indicating an encoding error. @sa https://fktn-k.github.io/fkYAML/api/exception/invalid_encoding/
| 1672 | /// @brief An exception class indicating an encoding error. |
| 1673 | /// @sa https://fktn-k.github.io/fkYAML/api/exception/invalid_encoding/ |
| 1674 | class invalid_encoding : public exception { |
| 1675 | public: |
| 1676 | /// @brief Construct a new invalid_encoding object for UTF-8 related errors. |
| 1677 | /// @param msg An error message. |
| 1678 | /// @param u8 The UTF-8 character bytes. |
| 1679 | explicit invalid_encoding(const char* msg, const std::initializer_list<uint8_t>& u8) noexcept |
| 1680 | : exception(generate_error_message(msg, u8).c_str()) { |
| 1681 | } |
| 1682 | |
| 1683 | /// @brief Construct a new invalid_encoding object for UTF-16 related errors. |
| 1684 | /// @param msg An error message. |
| 1685 | /// @param u16_h The first UTF-16 encoded element used for the UTF-8 encoding. |
| 1686 | /// @param u16_l The second UTF-16 encoded element used for the UTF-8 encoding. |
| 1687 | explicit invalid_encoding(const char* msg, std::array<char16_t, 2> u16) noexcept |
| 1688 | : exception(generate_error_message(msg, u16).c_str()) { |
| 1689 | } |
| 1690 | |
| 1691 | /// @brief Construct a new invalid_encoding object for UTF-32 related errors. |
| 1692 | /// @param msg An error message. |
| 1693 | /// @param u32 The UTF-32 encoded element used for the UTF-8 encoding. |
| 1694 | explicit invalid_encoding(const char* msg, char32_t u32) noexcept |
| 1695 | : exception(generate_error_message(msg, u32).c_str()) { |
| 1696 | } |
| 1697 | |
| 1698 | private: |
| 1699 | static std::string generate_error_message(const char* msg, const std::initializer_list<uint8_t>& u8) noexcept { |
| 1700 | const auto* itr = u8.begin(); |
| 1701 | const auto* end_itr = u8.end(); |
| 1702 | std::string formatted = detail::format("invalid_encoding: %s in=[ 0x%02x", msg, *itr++); |
| 1703 | while (itr != end_itr) { |
| 1704 | formatted += detail::format(", 0x%02x", *itr++); |
| 1705 | } |
| 1706 | formatted += " ]"; |
| 1707 | return formatted; |
| 1708 | } |
| 1709 | |
| 1710 | /// @brief Generate an error message from the given parameters for the UTF-16 encoding. |
| 1711 | /// @param msg An error message. |
| 1712 | /// @param h The first UTF-16 encoded element used for the UTF-8 encoding. |
| 1713 | /// @param l The second UTF-16 encoded element used for the UTF-8 encoding. |
| 1714 | /// @return A generated error message. |
| 1715 | static std::string generate_error_message(const char* msg, std::array<char16_t, 2> u16) noexcept { |
| 1716 | // uint16_t is large enough for UTF-16 encoded elements. |
| 1717 | return detail::format( |
| 1718 | "invalid_encoding: %s in=[ 0x%04x, 0x%04x ]", |
| 1719 | msg, |
| 1720 | static_cast<uint16_t>(u16[0]), |
| 1721 | static_cast<uint16_t>(u16[1])); |
| 1722 | } |
| 1723 | |
| 1724 | /// @brief Generate an error message from the given parameters for the UTF-32 encoding. |
| 1725 | /// @param msg An error message. |
| 1726 | /// @param u32 The UTF-32 encoded element used for the UTF-8 encoding. |
| 1727 | /// @return A genereated error message. |
| 1728 | static std::string generate_error_message(const char* msg, char32_t u32) noexcept { |
| 1729 | // uint32_t is large enough for UTF-32 encoded elements. |
| 1730 | return detail::format("invalid_encoding: %s in=0x%08x", msg, static_cast<uint32_t>(u32)); |
| 1731 | } |
no outgoing calls
no test coverage detected