unic_ucd_block/
block.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2012-2015 The Rust Project Developers.
// Copyright 2017 The UNIC Project Developers.
//
// See the COPYRIGHT file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use unic_char_property::{tables::CharDataTableIter, PartialCharProperty};
use unic_char_range::CharRange;

/// A Unicode Block.
///
/// Blocks are contiguous range of code points, uniquely named, and have no overlaps.
///
/// All Assigned characters have a Block property value, but Reserved characters may or may not
/// have a Block.
#[derive(Clone, Copy, Debug)]
pub struct Block {
    /// The Character range of the Block.
    pub range: CharRange,

    /// The unique name of the Block.
    pub name: &'static str,

    // Private field to keep struct expandable.
    _priv: (),
}

impl Block {
    /// Find the character `Block` property value.
    pub fn of(ch: char) -> Option<Block> {
        match data::BLOCKS.find_with_range(ch) {
            None => None,
            Some((range, name)) => Some(Block {
                range,
                name,
                _priv: (),
            }),
        }
    }
}

impl PartialCharProperty for Block {
    fn of(ch: char) -> Option<Self> {
        Self::of(ch)
    }
}

/// Iterator for all assigned Unicode Blocks, except:
/// - U+D800..U+DB7F, High Surrogates
/// - U+DB80..U+DBFF, High Private Use Surrogates
/// - U+DC00..U+DFFF, Low Surrogates
#[derive(Debug)]
pub struct BlockIter<'a> {
    iter: CharDataTableIter<'a, &'static str>,
}

impl<'a> BlockIter<'a> {
    /// Create a new Block Iterator.
    pub fn new() -> BlockIter<'a> {
        BlockIter {
            iter: data::BLOCKS.iter(),
        }
    }
}

impl<'a> Default for BlockIter<'a> {
    fn default() -> Self {
        BlockIter::new()
    }
}

impl<'a> Iterator for BlockIter<'a> {
    type Item = Block;

    fn next(&mut self) -> Option<Block> {
        match self.iter.next() {
            None => None,
            Some((range, name)) => Some(Block {
                range,
                name,
                _priv: (),
            }),
        }
    }
}

mod data {
    use unic_char_property::tables::CharDataTable;
    pub const BLOCKS: CharDataTable<&str> = include!("../tables/blocks.rsv");
}