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
use super::{DynamicString, PatternFinder, MIN_SLICE_LENGTH};
use std::cmp;
impl DynamicString {
/// Extracts a section of a string and returns it as a new string, without modifying
/// the original string.
pub fn slice(&self, start: usize, length: usize) -> Self {
if length == 0 {
return DynamicString::empty();
}
let len = self.len();
if start >= len {
return DynamicString::empty();
}
// start < len
// end = start + length
// max(end) = len
// => length = end - start
// => max(length) = max(end) - min(start)
// = len - start
// length = min(len - start, length)
let length = cmp::min(len - start, length);
let ret = DynamicString::SlicedString {
root: Box::new(self.clone()),
start,
length,
};
if length < MIN_SLICE_LENGTH {
ret.flatten()
} else {
ret
}
}
/// Concatenate the current string with another string, returns the result.
/// ```
/// use dynstr::DynamicString;
/// let str = DynamicString::new("hello");
/// assert_eq!(str.append(" world"), DynamicString::new("hello world"));
/// ```
pub fn append<T: Into<DynamicString>>(&self, other: T) -> Self {
let other = other.into();
match (self, &other) {
(DynamicString::Empty, s) | (s, DynamicString::Empty) => return s.clone(),
_ => {}
}
let ret = DynamicString::ConsString {
first: Box::new(self.clone()),
second: Box::new(other.clone()),
};
if ret.len() < MIN_SLICE_LENGTH {
ret.flatten()
} else {
ret
}
}
/// Return the index of the first occurrence of the specified value in the current string.
/// ```
/// use dynstr::DynamicString;
/// let str = DynamicString::new("Hello world");
/// assert_eq!(str.index_of("world"), Some(6));
/// assert_eq!(str.index_of("world!"), None);
/// ```
pub fn index_of<T: Into<DynamicString>>(&self, pattern: T) -> Option<usize> {
PatternFinder::new(self.clone(), pattern.into()).next()
}
/// Divides a String into an ordered list of substrings, puts these substrings into a vector,
/// and returns the vector. The division is done by searching for a pattern; where the pattern
/// is provided as the first parameter in the method's call.
/// This method tries to follow the JavaScript's String.split method in edge cases.
/// ```
/// use dynstr::DynamicString;
/// let str = DynamicString::new("Hello world");
/// assert_eq!(DynamicString::new("Jack,Joe,John").split(",", None), vec!["Jack", "Joe", "John"]);
/// assert_eq!(DynamicString::new("Jack,Joe,John").split(",", Some(1)), vec!["Jack"]);
/// // edge cases:
/// assert!(DynamicString::new("").split("", None).is_empty());
/// assert_eq!(DynamicString::new("ABC").split("", None), vec!["A", "B", "C"]);
/// assert_eq!(DynamicString::new("").split("ABC", None), vec![""]);
/// ```
pub fn split<T: Into<DynamicString>>(
&self,
separator: T,
limit: Option<usize>,
) -> Vec<DynamicString> {
if limit == Some(0) {
return Vec::with_capacity(0);
}
let separator = separator.into();
let sep_len = separator.len();
let patterns = PatternFinder::new(self.clone(), separator);
let mut result = Vec::new();
let mut last_index = 0;
for index in patterns {
if !(sep_len == 0 && last_index == 0 && index == 0) {
result.push(self.slice(last_index, index - last_index));
}
last_index = index + sep_len;
match limit {
Some(n) if n == result.len() => return result,
_ => {}
}
}
if last_index < self.len() {
result.push(self.slice(last_index, self.len() - last_index));
}
result
}
/// Determines whether a string begins with the characters of a specified string, returning
/// true or false as appropriate.
pub fn starts_with<T: Into<DynamicString>>(&self, other: T) -> bool {
let o: DynamicString = other.into();
if o.len() > self.len() {
false
} else {
self.iter().take(o.len()).eq(o.iter())
}
}
}
impl<T: Into<DynamicString>> std::ops::Add<T> for DynamicString {
type Output = DynamicString;
fn add(self, rhs: T) -> Self::Output {
self.append(rhs.into())
}
}