unic_ucd_block/block.rs
1// Copyright 2012-2015 The Rust Project Developers.
2// Copyright 2017 The UNIC Project Developers.
3//
4// See the COPYRIGHT file at the top-level directory of this distribution.
5//
6// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9// option. This file may not be copied, modified, or distributed
10// except according to those terms.
11
12use unic_char_property::{tables::CharDataTableIter, PartialCharProperty};
13use unic_char_range::CharRange;
14
15/// A Unicode Block.
16///
17/// Blocks are contiguous range of code points, uniquely named, and have no overlaps.
18///
19/// All Assigned characters have a Block property value, but Reserved characters may or may not
20/// have a Block.
21#[derive(Clone, Copy, Debug)]
22pub struct Block {
23 /// The Character range of the Block.
24 pub range: CharRange,
25
26 /// The unique name of the Block.
27 pub name: &'static str,
28
29 // Private field to keep struct expandable.
30 _priv: (),
31}
32
33impl Block {
34 /// Find the character `Block` property value.
35 pub fn of(ch: char) -> Option<Block> {
36 match data::BLOCKS.find_with_range(ch) {
37 None => None,
38 Some((range, name)) => Some(Block {
39 range,
40 name,
41 _priv: (),
42 }),
43 }
44 }
45}
46
47impl PartialCharProperty for Block {
48 fn of(ch: char) -> Option<Self> {
49 Self::of(ch)
50 }
51}
52
53/// Iterator for all assigned Unicode Blocks, except:
54/// - U+D800..U+DB7F, High Surrogates
55/// - U+DB80..U+DBFF, High Private Use Surrogates
56/// - U+DC00..U+DFFF, Low Surrogates
57#[derive(Debug)]
58pub struct BlockIter<'a> {
59 iter: CharDataTableIter<'a, &'static str>,
60}
61
62impl<'a> BlockIter<'a> {
63 /// Create a new Block Iterator.
64 pub fn new() -> BlockIter<'a> {
65 BlockIter {
66 iter: data::BLOCKS.iter(),
67 }
68 }
69}
70
71impl<'a> Default for BlockIter<'a> {
72 fn default() -> Self {
73 BlockIter::new()
74 }
75}
76
77impl<'a> Iterator for BlockIter<'a> {
78 type Item = Block;
79
80 fn next(&mut self) -> Option<Block> {
81 match self.iter.next() {
82 None => None,
83 Some((range, name)) => Some(Block {
84 range,
85 name,
86 _priv: (),
87 }),
88 }
89 }
90}
91
92mod data {
93 use unic_char_property::tables::CharDataTable;
94 pub const BLOCKS: CharDataTable<&str> = include!("../tables/blocks.rsv");
95}