Skip to content

Commit e3cf37f

Browse files
committed
Add methods to iterate exemplar characters
1 parent a503f5e commit e3cf37f

File tree

12 files changed

+316
-2
lines changed

12 files changed

+316
-2
lines changed

ffi/capi/bindings/js/ExemplarCharacters.d.ts

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/js/ExemplarCharacters.mjs

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/js/StringIterator.d.ts

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/js/StringIterator.mjs

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/js/index.d.ts

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/js/index.mjs

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/src/exemplar_chars.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#[diplomat::attr(auto, namespace = "icu4x")]
88
pub mod ffi {
99
use alloc::boxed::Box;
10+
use crate::{properties_iter::ffi::CodePointRangeIterator, string_iter::ffi::StringIterator};
1011

1112
#[cfg(feature = "buffer_provider")]
1213
use crate::provider::ffi::DataProvider;
@@ -47,6 +48,28 @@ pub mod ffi {
4748
self.0.as_borrowed().contains32(cp)
4849
}
4950

51+
/// Get an iterator of all the code point ranges in the current exemplar character set.
52+
#[diplomat::rust_link(
53+
icu::collections::codepointinvliststringlist::CodePointInversionListAndStringList::code_points,
54+
FnInStruct
55+
)]
56+
pub fn code_point_ranges<'a>(&'a self) -> Box<CodePointRangeIterator<'a>> {
57+
let ranges = self.0.as_borrowed().code_points().iter_ranges().collect::<Vec<_>>();
58+
59+
Box::new(CodePointRangeIterator(Box::new(ranges.into_iter())))
60+
}
61+
62+
/// Get an iterator of all the code point ranges in the current exemplar character set.
63+
#[diplomat::rust_link(
64+
icu::collections::codepointinvliststringlist::CodePointInversionListAndStringList::strings,
65+
FnInStruct
66+
)]
67+
pub fn strings(&self) -> Box<StringIterator> {
68+
let strings = self.0.as_borrowed().strings().iter().map(|s| s.to_string()).collect::<Vec<_>>();
69+
70+
Box::new(StringIterator(Box::new(strings.into_iter())))
71+
}
72+
5073
/// Create an [`ExemplarCharacters`] for the "main" set of exemplar characters for a given locale, using compiled data.
5174
#[diplomat::rust_link(
5275
icu::locale::exemplar_chars::ExemplarCharacters::try_new_main,

ffi/capi/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ pub mod properties_sets;
118118
pub mod properties_unisets;
119119
#[cfg(feature = "properties")]
120120
pub mod script;
121+
#[cfg(feature = "properties")]
122+
pub mod string_iter;
121123
#[cfg(feature = "segmenter")]
122124
pub mod segmenter_grapheme;
123125
#[cfg(feature = "segmenter")]

ffi/capi/src/string_iter.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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"]
7+
#[diplomat::attr(auto, namespace = "icu4x")]
8+
pub mod ffi {
9+
use core::fmt::Write;
10+
use alloc::boxed::Box;
11+
12+
/// An iterator over strings
13+
#[diplomat::opaque]
14+
pub struct StringIterator(
15+
pub Box<dyn Iterator<Item = String>>,
16+
);
17+
18+
impl StringIterator {
19+
/// Advance the iterator by one and return the next string, terminated with a null byte.
20+
/// If there are no more strings to be iterated, an empty string is returned.
21+
pub fn next(&mut self, write: &mut DiplomatWrite) {
22+
let _ = write.write_str(&self.0.next().map(|mut s| { s.push('\0'); s }).unwrap_or_default());
23+
}
24+
}
25+
}

tutorials/npm/index.html

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@
8787
<li class="nav-item" role="presentation">
8888
<button class="nav-link" data-bs-toggle="pill" data-bs-target="#pills-seg" type="button" role="tab" aria-controls="pills-seg" aria-selected="false">Segmenter</button>
8989
</li>
90+
<li class="nav-item" role="presentation">
91+
<button class="nav-link" data-bs-toggle="pill" data-bs-target="#pills-ech" type="button" role="tab" aria-controls="pills-ech" aria-selected="false">Exemplar Characters</button>
92+
</li>
9093
</ul>
9194
</div>
9295

@@ -235,6 +238,59 @@
235238
</div>
236239
</div>
237240
</div>
241+
<div class="tab-pane" id="pills-ech" role="tabpanel" aria-labelledby="pills-ech-tab" tabindex="0">
242+
<div class="vstack gap-2">
243+
<label>
244+
Locale
245+
<div class="btn-toolbar" role="toolbar" aria-label="Fixed Decimal Formatting options">
246+
<div class="btn-group" role="group" aria-label="Fixed Decimal Formatting Locale">
247+
<input type="radio" class="btn-check" name="ech-locale" id="ech-locale-en" value="en" checked>
248+
<label class="btn btn-outline-primary" for="ech-locale-en">en</label>
249+
250+
<input type="radio" class="btn-check" name="ech-locale" id="ech-locale-bn" value="bn">
251+
<label class="btn btn-outline-primary" for="ech-locale-bn">bn</label>
252+
253+
<div class="input-group">
254+
<input type="radio" class="btn-check" name="ech-locale" id="ech-locale-other" value="other">
255+
<label class="btn btn-outline-primary" for="ech-locale-other">other</label>
256+
<input type="text" id="ech-locale-other-input" class="form-control" placeholder="Locale ID">
257+
</div>
258+
</div>
259+
</div>
260+
</label>
261+
262+
<div class="card">
263+
<div class="card-header">Main</div>
264+
<div class="card-body">
265+
<p id="ech-output-main" class="card-text"></p>
266+
</div>
267+
</div>
268+
<div class="card">
269+
<div class="card-header">Auxiliary</div>
270+
<div class="card-body">
271+
<p id="ech-output-auxiliary" class="card-text"></p>
272+
</div>
273+
</div>
274+
<div class="card">
275+
<div class="card-header">Punctuation</div>
276+
<div class="card-body">
277+
<p id="ech-output-punctuation" class="card-text"></p>
278+
</div>
279+
</div>
280+
<div class="card">
281+
<div class="card-header">Numbers</div>
282+
<div class="card-body">
283+
<p id="ech-output-numbers" class="card-text"></p>
284+
</div>
285+
</div>
286+
<div class="card">
287+
<div class="card-header">Index</div>
288+
<div class="card-body">
289+
<p id="ech-output-index" class="card-text"></p>
290+
</div>
291+
</div>
292+
</div>
293+
</div>
238294
</div>
239295
</div>
240296
<div id="bigspinner">
@@ -287,4 +343,4 @@
287343
}
288344
</style>
289345
</body>
290-
</html>
346+
</html>

tutorials/npm/src/ts/app.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fdf from './fixed-decimal';
22
import * as dtf from './date-time';
33
import * as seg from './segmenter';
4+
import * as ech from './exemplar-characters';
45

56
import 'bootstrap/js/dist/tab';
67
import 'bootstrap/js/dist/dropdown';
@@ -10,5 +11,6 @@ import 'bootstrap/js/dist/collapse';
1011
fdf.setup();
1112
dtf.setup();
1213
seg.setup();
14+
ech.setup();
1315
(document.querySelector("#bigspinner") as HTMLElement).style.display = "none";
14-
})()
16+
})()

0 commit comments

Comments
 (0)