| 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 |
|
| 17 |
icu_benchmark_macros::static_setup!(); |
| 18 |
|
| 19 |
use icu_collections::codepointinvlist::{CodePointInversionList, CodePointInversionListBuilder}; |
| 20 |
|
| 21 |
fn get_basic_latin_block() -> CodePointInversionList<'static> { |
| 22 |
let mut builder = CodePointInversionListBuilder::new(); |
| 23 |
builder.add_range(&('\u{0000}'..='\u{007F}')); |
| 24 |
builder.build() |
| 25 |
} |
| 26 |
|
| 27 |
fn get_latin1_supplement_block() -> CodePointInversionList<'static> { |
| 28 |
let mut builder = CodePointInversionListBuilder::new(); |
| 29 |
builder.add_range(&('\u{0080}'..='\u{00FF}')); |
| 30 |
builder.build() |
| 31 |
} |
| 32 |
|
| 33 |
#[derive(Copy, Clone, Debug)] |
| 34 |
enum BmpBlock { |
| 35 |
Basic, |
| 36 |
Latin1Supplement, |
| 37 |
Unknown, |
| 38 |
} |
| 39 |
|
| 40 |
struct BmpBlockSelector<'data> { |
| 41 |
blocks: Vec<(BmpBlock, CodePointInversionList<'data>)>, |
| 42 |
} |
| 43 |
|
| 44 |
impl<'data> BmpBlockSelector<'data> { |
| 45 |
pub fn new() -> Self { |
| 46 |
let blocks = vec![ |
| 47 |
(BmpBlock::Basic, get_basic_latin_block()), |
| 48 |
(BmpBlock::Latin1Supplement, get_latin1_supplement_block()), |
| 49 |
]; |
| 50 |
BmpBlockSelector { blocks } |
| 51 |
} |
| 52 |
|
| 53 |
pub fn select(&self, input: char) -> BmpBlock { |
| 54 |
for (block, set) in &self.blocks { |
| 55 |
if set.contains(input) { |
| 56 |
return *block; |
| 57 |
} |
| 58 |
} |
| 59 |
BmpBlock::Unknown |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
fn print(_input: &str) { |
| 64 |
#[cfg(debug_assertions)] |
| 65 |
println!("{_input}"); |
| 66 |
} |
| 67 |
|
| 68 |
#[no_mangle] |
| 69 |
fn main(_argc: isize, _argv: *const *const u8) -> isize { |
| 70 |
icu_benchmark_macros::main_setup!(); |
| 71 |
let selector = BmpBlockSelector::new(); |
| 72 |
|
| 73 |
let sample = "Welcome to MyName©®, Алексей!"; |
| 74 |
|
| 75 |
let mut result = vec![]; |
| 76 |
|
| 77 |
for ch in sample.chars() { |
| 78 |
result.push((ch, selector.select(ch))); |
| 79 |
} |
| 80 |
|
| 81 |
print("\n====== Unicode BMP Block Selector example ============"); |
| 82 |
for (ch, block) in result { |
| 83 |
print(&format!("{ch}: {block:#?}")); |
| 84 |
} |
| 85 |
|
| 86 |
0 |
| 87 |
} |