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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::cell::{Cell, Ref, RefCell};
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use std::rc::Rc;

#[derive(PartialOrd, PartialEq, Ord, Eq)]
pub struct StringListWrapper(RefCell<String>);

impl Hash for StringListWrapper {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.borrow().hash(state)
    }
}

// cursor is ignored if the double_quotes flag is set to atom
#[derive(Clone)]
pub struct StringList {
    body: Rc<StringListWrapper>,
    cursor: usize, // use this to generate a chars() iterator on the fly,
                   // and skip over the first cursor chars.
    expandable: Rc<Cell<bool>>
}

impl Hash for StringList {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        (self.borrow().as_str(), self.cursor, self.expandable.get()).hash(state);
    }
}

impl PartialOrd for StringList {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.body.cmp(&other.body))
    }
}

impl Ord for StringList {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        if self.expandable.get() && !self.expandable.get() {
            Ordering::Greater
        } else if !self.expandable.get() && self.expandable.get() {
            Ordering::Less
        } else {
            self.borrow()[self.cursor ..].cmp(&other.borrow()[other.cursor ..])
        }
    }
}

impl PartialEq for StringList {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.body, &other.body)
    }
}

impl Eq for StringList {}

impl StringList {
    #[inline]
    pub fn new(s: String, expandable: bool) -> Self {
        let body = Rc::new(StringListWrapper(RefCell::new(s)));

        StringList {
            cursor: 0,
            body,
            expandable: Rc::new(Cell::new(expandable))
        }
    }
    
    #[inline]
    pub fn is_expandable(&self) -> bool {
        self.expandable.get()
    }

    #[inline]
    pub fn set_expandable(&self, value: bool) {
        self.expandable.set(value);
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.borrow().len() - self.cursor
    }

    #[inline]
    pub fn truncate(&mut self, len: usize) {
        self.body.0.borrow_mut().truncate(len);
        self.expandable.set(true);
    }

    #[inline]
    pub fn starts_with(&self, pat: &StringList) -> bool {
        self.borrow()[self.cursor ..].starts_with(&pat.borrow()[pat.cursor ..])
    }

    /* Called under the assumption that self is a prefix of suffix,
       and we want to copy the rest of suffix into self.
     */
    #[inline]
    pub fn append_suffix(&mut self, suffix: &StringList) {
        if self.expandable.get() {
            let cursor = suffix.cursor + self.len();
            self.body.0.borrow_mut().extend(suffix.borrow()[cursor ..].chars());
        }
    }

    #[inline]
    pub fn push_char(&mut self, c: char) -> Self {
        if self.expandable.get() {
            self.body.0.borrow_mut().push(c);

            let mut new_string_list = self.clone();
            new_string_list.cursor += c.len_utf8();

            new_string_list
        } else {
            self.clone()
        }
    }

    #[inline]
    pub fn append(&mut self, s: &StringList) {
        self.body.0.borrow_mut().extend(s.borrow()[s.cursor ..].chars());
        self.expandable.set(s.expandable.get());
    }

    #[inline]
    pub fn cursor(&self) -> usize {
        self.cursor
    }

    #[inline]
    pub fn char_span(&self, index: usize) -> String {
        self.borrow()[self.cursor + index ..].chars().collect()
    }

    #[inline]
    pub fn head(&self) -> Option<char> {
        self.borrow()[self.cursor ..].chars().next()
    }

    #[inline]
    pub fn tail(&self) -> Self {
        let mut new_string_list = self.clone();

        if let Some(c) = self.head() {
            new_string_list.cursor += c.len_utf8();
        }

        new_string_list
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.borrow().len() == self.cursor
    }

    #[inline]
    pub fn borrow(&self) -> Ref<String> {
        self.body.0.borrow()
    }
}