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
use serde::{Deserialize, Serialize};

/// An object describing a part of a text
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct TextBlock {
    /// Offset in UTF-16 code units to the start of the entity
    pub offset: usize,
    /// Length of the entity in UTF-16 code units
    pub length: usize,
}

impl TextBlock {
    /// Gets the part of the text described by the `TextBlock`
    pub fn get_text(&self, text: &str) -> String {
        let mut res: Vec<u16> = Vec::with_capacity(self.length);

        for (i, point) in text.encode_utf16().enumerate() {
            if (self.offset + self.length) <= i {
                break;
            }
            if self.offset <= i {
                res.push(point)
            }
        }

        String::from_utf16_lossy(res.as_slice())
    }
}