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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//
// Copyright 2024 Formata, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
use std::ops::{Deref, DerefMut};
use crate::{lang::SError, Library, SDoc, SNum, SVal};
/// String library.
#[derive(Default, Debug)]
pub struct StringLibrary;
impl StringLibrary {
/// Call string operation.
pub fn operate(&self, pid: &str, doc: &mut SDoc, name: &str, val: &mut String, parameters: &mut Vec<SVal>) -> Result<SVal, SError> {
match name {
// Get the length of this string.
"len" => {
Ok(SVal::Number(SNum::I64(val.len() as i64)))
},
// Get a char from this string at a specific index.
"at" => {
if parameters.len() < 1 {
return Err(SError::string(pid, &doc, "at", "invalid arguments - index not found"));
}
let index = parameters.pop().unwrap().unbox();
match index {
SVal::Number(index) => {
let index = index.int() as usize;
if index >= val.len() {
return Ok(SVal::Null);
}
let char = val.as_bytes()[index] as char;
Ok(SVal::from(char))
},
SVal::Boxed(bval) => {
let bval = bval.lock().unwrap();
let bval = bval.deref();
match bval {
SVal::Number(index) => {
let index = index.int() as usize;
if index >= val.len() {
return Ok(SVal::Null);
}
let char = val.as_bytes()[index] as char;
Ok(SVal::from(char))
},
_ => {
Err(SError::string(pid, &doc, "at", "invalid arguments - index must be numerical"))
}
}
},
_ => {
Err(SError::string(pid, &doc, "at", "invalid arguments - index must be numerical"))
}
}
},
// Get the first char in this string.
"first" => {
if val.len() < 1 {
return Ok(SVal::Null);
}
let char = val.as_bytes()[0] as char;
Ok(SVal::from(char))
},
// Get the last char in this string.
"last" => {
if val.len() < 1 {
return Ok(SVal::Null);
}
let char = val.as_bytes()[val.len() - 1] as char;
Ok(SVal::from(char))
},
// Test whether the string starts with a value.
"startsWith" => {
if parameters.len() == 1 {
let second = parameters[0].to_string();
return Ok(SVal::Bool(val.starts_with(&second)));
}
Err(SError::string(pid, &doc, "startsWith", "invalid arguments - string value to test startsWith not found"))
},
// Test whether the string ends with a value.
"endsWith" => {
if parameters.len() == 1 {
let second = parameters[0].to_string();
return Ok(SVal::Bool(val.ends_with(&second)));
}
Err(SError::string(pid, &doc, "endsWith", "invalid arguments - string value to test endsWith not found"))
},
// Push values to this string.
"push" => {
for param in parameters.drain(..) {
val.push_str(¶m.to_string());
}
Ok(SVal::Void)
},
// Does this string contain a substring?
"contains" => {
if parameters.len() < 1 {
return Err(SError::string(pid, &doc, "contains", "invalid arguments - contains string not found"));
}
Ok(SVal::Bool(val.contains(¶meters[0].to_string())))
},
// Index of a substring in this string. -1 if not found, index of first char otherwise.
"indexOf" => {
if parameters.len() < 1 {
return Err(SError::string(pid, &doc, "indexOf", "invalid arguments - substring to search for not found"));
}
if let Some(index) = val.find(¶meters[0].to_string()) {
return Ok(SVal::Number(SNum::I64(index as i64)));
}
Ok(SVal::Number(SNum::I64(-1 as i64)))
},
// Replace all instances of a substring in this string.
"replace" => {
if parameters.len() < 2 {
return Err(SError::string(pid, &doc, "replace", "invalid arguments - string replace takes a from string value and a to string value"));
}
let from = parameters[0].to_string();
let to = parameters[1].to_string();
Ok(SVal::String(val.replace(&from, &to)))
},
// Split this string at every occurrence of a substring.
"split" => {
if parameters.len() < 1 {
return Err(SError::string(pid, &doc, "split", "invalid arguments - substring to split with not found"));
}
let vals = val.split(¶meters[0].to_string()).collect::<Vec<&str>>();
let mut array = Vec::new();
for v in vals { array.push(SVal::from(v)); }
Ok(SVal::Array(array))
},
// Transform this string to all uppercase.
"toUpper" => {
Ok(SVal::String(val.to_uppercase()))
},
// Transform this string to all lowercase.
"toLower" => {
Ok(SVal::String(val.to_lowercase()))
},
// Trim the whitespace off of the front and end of this string.
"trim" => {
Ok(SVal::String(val.trim().to_string()))
},
// Trim the whitespace off of the front of this string.
"trimStart" => {
Ok(SVal::String(val.trim_start().to_string()))
},
// Trim the whitespace off of the end of this string.
"trimEnd" => {
Ok(SVal::String(val.trim_end().to_string()))
},
// Get a substring of this string.
"substring" => {
if parameters.len() < 1 {
return Err(SError::string(pid, &doc, "substring", "invalid arguments - start (and optional end) not found"));
}
let start;
match ¶meters[0] {
SVal::Number(num) => {
start = num.int() as usize;
},
_ => {
return Err(SError::string(pid, &doc, "substring", "non-numerical ranges are not supported"));
}
}
if parameters.len() > 1 {
let end;
match ¶meters[1] {
SVal::Number(num) => {
end = num.int() as usize;
},
_ => {
return Err(SError::string(pid, &doc, "substring", "non-numerical ranges are not supported"));
}
}
if let Some(slice) = val.get(start..end) {
return Ok(SVal::String(slice.to_string()));
}
return Err(SError::string(pid, &doc, "substring", "cannot get substring at the requested range"));
}
if let Some(slice) = val.get(start..) {
Ok(SVal::String(slice.to_string()))
} else {
Err(SError::string(pid, &doc, "substring", "cannot get substring at the requested range"))
}
},
_ => {
Err(SError::string(pid, &doc, "NotFound", &format!("{} is not a function in the String Library", name)))
}
}
}
}
impl Library for StringLibrary {
/// Scope.
fn scope(&self) -> String {
"String".to_string()
}
/// Call into the String library.
fn call(&self, pid: &str, doc: &mut SDoc, name: &str, parameters: &mut Vec<SVal>) -> Result<SVal, SError> {
if parameters.len() > 0 {
match name {
"toString" => {
return Ok(SVal::String(parameters[0].print(doc)));
},
"or" => {
for param in parameters.drain(..) {
if !param.is_empty() {
return Ok(param);
}
}
return Ok(SVal::Null);
},
_ => {}
}
let mut params;
if parameters.len() > 1 {
params = parameters.drain(1..).collect();
} else {
params = Vec::new();
}
match &mut parameters[0] {
SVal::String(val) => {
return self.operate(pid, doc, name, val, &mut params);
},
SVal::Boxed(val) => {
let mut val = val.lock().unwrap();
let val = val.deref_mut();
match val {
SVal::String(val) => {
return self.operate(pid, doc, name, val, &mut params);
},
_ => {
return Err(SError::string(pid, &doc, "InvalidArgument", "string argument not found"));
}
}
},
_ => {
return Err(SError::string(pid, &doc, "InvalidArgument", "string argument not found"));
}
}
} else {
return Err(SError::string(pid, &doc, "InvalidArgument", "string argument not found"));
}
}
}