| 1 |
#ifndef ICU4XBidiParagraph_HPP |
| 2 |
#define ICU4XBidiParagraph_HPP |
| 3 |
#include <stdint.h> |
| 4 |
#include <stddef.h> |
| 5 |
#include <stdbool.h> |
| 6 |
#include <algorithm> |
| 7 |
#include <memory> |
| 8 |
#include <variant> |
| 9 |
#include <optional> |
| 10 |
#include "diplomat_runtime.hpp" |
| 11 |
|
| 12 |
#include "ICU4XBidiParagraph.h" |
| 13 |
|
| 14 |
#include "ICU4XError.hpp" |
| 15 |
#include "ICU4XBidiDirection.hpp" |
| 16 |
|
| 17 |
/** |
| 18 |
* A destruction policy for using ICU4XBidiParagraph with std::unique_ptr. |
| 19 |
*/ |
| 20 |
struct ICU4XBidiParagraphDeleter { |
| 21 |
void operator()(capi::ICU4XBidiParagraph* l) const noexcept { |
| 22 |
capi::ICU4XBidiParagraph_destroy(l); |
| 23 |
} |
| 24 |
}; |
| 25 |
|
| 26 |
/** |
| 27 |
* Bidi information for a single processed paragraph |
| 28 |
*/ |
| 29 |
class ICU4XBidiParagraph { |
| 30 |
public: |
| 31 |
|
| 32 |
/** |
| 33 |
* Given a paragraph index `n` within the surrounding text, this sets this |
| 34 |
* object to the paragraph at that index. Returns `ICU4XError::OutOfBoundsError` when out of bounds. |
| 35 |
* |
| 36 |
* This is equivalent to calling `paragraph_at()` on `ICU4XBidiInfo` but doesn't |
| 37 |
* create a new object |
| 38 |
*/ |
| 39 |
diplomat::result<std::monostate, ICU4XError> set_paragraph_in_text(size_t n); |
| 40 |
|
| 41 |
/** |
| 42 |
* The primary direction of this paragraph |
| 43 |
* |
| 44 |
* See the [Rust documentation for `level_at`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.Paragraph.html#method.level_at) for more information. |
| 45 |
*/ |
| 46 |
ICU4XBidiDirection direction() const; |
| 47 |
|
| 48 |
/** |
| 49 |
* The number of bytes in this paragraph |
| 50 |
* |
| 51 |
* See the [Rust documentation for `len`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.ParagraphInfo.html#method.len) for more information. |
| 52 |
*/ |
| 53 |
size_t size() const; |
| 54 |
|
| 55 |
/** |
| 56 |
* The start index of this paragraph within the source text |
| 57 |
*/ |
| 58 |
size_t range_start() const; |
| 59 |
|
| 60 |
/** |
| 61 |
* The end index of this paragraph within the source text |
| 62 |
*/ |
| 63 |
size_t range_end() const; |
| 64 |
|
| 65 |
/** |
| 66 |
* Reorder a line based on display order. The ranges are specified relative to the source text and must be contained |
| 67 |
* within this paragraph's range. |
| 68 |
* |
| 69 |
* See the [Rust documentation for `level_at`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.Paragraph.html#method.level_at) for more information. |
| 70 |
*/ |
| 71 |
template<typename W> diplomat::result<std::monostate, ICU4XError> reorder_line_to_writeable(size_t range_start, size_t range_end, W& out) const; |
| 72 |
|
| 73 |
/** |
| 74 |
* Reorder a line based on display order. The ranges are specified relative to the source text and must be contained |
| 75 |
* within this paragraph's range. |
| 76 |
* |
| 77 |
* See the [Rust documentation for `level_at`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.Paragraph.html#method.level_at) for more information. |
| 78 |
*/ |
| 79 |
diplomat::result<std::string, ICU4XError> reorder_line(size_t range_start, size_t range_end) const; |
| 80 |
|
| 81 |
/** |
| 82 |
* Get the BIDI level at a particular byte index in this paragraph. |
| 83 |
* This integer is conceptually a `unicode_bidi::Level`, |
| 84 |
* and can be further inspected using the static methods on ICU4XBidi. |
| 85 |
* |
| 86 |
* Returns 0 (equivalent to `Level::ltr()`) on error |
| 87 |
* |
| 88 |
* See the [Rust documentation for `level_at`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.Paragraph.html#method.level_at) for more information. |
| 89 |
*/ |
| 90 |
uint8_t level_at(size_t pos) const; |
| 91 |
inline const capi::ICU4XBidiParagraph* AsFFI() const { return this->inner.get(); } |
| 92 |
inline capi::ICU4XBidiParagraph* AsFFIMut() { return this->inner.get(); } |
| 93 |
inline explicit ICU4XBidiParagraph(capi::ICU4XBidiParagraph* i) : inner(i) {} |
| 94 |
ICU4XBidiParagraph() = default; |
| 95 |
ICU4XBidiParagraph(ICU4XBidiParagraph&&) noexcept = default; |
| 96 |
ICU4XBidiParagraph& operator=(ICU4XBidiParagraph&& other) noexcept = default; |
| 97 |
private: |
| 98 |
std::unique_ptr<capi::ICU4XBidiParagraph, ICU4XBidiParagraphDeleter> inner; |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
inline diplomat::result<std::monostate, ICU4XError> ICU4XBidiParagraph::set_paragraph_in_text(size_t n) { |
| 103 |
auto diplomat_result_raw_out_value = capi::ICU4XBidiParagraph_set_paragraph_in_text(this->inner.get(), n); |
| 104 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 105 |
if (diplomat_result_raw_out_value.is_ok) { |
| 106 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 107 |
} else { |
| 108 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 109 |
} |
| 110 |
return diplomat_result_out_value; |
| 111 |
} |
| 112 |
inline ICU4XBidiDirection ICU4XBidiParagraph::direction() const { |
| 113 |
return static_cast<ICU4XBidiDirection>(capi::ICU4XBidiParagraph_direction(this->inner.get())); |
| 114 |
} |
| 115 |
inline size_t ICU4XBidiParagraph::size() const { |
| 116 |
return capi::ICU4XBidiParagraph_size(this->inner.get()); |
| 117 |
} |
| 118 |
inline size_t ICU4XBidiParagraph::range_start() const { |
| 119 |
return capi::ICU4XBidiParagraph_range_start(this->inner.get()); |
| 120 |
} |
| 121 |
inline size_t ICU4XBidiParagraph::range_end() const { |
| 122 |
return capi::ICU4XBidiParagraph_range_end(this->inner.get()); |
| 123 |
} |
| 124 |
template<typename W> inline diplomat::result<std::monostate, ICU4XError> ICU4XBidiParagraph::reorder_line_to_writeable(size_t range_start, size_t range_end, W& out) const { |
| 125 |
capi::DiplomatWriteable out_writer = diplomat::WriteableTrait<W>::Construct(out); |
| 126 |
auto diplomat_result_raw_out_value = capi::ICU4XBidiParagraph_reorder_line(this->inner.get(), range_start, range_end, &out_writer); |
| 127 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 128 |
if (diplomat_result_raw_out_value.is_ok) { |
| 129 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 130 |
} else { |
| 131 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 132 |
} |
| 133 |
return diplomat_result_out_value; |
| 134 |
} |
| 135 |
inline diplomat::result<std::string, ICU4XError> ICU4XBidiParagraph::reorder_line(size_t range_start, size_t range_end) const { |
| 136 |
std::string diplomat_writeable_string; |
| 137 |
capi::DiplomatWriteable diplomat_writeable_out = diplomat::WriteableFromString(diplomat_writeable_string); |
| 138 |
auto diplomat_result_raw_out_value = capi::ICU4XBidiParagraph_reorder_line(this->inner.get(), range_start, range_end, &diplomat_writeable_out); |
| 139 |
diplomat::result<std::monostate, ICU4XError> diplomat_result_out_value; |
| 140 |
if (diplomat_result_raw_out_value.is_ok) { |
| 141 |
diplomat_result_out_value = diplomat::Ok<std::monostate>(std::monostate()); |
| 142 |
} else { |
| 143 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 144 |
} |
| 145 |
return diplomat_result_out_value.replace_ok(std::move(diplomat_writeable_string)); |
| 146 |
} |
| 147 |
inline uint8_t ICU4XBidiParagraph::level_at(size_t pos) const { |
| 148 |
return capi::ICU4XBidiParagraph_level_at(this->inner.get(), pos); |
| 149 |
} |
| 150 |
#endif |