Debian Codegraph @

intl/icu_capi/bindings/cpp/ICU4XSentenceSegmenter.hpp

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