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
use std::fmt;
#[derive(Clone)]
pub struct StringFactory{
pub(crate) string: String
}
impl fmt::Display for StringFactory {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.string)
}
}
impl StringFactory{
pub fn new() -> StringFactory{
StringFactory {string: String::new()}
}
pub fn from_str(val: &str) -> StringFactory{
StringFactory {string: val.into()}
}
pub fn from_string(string: String) -> StringFactory{
StringFactory {string: string}
}
pub fn append<T: fmt::Display>(&mut self, val: T){
self.string = format!("{}{}", self.string, val);
}
pub fn append_debug<T: fmt::Debug>(&mut self, val: T){
self.string = format!("{}{:?}", self.string, val);
}
pub fn append_front<T: fmt::Display>(&mut self, val: T){
self.string = format!("{}{}", val, self.string);
}
pub fn append_debug_front<T: fmt::Debug>(&mut self, val: T){
self.string = format!("{:?}{}", val, self.string);
}
pub fn to_char_vec(&self) -> Vec<char>{
let mut vec = Vec::new();
for c in self.string.chars(){
vec.push(c);
}
vec
}
pub fn index_of(&self, val: char) -> Option<usize>{
for (index, c) in self.string.chars().enumerate() {
if c == val{
return Some(index);
}
}
None
}
pub fn indexes_of(&self, val: char) -> Vec<usize>{
let mut vec = Vec::new();
for (index, c) in self.string.chars().enumerate() {
if c == val{
vec.push(index);
}
}
vec
}
pub fn replace(&mut self, from: char, to: char){
let mut chars = self.to_char_vec();
let indexes = self.indexes_of(from);
for index in indexes{
chars[index] = to;
}
self.string = chars.iter().collect();
}
}
#[cfg(test)]
mod string_factory_tests {
use super::StringFactory;
#[test]
fn test_creation() {
assert_eq!("".to_string(), StringFactory::new().string);
assert_eq!("Test".to_string(), StringFactory::from_str("Test").string);
assert_eq!("Test".to_string(),
StringFactory::from_string("Test".to_string()).string);
}
#[test]
fn test_append() {
let mut val = StringFactory::new();
val.append(5);
assert_eq!("5".to_string(), val.string);
val.append_front(20);
assert_eq!("205".to_string(), val.string);
let mut val = StringFactory::new();
val.append_debug(vec![0]);
assert_eq!(format!("{:?}", vec![0]), val.string);
val.append_debug_front(vec![5]);
assert_eq!(format!("{:?}{:?}", vec![5], vec![0]), val.string);
}
#[test]
fn test_index(){
assert!(StringFactory::from_str("ABC").index_of('B') == Some(1));
assert!(StringFactory::from_str("ABC").index_of('b') == None);
assert!(StringFactory::from_str("Hello").index_of('o') == Some(4));
assert!(StringFactory::from_str("bob").indexes_of('b') == vec![0, 2]);
assert!(StringFactory::from_str("Tatoo").indexes_of('o') == vec![3, 4]);
assert!(StringFactory::from_str("Test").indexes_of('e') != vec![0]);
}
#[test]
fn test_replace(){
let mut fac = StringFactory::from_str("Bob");
fac.replace('o', '0');
println!("{}", fac.string);
assert!(fac.string == "B0b".to_string());
let mut fac = StringFactory::from_str("Book");
fac.replace('o', '0');
assert!(fac.string == "B00k");
}
}