Skip to main content

ib_romaji/
input.rs

1/**
2Unfortunately, Japanese is highly contextual, surrounding charcaters
3are needed for accurate romanization.
4This struct can keep surrounding charcaters by storing the entire haystack
5and the start offset.
6*/
7#[derive(Clone, Copy, Debug)]
8pub struct Input<'h> {
9    haystack: &'h str,
10    start: usize,
11}
12
13impl<'h> Input<'h> {
14    #[inline]
15    pub fn new<H: ?Sized + AsRef<str>>(haystack: &'h H, start: usize) -> Self {
16        Self {
17            haystack: haystack.as_ref(),
18            start,
19        }
20    }
21
22    #[inline]
23    pub fn haystack(&self) -> &'h str {
24        self.haystack
25    }
26
27    #[inline]
28    pub fn start(&self) -> usize {
29        self.start
30    }
31
32    #[inline]
33    pub fn is_empty(&self) -> bool {
34        self.as_ref().is_empty()
35    }
36}
37
38impl<'h, H: ?Sized + AsRef<str>> From<&'h H> for Input<'h> {
39    fn from(haystack: &'h H) -> Self {
40        Self::new(haystack, 0)
41    }
42}
43
44impl<'h> AsRef<str> for Input<'h> {
45    fn as_ref(&self) -> &'h str {
46        &self.haystack[self.start..]
47    }
48}