Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
Punycode

An implementation of RFC 3492 Punycode. More...

Classes

class  sourcemeta::core::PunycodeError

Functions

SOURCEMETA_CORE_PUNYCODE_EXPORT auto sourcemeta::core::utf32_to_punycode (std::u32string_view input, std::size_t maximum_code_points=PUNYCODE_MAXIMUM_CODE_POINTS) -> std::string
SOURCEMETA_CORE_PUNYCODE_EXPORT auto sourcemeta::core::utf8_to_punycode (std::istream &input, std::ostream &output, std::size_t maximum_code_points=PUNYCODE_MAXIMUM_CODE_POINTS) -> void
SOURCEMETA_CORE_PUNYCODE_EXPORT auto sourcemeta::core::utf8_to_punycode (const std::string_view input, std::size_t maximum_code_points=PUNYCODE_MAXIMUM_CODE_POINTS) -> std::string
SOURCEMETA_CORE_PUNYCODE_EXPORT auto sourcemeta::core::punycode_to_utf32 (const std::string_view input, std::size_t maximum_encoded_bytes=PUNYCODE_MAXIMUM_ENCODED_BYTES) -> std::u32string
SOURCEMETA_CORE_PUNYCODE_EXPORT auto sourcemeta::core::punycode_to_utf8 (std::istream &input, std::ostream &output, std::size_t maximum_encoded_bytes=PUNYCODE_MAXIMUM_ENCODED_BYTES) -> void
SOURCEMETA_CORE_PUNYCODE_EXPORT auto sourcemeta::core::punycode_to_utf8 (const std::string_view input, std::size_t maximum_encoded_bytes=PUNYCODE_MAXIMUM_ENCODED_BYTES) -> std::string

Variables

constexpr std::size_t sourcemeta::core::PUNYCODE_MAXIMUM_CODE_POINTS
constexpr std::size_t sourcemeta::core::PUNYCODE_MAXIMUM_ENCODED_BYTES

Detailed Description

An implementation of RFC 3492 Punycode.

This functionality is included as follows:

#include <sourcemeta/core/punycode.h>

Class Documentation

◆ sourcemeta::core::PunycodeError

class sourcemeta::core::PunycodeError

An error that represents a Punycode encoding or decoding failure

Inheritance diagram for sourcemeta::core::PunycodeError:
sourcemeta::core::Error

Public Member Functions

 PunycodeError (const StaticString message) noexcept
 Construct an error with the given message.
Public Member Functions inherited from sourcemeta::core::Error
auto message () const noexcept -> StaticString

Additional Inherited Members

Protected Member Functions inherited from sourcemeta::core::Error
 Error (const StaticString message) noexcept
 Construct the error given its message.

Function Documentation

◆ punycode_to_utf32()

SOURCEMETA_CORE_PUNYCODE_EXPORT auto sourcemeta::core::punycode_to_utf32 ( const std::string_view input,
std::size_t maximum_encoded_bytes = PUNYCODE_MAXIMUM_ENCODED_BYTES ) -> std::u32string

Decode Punycode to Unicode code points (UTF-32). For example:

#include <sourcemeta/core/punycode.h>
#include <cassert>
const std::u32string expected{0x0048, 0x0065, 0x006C, 0x006C, 0x006F,
0x002D, 0x305D, 0x308C, 0x305E, 0x308C,
0x306E, 0x5834, 0x6240};
assert(sourcemeta::core::punycode_to_utf32("Hello--fc4qua05auwb3674vfr0b")
==
expected);
SOURCEMETA_CORE_PUNYCODE_EXPORT auto punycode_to_utf32(const std::string_view input, std::size_t maximum_encoded_bytes=PUNYCODE_MAXIMUM_ENCODED_BYTES) -> std::u32string

Note that stream-based overloads for UTF-32 are not provided because the C++ standard library does not define the required locale facets (std::ctype<char32_t>) for std::basic_istream<char32_t> and std::basic_ostream<char32_t> to function properly.

A bound smaller than the default refuses a larger input.

◆ punycode_to_utf8() [1/2]

SOURCEMETA_CORE_PUNYCODE_EXPORT auto sourcemeta::core::punycode_to_utf8 ( const std::string_view input,
std::size_t maximum_encoded_bytes = PUNYCODE_MAXIMUM_ENCODED_BYTES ) -> std::string

Decode Punycode to UTF-8. For example:

