Skip to content

Commit

Permalink
馃摑 Adjust format and text boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePhD committed Jun 13, 2022
1 parent 0f68674 commit 88324df
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 60 deletions.
2 changes: 1 addition & 1 deletion examples/documentation/snippets/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CompactNamespaces: true
FixNamespaceComments: true

# Overall Alignment
ColumnLimit: 75
ColumnLimit: 85
AlignAfterOpenBracket: DontAlign # uses ContinuationIndentWidth for this instead
AccessModifierOffset: -5 # do not push public: or private: around
AlignConsecutiveAssignments: true # affects more than what's expected: do not use
Expand Down
20 changes: 7 additions & 13 deletions examples/documentation/snippets/source/error_handler.anatomy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ============================================================================
// //
// ============================================================================ //

#include <ztd/text.hpp>

struct my_error_handler {
// Helper definitions
template <typename Encoding>
using code_point_span
= ztd::span<const ztd::text::code_point_t<Encoding>>;
using code_point_span = ztd::span<const ztd::text::code_point_t<Encoding>>;
template <typename Encoding>
using code_unit_span
= ztd::span<const ztd::text::code_unit_t<Encoding>>;
using code_unit_span = ztd::span<const ztd::text::code_unit_t<Encoding>>;

// Function call operator that returns a "deduced" (auto) type
// Specifically, this one is called for encode failures
template <typename Encoding, typename Input, typename Output,
typename State>
template <typename Encoding, typename Input, typename Output, typename State>
auto operator()(
// First Parameter
const Encoding& encoding,
Expand All @@ -62,8 +58,7 @@ struct my_error_handler {

// Function call operator that returns a "deduced" (auto) type
// Specifically, this one is called for decode failures
template <typename Encoding, typename Input, typename Output,
typename State>
template <typename Encoding, typename Input, typename Output, typename State>
auto operator()(
// First Parameter
const Encoding& encoding,
Expand All @@ -85,9 +80,8 @@ int main(int, char* argv[]) {

// convert from execution encoding to utf8 encoding,
// using our new handler
std::string utf8_string = ztd::text::transcode(
std::string_view(argv[0]), ztd::text::execution,
ztd::text::basic_utf8<char> {}, my_error_handler {});
std::string utf8_string = ztd::text::transcode(std::string_view(argv[0]),
ztd::text::execution, ztd::text::basic_utf8<char> {}, my_error_handler {});

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ============================================================================
// //
// ============================================================================ //

#include <ztd/text/encode.hpp>
#include <ztd/text/encoding.hpp>
Expand All @@ -43,21 +42,19 @@ using ascii_encode_result = ztd::text::encode_result<
ztd::text::encode_state_t<ztd::text::ascii_t>>;

ascii_encode_result my_printing_handler(const ztd::text::ascii_t& encoding,
ascii_encode_result result,
ztd::span<const char32_t> unused_read_characters,
ascii_encode_result result, ztd::span<const char32_t> unused_read_characters,
ztd::span<const char> unused_write_characters) noexcept {
(void)encoding;
// just printing some information
std::cout << "An error occurred.\n"
<< "\tError code value: "
<< ztd::text::to_name(result.error_code) << "\n"
<< "\t# of code unit spaces left: " << result.output.size()
<< "\tError code value: " << ztd::text::to_name(result.error_code)
<< "\n"
<< "\t# of unused code points: "
<< unused_read_characters.size() << "\n"
<< "\t# of code unit spaces left: " << result.output.size() << "\n"
<< "\t# of unused code points: " << unused_read_characters.size()
<< "\n"
<< "\n"
<< "\t# of unused code units: " << unused_write_characters.size()
<< "\n"
<< "\t# of unused code units: "
<< unused_write_characters.size() << "\n"
<< "\tInput units left: " << result.input.size() << "\n";
// setting the error to "ok"
// tells the algorithm to keep spinning,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ============================================================================
// //
// ============================================================================ //

#include <ztd/text/transcode.hpp>

Expand Down
56 changes: 23 additions & 33 deletions examples/documentation/snippets/source/runtime_locale_encoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ============================================================================
// //
// ============================================================================ //

#include <ztd/text.hpp>

Expand Down Expand Up @@ -69,8 +68,7 @@ class runtime_locale {
decode_state() noexcept : c_stdlib_state() {
// properly set for mbrtoc32 state
code_point ghost_ouput[2] {};
UCHAR_ACCESS mbrtoc32(
ghost_ouput, "\0", 1, &c_stdlib_state);
UCHAR_ACCESS mbrtoc32(ghost_ouput, "\0", 1, &c_stdlib_state);
}
};

Expand Down Expand Up @@ -113,8 +111,7 @@ class runtime_locale {
ztd::span<const code_unit> replacement_code_units() const noexcept {
if (this->contains_unicode_encoding()) {
// Probably CESU-8 or UTF-8!
static const char replacement[3]
= { '\xEF', '\xBF', '\xBD' };
static const char replacement[3] = { '\xEF', '\xBF', '\xBD' };
return replacement;
}
else {
Expand All @@ -125,37 +122,33 @@ class runtime_locale {
}

private:
using rtl_decode_result
= ztd::text::decode_result<ztd::span<const code_unit>,
ztd::span<code_point>, decode_state>;
using rtl_encode_result
= ztd::text::encode_result<ztd::span<const code_point>,
ztd::span<code_unit>, encode_state>;
using rtl_decode_error_handler = std::function<rtl_decode_result(
const runtime_locale&, rtl_decode_result, ztd::span<const char>,
ztd::span<const char32_t>)>;
using rtl_encode_error_handler = std::function<rtl_encode_result(
const runtime_locale&, rtl_encode_result,
ztd::span<const char32_t>, ztd::span<const char>)>;
using rtl_decode_result = ztd::text::decode_result<ztd::span<const code_unit>,
ztd::span<code_point>, decode_state>;
using rtl_encode_result = ztd::text::encode_result<ztd::span<const code_point>,
ztd::span<code_unit>, encode_state>;
using rtl_decode_error_handler
= std::function<rtl_decode_result(const runtime_locale&, rtl_decode_result,
ztd::span<const char>, ztd::span<const char32_t>)>;
using rtl_encode_error_handler
= std::function<rtl_encode_result(const runtime_locale&, rtl_encode_result,
ztd::span<const char32_t>, ztd::span<const char>)>;

using empty_code_unit_span = ztd::span<const code_unit, 0>;
using empty_code_point_span = ztd::span<const code_point, 0>;

public:
rtl_decode_result decode_one(
ztd::span<const code_unit> input, ztd::span<code_point> output,
rtl_decode_error_handler error_handler,
rtl_decode_result decode_one(ztd::span<const code_unit> input,
ztd::span<code_point> output, rtl_decode_error_handler error_handler,
decode_state& current // decode-based state
) const {
if (output.size() < 1) {
return error_handler(*this,
rtl_decode_result(input, output, current,
ztd::text::encoding_error::
insufficient_output_space),
ztd::text::encoding_error::insufficient_output_space),
empty_code_unit_span(), empty_code_point_span());
}
std::size_t result = UCHAR_ACCESS mbrtoc32(output.data(),
input.data(), input.size(), &current.c_stdlib_state);
std::size_t result = UCHAR_ACCESS mbrtoc32(
output.data(), input.data(), input.size(), &current.c_stdlib_state);
switch (result) {
case (std::size_t)0:
// '\0' was encountered in the input
Expand Down Expand Up @@ -187,9 +180,8 @@ class runtime_locale {
input.subspan(result), output.subspan(1), current);
}

rtl_encode_result encode_one(
ztd::span<const code_point> input, ztd::span<code_unit> output,
rtl_encode_error_handler error_handler,
rtl_encode_result encode_one(ztd::span<const code_point> input,
ztd::span<code_unit> output, rtl_encode_error_handler error_handler,
encode_state& current // encode-based state
) const {
// saved, in case we need to go
Expand All @@ -207,9 +199,8 @@ class runtime_locale {
// no more input: everything is fine
return rtl_encode_result(input, output, current);
}
std::size_t result
= UCHAR_ACCESS c32rtomb(intermediate_buffer,
*input.data(), &current.c_stdlib_state);
std::size_t result = UCHAR_ACCESS c32rtomb(
intermediate_buffer, *input.data(), &current.c_stdlib_state);
if (result == (std::size_t)-1) {
// invalid sequence!
return error_handler(*this,
Expand All @@ -229,8 +220,7 @@ class runtime_locale {
// can't fit!!
return error_handler(*this,
rtl_encode_result(original_input, output, current,
ztd::text::encoding_error::
insufficient_output_space),
ztd::text::encoding_error::insufficient_output_space),
empty_code_point_span(), empty_code_unit_span());
}
::std::memcpy(output.data(), intermediate_buffer,
Expand Down

0 comments on commit 88324df

Please sign in to comment.