Skip to main content

icu_capi/
segmenter_grapheme.rs

1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5#[diplomat::bridge]
6#[diplomat::abi_rename = "icu4x_{0}_mv1"]
7pub mod ffi {
8    use alloc::boxed::Box;
9    use icu_segmenter::scaffold::{Latin1, PotentiallyIllFormedUtf8, Utf16};
10
11    #[cfg(feature = "buffer_provider")]
12    use crate::unstable::{errors::ffi::DataError, provider::ffi::DataProvider};
13
14    #[diplomat::opaque]
15    /// An ICU4X grapheme-cluster-break segmenter, capable of finding grapheme cluster breakpoints
16    /// in strings.
17    #[diplomat::rust_link(icu::segmenter::GraphemeClusterSegmenter, Struct)]
18    #[diplomat::rust_link(icu::segmenter::GraphemeClusterSegmenterBorrowed, Struct, hidden)]
19    pub struct GraphemeClusterSegmenter(icu_segmenter::GraphemeClusterSegmenter);
20
21    #[diplomat::opaque]
22    #[diplomat::rust_link(icu::segmenter::iterators::GraphemeClusterBreakIterator, Struct)]
23    #[diplomat::attr(demo_gen, disable)] // iterator type
24    pub struct GraphemeClusterBreakIteratorUtf8<'a>(
25        icu_segmenter::iterators::GraphemeClusterBreakIterator<'a, 'a, PotentiallyIllFormedUtf8>,
26    );
27
28    #[diplomat::opaque]
29    #[diplomat::rust_link(icu::segmenter::iterators::GraphemeClusterBreakIterator, Struct)]
30    #[diplomat::attr(demo_gen, disable)] // iterator type
31    pub struct GraphemeClusterBreakIteratorUtf16<'a>(
32        icu_segmenter::iterators::GraphemeClusterBreakIterator<'a, 'a, Utf16>,
33    );
34
35    #[diplomat::opaque]
36    #[diplomat::rust_link(icu::segmenter::iterators::GraphemeClusterBreakIterator, Struct)]
37    #[diplomat::attr(demo_gen, disable)] // iterator type
38    pub struct GraphemeClusterBreakIteratorLatin1<'a>(
39        icu_segmenter::iterators::GraphemeClusterBreakIterator<'a, 'a, Latin1>,
40    );
41
42    impl GraphemeClusterSegmenter {
43        /// Construct an [`GraphemeClusterSegmenter`] using compiled data.
44        #[diplomat::rust_link(icu::segmenter::GraphemeClusterSegmenter::new, FnInStruct)]
45        #[diplomat::attr(auto, constructor)]
46        #[cfg(feature = "compiled_data")]
47        pub fn create() -> Box<GraphemeClusterSegmenter> {
48            Box::new(GraphemeClusterSegmenter(
49                icu_segmenter::GraphemeClusterSegmenter::new().static_to_owned(),
50            ))
51        }
52        /// Construct an [`GraphemeClusterSegmenter`].
53        #[diplomat::rust_link(icu::segmenter::GraphemeClusterSegmenter::new, FnInStruct)]
54        #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "with_provider")]
55        #[cfg(feature = "buffer_provider")]
56        pub fn create_with_provider(
57            provider: &DataProvider,
58        ) -> Result<Box<GraphemeClusterSegmenter>, DataError> {
59            Ok(Box::new(GraphemeClusterSegmenter(
60                icu_segmenter::GraphemeClusterSegmenter::try_new_with_buffer_provider(
61                    provider.get()?,
62                )?,
63            )))
64        }
65        /// Segments a string.
66        ///
67        /// Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according
68        /// to the WHATWG Encoding Standard.
69        #[diplomat::rust_link(
70            icu::segmenter::GraphemeClusterSegmenterBorrowed::segment_str,
71            FnInStruct,
72            hidden
73        )]
74        #[diplomat::rust_link(
75            icu::segmenter::GraphemeClusterSegmenterBorrowed::segment_utf8,
76            FnInStruct
77        )]
78        #[diplomat::attr(not(supports = utf8_strings), disable)]
79        #[diplomat::attr(*, rename = "segment")]
80        pub fn segment_utf8<'a>(
81            &'a self,
82            input: &'a DiplomatStr,
83        ) -> Box<GraphemeClusterBreakIteratorUtf8<'a>> {
84            Box::new(GraphemeClusterBreakIteratorUtf8(
85                self.0.as_borrowed().segment_utf8(input),
86            ))
87        }
88
89        /// Segments a string.
90        ///
91        /// Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according
92        /// to the WHATWG Encoding Standard.
93        #[diplomat::rust_link(
94            icu::segmenter::GraphemeClusterSegmenterBorrowed::segment_utf16,
95            FnInStruct
96        )]
97        #[diplomat::attr(not(supports = utf8_strings), rename = "segment")]
98        #[diplomat::attr(supports = utf8_strings, rename = "segment16")]
99        pub fn segment_utf16<'a>(
100            &'a self,
101            input: &'a DiplomatStr16,
102        ) -> Box<GraphemeClusterBreakIteratorUtf16<'a>> {
103            Box::new(GraphemeClusterBreakIteratorUtf16(
104                self.0.as_borrowed().segment_utf16(input),
105            ))
106        }
107
108        /// Segments a Latin-1 string.
109        #[diplomat::rust_link(
110            icu::segmenter::GraphemeClusterSegmenterBorrowed::segment_latin1,
111            FnInStruct
112        )]
113        #[diplomat::attr(not(supports = utf8_strings), disable)]
114        pub fn segment_latin1<'a>(
115            &'a self,
116            input: &'a [u8],
117        ) -> Box<GraphemeClusterBreakIteratorLatin1<'a>> {
118            Box::new(GraphemeClusterBreakIteratorLatin1(
119                self.0.as_borrowed().segment_latin1(input),
120            ))
121        }
122    }
123
124    impl<'a> GraphemeClusterBreakIteratorUtf8<'a> {
125        /// Finds the next breakpoint. Returns -1 if at the end of the string or if the index is
126        /// out of range of a 32-bit signed integer.
127        #[diplomat::rust_link(
128            icu::segmenter::iterators::GraphemeClusterBreakIterator::next,
129            FnInStruct
130        )]
131        #[diplomat::rust_link(
132            icu::segmenter::iterators::GraphemeClusterBreakIterator::Item,
133            AssociatedTypeInStruct,
134            hidden
135        )]
136        pub fn next(&mut self) -> i32 {
137            self.0
138                .next()
139                .and_then(|u| i32::try_from(u).ok())
140                .unwrap_or(-1)
141        }
142    }
143
144    impl<'a> GraphemeClusterBreakIteratorUtf16<'a> {
145        /// Finds the next breakpoint. Returns -1 if at the end of the string or if the index is
146        /// out of range of a 32-bit signed integer.
147        #[diplomat::rust_link(
148            icu::segmenter::iterators::GraphemeClusterBreakIterator::next,
149            FnInStruct
150        )]
151        #[diplomat::rust_link(
152            icu::segmenter::iterators::GraphemeClusterBreakIterator::Item,
153            AssociatedTypeInStruct,
154            hidden
155        )]
156        pub fn next(&mut self) -> i32 {
157            self.0
158                .next()
159                .and_then(|u| i32::try_from(u).ok())
160                .unwrap_or(-1)
161        }
162    }
163
164    impl<'a> GraphemeClusterBreakIteratorLatin1<'a> {
165        /// Finds the next breakpoint. Returns -1 if at the end of the string or if the index is
166        /// out of range of a 32-bit signed integer.
167        #[diplomat::rust_link(
168            icu::segmenter::iterators::GraphemeClusterBreakIterator::next,
169            FnInStruct
170        )]
171        #[diplomat::rust_link(
172            icu::segmenter::iterators::GraphemeClusterBreakIterator::Item,
173            AssociatedTypeInStruct,
174            hidden
175        )]
176        pub fn next(&mut self) -> i32 {
177            self.0
178                .next()
179                .and_then(|u| i32::try_from(u).ok())
180                .unwrap_or(-1)
181        }
182    }
183}