| 1 |
#ifndef ICU4XGraphemeClusterSegmenter_HPP |
| 2 |
#define ICU4XGraphemeClusterSegmenter_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 "ICU4XGraphemeClusterSegmenter.h" |
| 13 |
|
| 14 |
class ICU4XDataProvider; |
| 15 |
class ICU4XGraphemeClusterSegmenter; |
| 16 |
#include "ICU4XError.hpp" |
| 17 |
class ICU4XGraphemeClusterBreakIteratorUtf8; |
| 18 |
class ICU4XGraphemeClusterBreakIteratorUtf16; |
| 19 |
class ICU4XGraphemeClusterBreakIteratorLatin1; |
| 20 |
|
| 21 |
/** |
| 22 |
* A destruction policy for using ICU4XGraphemeClusterSegmenter with std::unique_ptr. |
| 23 |
*/ |
| 24 |
struct ICU4XGraphemeClusterSegmenterDeleter { |
| 25 |
void operator()(capi::ICU4XGraphemeClusterSegmenter* l) const noexcept { |
| 26 |
capi::ICU4XGraphemeClusterSegmenter_destroy(l); |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
/** |
| 31 |
* An ICU4X grapheme-cluster-break segmenter, capable of finding grapheme cluster breakpoints |
| 32 |
* in strings. |
| 33 |
* |
| 34 |
* See the [Rust documentation for `GraphemeClusterSegmenter`](https://docs.rs/icu/latest/icu/segmenter/struct.GraphemeClusterSegmenter.html) for more information. |
| 35 |
*/ |
| 36 |
class ICU4XGraphemeClusterSegmenter { |
| 37 |
public: |
| 38 |
|
| 39 |
/** |
| 40 |
* Construct an [`ICU4XGraphemeClusterSegmenter`]. |
| 41 |
* |
| 42 |
* See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/segmenter/struct.GraphemeClusterSegmenter.html#method.new) for more information. |
| 43 |
*/ |
| 44 |
static diplomat::result<ICU4XGraphemeClusterSegmenter, ICU4XError> create(const ICU4XDataProvider& provider); |
| 45 |
|
| 46 |
/** |
| 47 |
* Segments a string. |
| 48 |
* |
| 49 |
* Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according |
| 50 |
* to the WHATWG Encoding Standard. |
| 51 |
* |
| 52 |
* See the [Rust documentation for `segment_utf8`](https://docs.rs/icu/latest/icu/segmenter/struct.GraphemeClusterSegmenter.html#method.segment_utf8) for more information. |
| 53 |
* |
| 54 |
* Lifetimes: `this`, `input` must live at least as long as the output. |
| 55 |
*/ |
| 56 |
ICU4XGraphemeClusterBreakIteratorUtf8 segment_utf8(const std::string_view input) const; |
| 57 |
|
| 58 |
/** |
| 59 |
* Segments a string. |
| 60 |
* |
| 61 |
* Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according |
| 62 |
* to the WHATWG Encoding Standard. |
| 63 |
* |
| 64 |
* See the [Rust documentation for `segment_utf16`](https://docs.rs/icu/latest/icu/segmenter/struct.GraphemeClusterSegmenter.html#method.segment_utf16) for more information. |
| 65 |
* |
| 66 |
* Lifetimes: `this`, `input` must live at least as long as the output. |
| 67 |
*/ |
| 68 |
ICU4XGraphemeClusterBreakIteratorUtf16 segment_utf16(const std::u16string_view input) const; |
| 69 |
|
| 70 |
/** |
| 71 |
* Segments a Latin-1 string. |
| 72 |
* |
| 73 |
* See the [Rust documentation for `segment_latin1`](https://docs.rs/icu/latest/icu/segmenter/struct.GraphemeClusterSegmenter.html#method.segment_latin1) for more information. |
| 74 |
* |
| 75 |
* Lifetimes: `this`, `input` must live at least as long as the output. |
| 76 |
*/ |
| 77 |
ICU4XGraphemeClusterBreakIteratorLatin1 segment_latin1(const diplomat::span<const uint8_t> input) const; |
| 78 |
inline const capi::ICU4XGraphemeClusterSegmenter* AsFFI() const { return this->inner.get(); } |
| 79 |
inline capi::ICU4XGraphemeClusterSegmenter* AsFFIMut() { return this->inner.get(); } |
| 80 |
inline explicit ICU4XGraphemeClusterSegmenter(capi::ICU4XGraphemeClusterSegmenter* i) : inner(i) {} |
| 81 |
ICU4XGraphemeClusterSegmenter() = default; |
| 82 |
ICU4XGraphemeClusterSegmenter(ICU4XGraphemeClusterSegmenter&&) noexcept = default; |
| 83 |
ICU4XGraphemeClusterSegmenter& operator=(ICU4XGraphemeClusterSegmenter&& other) noexcept = default; |
| 84 |
private: |
| 85 |
std::unique_ptr<capi::ICU4XGraphemeClusterSegmenter, ICU4XGraphemeClusterSegmenterDeleter> inner; |
| 86 |
}; |
| 87 |
|
| 88 |
#include "ICU4XDataProvider.hpp" |
| 89 |
#include "ICU4XGraphemeClusterBreakIteratorUtf8.hpp" |
| 90 |
#include "ICU4XGraphemeClusterBreakIteratorUtf16.hpp" |
| 91 |
#include "ICU4XGraphemeClusterBreakIteratorLatin1.hpp" |
| 92 |
|
| 93 |
inline diplomat::result<ICU4XGraphemeClusterSegmenter, ICU4XError> ICU4XGraphemeClusterSegmenter::create(const ICU4XDataProvider& provider) { |
| 94 |
auto diplomat_result_raw_out_value = capi::ICU4XGraphemeClusterSegmenter_create(provider.AsFFI()); |
| 95 |
diplomat::result<ICU4XGraphemeClusterSegmenter, ICU4XError> diplomat_result_out_value; |
| 96 |
if (diplomat_result_raw_out_value.is_ok) { |
| 97 |
diplomat_result_out_value = diplomat::Ok<ICU4XGraphemeClusterSegmenter>(ICU4XGraphemeClusterSegmenter(diplomat_result_raw_out_value.ok)); |
| 98 |
} else { |
| 99 |
diplomat_result_out_value = diplomat::Err<ICU4XError>(static_cast<ICU4XError>(diplomat_result_raw_out_value.err)); |
| 100 |
} |
| 101 |
return diplomat_result_out_value; |
| 102 |
} |
| 103 |
inline ICU4XGraphemeClusterBreakIteratorUtf8 ICU4XGraphemeClusterSegmenter::segment_utf8(const std::string_view input) const { |
| 104 |
return ICU4XGraphemeClusterBreakIteratorUtf8(capi::ICU4XGraphemeClusterSegmenter_segment_utf8(this->inner.get(), input.data(), input.size())); |
| 105 |
} |
| 106 |
inline ICU4XGraphemeClusterBreakIteratorUtf16 ICU4XGraphemeClusterSegmenter::segment_utf16(const std::u16string_view input) const { |
| 107 |
return ICU4XGraphemeClusterBreakIteratorUtf16(capi::ICU4XGraphemeClusterSegmenter_segment_utf16(this->inner.get(), input.data(), input.size())); |
| 108 |
} |
| 109 |
inline ICU4XGraphemeClusterBreakIteratorLatin1 ICU4XGraphemeClusterSegmenter::segment_latin1(const diplomat::span<const uint8_t> input) const { |
| 110 |
return ICU4XGraphemeClusterBreakIteratorLatin1(capi::ICU4XGraphemeClusterSegmenter_segment_latin1(this->inner.get(), input.data(), input.size())); |
| 111 |
} |
| 112 |
#endif |