| 1 |
#ifndef ICU4XBidi_HPP |
| 2 |
#define ICU4XBidi_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 "ICU4XBidi.h" |
| 13 |
|
| 14 |
class ICU4XDataProvider; |
| 15 |
class ICU4XBidi; |
| 16 |
#include "ICU4XError.hpp" |
| 17 |
class ICU4XBidiInfo; |
| 18 |
class ICU4XReorderedIndexMap; |
| 19 |
|
| 20 |
/** |
| 21 |
* A destruction policy for using ICU4XBidi with std::unique_ptr. |
| 22 |
*/ |
| 23 |
struct ICU4XBidiDeleter { |
| 24 |
void operator()(capi::ICU4XBidi* l) const noexcept { |
| 25 |
capi::ICU4XBidi_destroy(l); |
| 26 |
} |
| 27 |
}; |
| 28 |
|
| 29 |
/** |
| 30 |
* An ICU4X Bidi object, containing loaded bidi data |
| 31 |
* |
| 32 |
* See the [Rust documentation for `BidiClassAdapter`](https://docs.rs/icu/latest/icu/properties/bidi/struct.BidiClassAdapter.html) for more information. |
| 33 |
*/ |
| 34 |
class ICU4XBidi { |
| 35 |
public: |
| 36 |
|
| 37 |
/** |
| 38 |
* Creates a new [`ICU4XBidi`] from locale data. |
| 39 |
* |
| 40 |
* See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/properties/bidi/struct.BidiClassAdapter.html#method.new) for more information. |
| 41 |
*/ |
| 42 |
static diplomat::result<ICU4XBidi, ICU4XError> create(const ICU4XDataProvider& provider); |
| 43 |
|
| 44 |
/** |
| 45 |
* Use the data loaded in this object to process a string and calculate bidi information |
| 46 |
* |
| 47 |
* Takes in a Level for the default level, if it is an invalid value it will default to LTR |
| 48 |
* |
| 49 |
* Returns nothing if `text` is invalid UTF-8. |
| 50 |
* |
| 51 |
* See the [Rust documentation for `new_with_data_source`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.BidiInfo.html#method.new_with_data_source) for more information. |
| 52 |
* |
| 53 |
* Lifetimes: `text` must live at least as long as the output. |
| 54 |
*/ |
| 55 |
std::optional<ICU4XBidiInfo> for_text(const std::string_view text, uint8_t default_level) const; |
| 56 |
|
| 57 |
/** |
| 58 |
* Utility function for producing reorderings given a list of levels |
| 59 |
* |
| 60 |
* Produces a map saying which visual index maps to which source index. |
| 61 |
* |
| 62 |
* The levels array must not have values greater than 126 (this is the |
| 63 |
* Bidi maximum explicit depth plus one). |
| 64 |
* Failure to follow this invariant may lead to incorrect results, |
| 65 |
* but is still safe. |
| 66 |
* |
| 67 |
* See the [Rust documentation for `reorder_visual`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.BidiInfo.html#method.reorder_visual) for more information. |
| 68 |
*/ |
| 69 |
ICU4XReorderedIndexMap reorder_visual(const diplomat::span<const uint8_t> levels) const; |
| 70 |
|
| 71 |
/** |
| 72 |
* Check if a Level returned by level_at is an RTL level. |
| 73 |
* |
| 74 |
* Invalid levels (numbers greater than 125) will be assumed LTR |
| 75 |
* |
| 76 |
* See the [Rust documentation for `is_rtl`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.Level.html#method.is_rtl) for more information. |
| 77 |
*/ |
| 78 |
static bool level_is_rtl(uint8_t level); |
| 79 |
|
| 80 |
/** |
| 81 |
* Check if a Level returned by level_at is an LTR level. |
| 82 |
* |
| 83 |
* Invalid levels (numbers greater than 125) will be assumed LTR |
| 84 |
* |
| 85 |
* See the [Rust documentation for `is_ltr`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.Level.html#method.is_ltr) for more information. |
| 86 |
*/ |
| 87 |
static bool level_is_ltr(uint8_t level); |
| 88 |
|
| 89 |
/** |
| 90 |
* Get a basic RTL Level value |
| 91 |
* |
| 92 |
* See the [Rust documentation for `rtl`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.Level.html#method.rtl) for more information. |
| 93 |
*/ |
| 94 |
static uint8_t level_rtl(); |
| 95 |
|
| 96 |
/** |
| 97 |
* Get a simple LTR Level value |
| 98 |
* |
| 99 |
* See the [Rust documentation for `ltr`](https://docs.rs/unicode_bidi/latest/unicode_bidi/struct.Level.html#method.ltr) for more information. |
| 100 |
*/ |
| 101 |
static uint8_t level_ltr(); |
| 102 |
inline const capi::ICU4XBidi* AsFFI() const { return this->inner.get(); } |
| 103 |
inline capi::ICU4XBidi* AsFFIMut() { return this->inner.get(); } |
| 104 |
inline explicit ICU4XBidi(capi::ICU4XBidi* i) : inner(i) {} |
| 105 |
ICU4XBidi() = default; |
| 106 |
ICU4XBidi(ICU4XBidi&&) noexcept = default; |
| 107 |
ICU4XBidi& operator=(ICU4XBidi&& other) noexcept = default; |
| 108 |
private: |
| 109 |
std::unique_ptr<capi::ICU4XBidi, ICU4XBidiDeleter> inner; |
| 110 |
}; |
| 111 |
|
| 112 |
#include "ICU4XDataProvider.hpp" |
| 113 |
#include "ICU4XBidiInfo.hpp" |
| 114 |
#include "ICU4XReorderedIndexMap.hpp" |
| 115 |
|
| 116 |
inline diplomat::result<ICU4XBidi, ICU4XError> ICU4XBidi::create(const ICU4XDataProvider& provider) { |
| 117 |
auto diplomat_result_raw_out_value = capi::ICU4XBidi_create(provider.AsFFI()); |
| 118 |
diplomat::result<ICU4XBidi, ICU4XError> diplomat_result_out_value; |
| 119 |
if (diplomat_result_raw_out_value.is_ok) { |
| 120 |
diplomat_result_out_value = diplomat::Ok<ICU4XBidi>(ICU4XBidi(diplomat_result_raw_out_value.ok)); |
| 121 |
} else { |
| 122 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 123 |
} |
| 124 |
return diplomat_result_out_value; |
| 125 |
} |
| 126 |
inline std::optional<ICU4XBidiInfo> ICU4XBidi::for_text(const std::string_view text, uint8_t default_level) const { |
| 127 |
auto diplomat_optional_raw_out_value = capi::ICU4XBidi_for_text(this->inner.get(), text.data(), text.size(), default_level); |
| 128 |
std::optional<ICU4XBidiInfo> diplomat_optional_out_value; |
| 129 |
if (diplomat_optional_raw_out_value != nullptr) { |
| 130 |
diplomat_optional_out_value = ICU4XBidiInfo(diplomat_optional_raw_out_value); |
| 131 |
} else { |
| 132 |
diplomat_optional_out_value = std::nullopt; |
| 133 |
} |
| 134 |
return diplomat_optional_out_value; |
| 135 |
} |
| 136 |
inline ICU4XReorderedIndexMap ICU4XBidi::reorder_visual(const diplomat::span<const uint8_t> levels) const { |
| 137 |
return ICU4XReorderedIndexMap(capi::ICU4XBidi_reorder_visual(this->inner.get(), levels.data(), levels.size())); |
| 138 |
} |
| 139 |
inline bool ICU4XBidi::level_is_rtl(uint8_t level) { |
| 140 |
return capi::ICU4XBidi_level_is_rtl(level); |
| 141 |
} |
| 142 |
inline bool ICU4XBidi::level_is_ltr(uint8_t level) { |
| 143 |
return capi::ICU4XBidi_level_is_ltr(level); |
| 144 |
} |
| 145 |
inline uint8_t ICU4XBidi::level_rtl() { |
| 146 |
return capi::ICU4XBidi_level_rtl(); |
| 147 |
} |
| 148 |
inline uint8_t ICU4XBidi::level_ltr() { |
| 149 |
return capi::ICU4XBidi_level_ltr(); |
| 150 |
} |
| 151 |
#endif |