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 // An example application which uses icu_uniset to test what blocks of
6 // Basic Multilingual Plane a character belongs to.
7 //
8 // In this example we use `CodePointInversionListBuilder` to construct just the first
9 // two blocks of the first plane, and use an instance of a `BMPBlockSelector`
10 // to retrieve which of those blocks each character of a string belongs to.
11 //
12 // This is a simple example of the API use and is severely oversimplified
13 // compared to real Unicode block selection.
14
15 #![no_main] // https://github.com/unicode-org/icu4x/issues/395
16 icu_benchmark_macros::instrument!();
17 use icu_benchmark_macros::println;
18
19 use icu_collections::codepointinvlist::{CodePointInversionList, CodePointInversionListBuilder};
20 use std::ops::RangeInclusive;
21
22 #[derive(Copy, Clone, Debug)]
23 enum BmpBlock {
24 Basic,
25 Latin1Supplement,
26 Unknown,
27 }
28
29 const BLOCKS: [(BmpBlock, RangeInclusive<char>); 2] = [
30 (BmpBlock::Basic, '\u{0000}'..='\u{007F}'),
31 (BmpBlock::Latin1Supplement, '\u{0080}'..='\u{00FF}'),
32 ];
33
34 struct BmpBlockSelector {
35 blocks: [(BmpBlock, CodePointInversionList<'static>); 2],
36 }
37
38 impl BmpBlockSelector {
new() -> BmpBlockSelector39 pub fn new() -> BmpBlockSelector {
40 BmpBlockSelector {
41 blocks: BLOCKS.map(|(ch, range)| {
42 (ch, {
43 let mut builder = CodePointInversionListBuilder::new();
44 builder.add_range(range);
45 builder.build()
46 })
47 }),
48 }
49 }
50
select(&self, input: char) -> BmpBlock51 pub fn select(&self, input: char) -> BmpBlock {
52 for (block, set) in &self.blocks {
53 if set.contains(input) {
54 return *block;
55 }
56 }
57 BmpBlock::Unknown
58 }
59 }
60
main()61 fn main() {
62 let selector = BmpBlockSelector::new();
63
64 let sample = "Welcome to MyName©®, Алексей!";
65
66 println!("\n====== Unicode BMP Block Selector example ============");
67 for ch in sample.chars() {
68 let block = selector.select(ch);
69 println!("{ch}: {block:#?}");
70 }
71 }
72