enhanced_magic_string/utils/
char_string.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
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
165
166
use std::fmt::{Debug, Display};

#[derive(Clone, PartialEq, Eq)]
pub struct CharString {
  chars: Vec<char>,
}

impl CharString {
  pub fn new(str: &str) -> Self {
    Self {
      chars: str.chars().collect(),
    }
  }

  pub fn get(&self, index: usize) -> Option<&char> {
    self.chars.get(index)
  }

  pub fn get_mut(&mut self, index: usize) -> Option<&mut char> {
    self.chars.get_mut(index)
  }

  pub fn slice(&self, start: usize, end: usize) -> Self {
    Self {
      chars: self.chars[start..end].to_vec(),
    }
  }

  pub fn split(&self, separator: char) -> Vec<Self> {
    let mut result = vec![];
    let mut start = 0;

    for (index, char) in self.chars.iter().enumerate() {
      if *char == separator {
        result.push(self.slice(start, index));
        start = index + 1;
      }
    }

    result.push(self.slice(start, self.len()));

    result
  }

  pub fn len(&self) -> usize {
    self.chars.len()
  }

  pub fn is_empty(&self) -> bool {
    self.chars.is_empty()
  }

  pub fn insert(&mut self, index: usize, char: char) {
    self.chars.insert(index, char);
  }

  pub fn remove(&mut self, index: usize) -> Option<char> {
    if index >= self.chars.len() {
      return None;
    }

    Some(self.chars.remove(index))
  }

  pub fn append_str(&mut self, other: &str) {
    self.chars.extend(other.chars());
  }

  pub fn append(&mut self, other: &CharString) {
    self.chars.extend(other.chars.iter());
  }
}

impl From<&str> for CharString {
  fn from(str: &str) -> Self {
    Self::new(str)
  }
}

impl From<String> for CharString {
  fn from(str: String) -> Self {
    Self::new(&str)
  }
}

impl From<&String> for CharString {
  fn from(str: &String) -> Self {
    Self::new(str)
  }
}

impl From<char> for CharString {
  fn from(char: char) -> Self {
    Self { chars: vec![char] }
  }
}

impl Display for CharString {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", self.chars.iter().collect::<String>())
  }
}

impl Debug for CharString {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(f, "{:?}", self.chars.iter().collect::<String>())
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_char_string() {
    let str = CharString::new("hello world");
    assert_eq!(str.len(), 11);
    assert_eq!(str.get(0), Some(&'h'));
    assert_eq!(str.get(1), Some(&'e'));
    assert_eq!(str.get(2), Some(&'l'));
    assert_eq!(str.get(3), Some(&'l'));
    assert_eq!(str.get(4), Some(&'o'));
    assert_eq!(str.get(5), Some(&' '));
    assert_eq!(str.get(6), Some(&'w'));
    assert_eq!(str.get(7), Some(&'o'));
    assert_eq!(str.get(8), Some(&'r'));
    assert_eq!(str.get(9), Some(&'l'));
    assert_eq!(str.get(10), Some(&'d'));
    assert_eq!(str.get(11), None);

    let mut str = CharString::new("hello world");
    str.insert(5, '!');
    assert_eq!(str.to_string(), "hello! world");

    let mut str = CharString::new("hello world");
    str.remove(5);
    assert_eq!(str.to_string(), "helloworld");

    let str = CharString::new("hello world");
    assert_eq!(str.slice(0, 5).to_string(), "hello");
    assert_eq!(str.slice(6, 11).to_string(), "world");
    assert_eq!(str.slice(0, 11).to_string(), "hello world");
  }

  #[test]
  fn test_split() {
    let str = CharString::new("hello world");
    let result = str.split(' ');
    assert_eq!(result.len(), 2);
    assert_eq!(result[0].to_string(), "hello");
    assert_eq!(result[1].to_string(), "world");

    let str = CharString::new("hello world");
    let result = str.split('o');
    assert_eq!(result.len(), 3);
    assert_eq!(result[0].to_string(), "hell");
    assert_eq!(result[1].to_string(), " w");
    assert_eq!(result[2].to_string(), "rld");

    let str = CharString::new("\n");
    let result = str.split('\n');
    assert_eq!(result.len(), 2);
    assert_eq!(result[0].to_string(), "");
    assert_eq!(result[1].to_string(), "");
  }
}