#include <sourcemeta/core/punycode.h>
#include <cassert>
assert(sourcemeta::core::punycode_to_utf8("Mnchen-3ya") ==
"M\xC3\xBCnchen");
SOURCEMETA_CORE_PUNYCODE_EXPORT auto punycode_to_utf8(std::istream &input, std::ostream &output, std::size_t maximum_encoded_bytes=PUNYCODE_MAXIMUM_ENCODED_BYTES) -> void

A bound smaller than the default refuses a larger input.

◆ punycode_to_utf8() [2/2]

SOURCEMETA_CORE_PUNYCODE_EXPORT auto sourcemeta::core::punycode_to_utf8 ( std::istream & input,
std::ostream & output,
std::size_t maximum_encoded_bytes = PUNYCODE_MAXIMUM_ENCODED_BYTES ) -> void

Decode Punycode to UTF-8 using streams. For example:

#include <sourcemeta/core/punycode.h>
#include <sstream>
#include <cassert>
std::istringstream input{"Mnchen-3ya"};
std::ostringstream output;
assert(output.str() == "M\xC3\xBCnchen");

A bound smaller than the default refuses a larger input.

◆ utf32_to_punycode()

SOURCEMETA_CORE_PUNYCODE_EXPORT auto sourcemeta::core::utf32_to_punycode ( std::u32string_view input,
std::size_t maximum_code_points = PUNYCODE_MAXIMUM_CODE_POINTS ) -> std::string

Encode Unicode code points (UTF-32) to Punycode. For example:

#include <sourcemeta/core/punycode.h>
#include <cassert>
const std::u32string input{0x0048, 0x0065, 0x006C, 0x006C, 0x006F,
0x002D, 0x305D, 0x308C, 0x305E, 0x308C,
0x306E, 0x5834, 0x6240};
"Hello--fc4qua05auwb3674vfr0b");
SOURCEMETA_CORE_PUNYCODE_EXPORT auto utf32_to_punycode(std::u32string_view input, std::size_t maximum_code_points=PUNYCODE_MAXIMUM_CODE_POINTS) -> std::string

Note that stream-based overloads for UTF-32 are not provided because the C++ standard library does not define the required locale facets (std::ctype<char32_t>) for std::basic_istream<char32_t> and std::basic_ostream<char32_t> to function properly.

A bound smaller than the default refuses a larger input.

◆ utf8_to_punycode() [1/2]

SOURCEMETA_CORE_PUNYCODE_EXPORT auto sourcemeta::core::utf8_to_punycode ( const std::string_view input,
std::size_t maximum_code_points = PUNYCODE_MAXIMUM_CODE_POINTS ) -> std::string

Encode UTF-8 to Punycode. For example:

#include <sourcemeta/core/punycode.h>
#include <cassert>
assert(sourcemeta::core::utf8_to_punycode("M\xC3\xBCnchen") ==
"Mnchen-3ya");
SOURCEMETA_CORE_PUNYCODE_EXPORT auto utf8_to_punycode(std::istream &input, std::ostream &output, std::size_t maximum_code_points=PUNYCODE_MAXIMUM_CODE_POINTS) -> void

A bound smaller than the default refuses a larger input.

◆ utf8_to_punycode() [2/2]

SOURCEMETA_CORE_PUNYCODE_EXPORT auto sourcemeta::core::utf8_to_punycode ( std::istream & input,
std::ostream & output,
std::size_t maximum_code_points = PUNYCODE_MAXIMUM_CODE_POINTS ) -> void

Encode UTF-8 to Punycode using streams. For example:

#include <sourcemeta/core/punycode.h>
#include <sstream>
#include <cassert>
std::istringstream input{"M\xC3\xBCnchen"};
std::ostringstream output;
assert(output.str() == "Mnchen-3ya");

A bound smaller than the default refuses a larger input.

Variable Documentation

◆ PUNYCODE_MAXIMUM_CODE_POINTS

std::size_t sourcemeta::core::PUNYCODE_MAXIMUM_CODE_POINTS
inlineconstexpr
Initial value:
{
std::numeric_limits<std::uint32_t>::max()}

The most code points an encode accepts when a caller names no bound of its own. RFC 3492 Section 6.4 offers bounding the input as one way of keeping the position counters from wrapping, which is the ceiling this stands at, so a caller may name a smaller bound but never a larger one.

◆ PUNYCODE_MAXIMUM_ENCODED_BYTES

std::size_t sourcemeta::core::PUNYCODE_MAXIMUM_ENCODED_BYTES
inlineconstexpr
Initial value:
{
std::numeric_limits<std::uint32_t>::max()}

The most bytes a decode accepts when a caller names no bound of its own, which stands at the same ceiling the encode bound does and may likewise only be tightened.