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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
use std::borrow::{Borrow, Cow};
use std::fmt::{self, Debug, Formatter, Write};
use std::hash::{Hash, Hasher};
use std::ops::{Add, AddAssign, Deref};
use unicode_segmentation::UnicodeSegmentation;
use super::{castable, dict, Array, Dict, Value};
use crate::diag::StrResult;
use crate::geom::GenAlign;
use crate::util::EcoString;
/// Create a new [`Str`] from a format string.
#[macro_export]
#[doc(hidden)]
macro_rules! __format_str {
($($tts:tt)*) => {{
$crate::model::Str::from(format_eco!($($tts)*))
}};
}
#[doc(inline)]
pub use crate::__format_str as format_str;
/// An immutable reference counted string.
#[derive(Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Str(EcoString);
impl Str {
/// Create a new, empty string.
pub fn new() -> Self {
Self(EcoString::new())
}
/// The length of the string in bytes.
pub fn len(&self) -> i64 {
self.0.len() as i64
}
/// A string slice containing the entire string.
pub fn as_str(&self) -> &str {
self
}
/// The codepoints the string consists of.
pub fn codepoints(&self) -> Array {
self.as_str().chars().map(|c| Value::Str(c.into())).collect()
}
/// The grapheme clusters the string consists of.
pub fn graphemes(&self) -> Array {
self.as_str().graphemes(true).map(|s| Value::Str(s.into())).collect()
}
/// Extract a contigous substring.
pub fn slice(&self, start: i64, end: Option<i64>) -> StrResult<Self> {
let len = self.len();
let start = self
.locate(start)
.filter(|&start| start <= self.0.len())
.ok_or_else(|| out_of_bounds(start, len))?;
let end = end.unwrap_or(self.len());
let end = self
.locate(end)
.filter(|&end| end <= self.0.len())
.ok_or_else(|| out_of_bounds(end, len))?
.max(start);
Ok(self.0[start..end].into())
}
/// Resolve an index.
fn locate(&self, index: i64) -> Option<usize> {
usize::try_from(if index >= 0 { index } else { self.len().checked_add(index)? })
.ok()
}
/// Whether the given pattern exists in this string.
pub fn contains(&self, pattern: Pattern) -> bool {
match pattern {
Pattern::Str(pat) => self.0.contains(pat.as_str()),
Pattern::Regex(re) => re.is_match(self),
}
}
/// Whether this string begins with the given pattern.
pub fn starts_with(&self, pattern: Pattern) -> bool {
match pattern {
Pattern::Str(pat) => self.0.starts_with(pat.as_str()),
Pattern::Regex(re) => re.find(self).map_or(false, |m| m.start() == 0),
}
}
/// Whether this string ends with the given pattern.
pub fn ends_with(&self, pattern: Pattern) -> bool {
match pattern {
Pattern::Str(pat) => self.0.ends_with(pat.as_str()),
Pattern::Regex(re) => {
re.find_iter(self).last().map_or(false, |m| m.end() == self.0.len())
}
}
}
/// The text of the pattern's first match in this string.
pub fn find(&self, pattern: Pattern) -> Option<Self> {
match pattern {
Pattern::Str(pat) => self.0.contains(pat.as_str()).then(|| pat),
Pattern::Regex(re) => re.find(self).map(|m| m.as_str().into()),
}
}
/// The position of the pattern's first match in this string.
pub fn position(&self, pattern: Pattern) -> Option<i64> {
match pattern {
Pattern::Str(pat) => self.0.find(pat.as_str()).map(|i| i as i64),
Pattern::Regex(re) => re.find(self).map(|m| m.start() as i64),
}
}
/// The start and, text and capture groups (if any) of the first match of
/// the pattern in this string.
pub fn match_(&self, pattern: Pattern) -> Option<Dict> {
match pattern {
Pattern::Str(pat) => {
self.0.match_indices(pat.as_str()).next().map(match_to_dict)
}
Pattern::Regex(re) => re.captures(self).map(captures_to_dict),
}
}
/// The start, end, text and capture groups (if any) of all matches of the
/// pattern in this string.
pub fn matches(&self, pattern: Pattern) -> Array {
match pattern {
Pattern::Str(pat) => self
.0
.match_indices(pat.as_str())
.map(match_to_dict)
.map(Value::Dict)
.collect(),
Pattern::Regex(re) => re
.captures_iter(self)
.map(captures_to_dict)
.map(Value::Dict)
.collect(),
}
}
/// Split this string at whitespace or a specific pattern.
pub fn split(&self, pattern: Option<Pattern>) -> Array {
let s = self.as_str();
match pattern {
None => s.split_whitespace().map(|v| Value::Str(v.into())).collect(),
Some(Pattern::Str(pat)) => {
s.split(pat.as_str()).map(|v| Value::Str(v.into())).collect()
}
Some(Pattern::Regex(re)) => {
re.split(s).map(|v| Value::Str(v.into())).collect()
}
}
}
/// Trim either whitespace or the given pattern at both or just one side of
/// the string. If `repeat` is true, the pattern is trimmed repeatedly
/// instead of just once. Repeat must only be given in combination with a
/// pattern.
pub fn trim(
&self,
pattern: Option<Pattern>,
at: Option<StrSide>,
repeat: bool,
) -> Self {
let mut start = matches!(at, Some(StrSide::Start) | None);
let end = matches!(at, Some(StrSide::End) | None);
let trimmed = match pattern {
None => match at {
None => self.0.trim(),
Some(StrSide::Start) => self.0.trim_start(),
Some(StrSide::End) => self.0.trim_end(),
},
Some(Pattern::Str(pat)) => {
let pat = pat.as_str();
let mut s = self.as_str();
if repeat {
if start {
s = s.trim_start_matches(pat);
}
if end {
s = s.trim_end_matches(pat);
}
} else {
if start {
s = s.strip_prefix(pat).unwrap_or(s);
}
if end {
s = s.strip_suffix(pat).unwrap_or(s);
}
}
s
}
Some(Pattern::Regex(re)) => {
let s = self.as_str();
let mut last = 0;
let mut range = 0..s.len();
for m in re.find_iter(s) {
// Does this match follow directly after the last one?
let consecutive = last == m.start();
// As long as we're consecutive and still trimming at the
// start, trim.
start &= consecutive;
if start {
range.start = m.end();
start &= repeat;
}
// Reset end trim if we aren't consecutive anymore or aren't
// repeating.
if end && (!consecutive || !repeat) {
range.end = m.start();
}
last = m.end();
}
// Is the last match directly at the end?
if last < s.len() {
range.end = s.len();
}
&s[range.start..range.start.max(range.end)]
}
};
trimmed.into()
}
/// Replace at most `count` occurances of the given pattern with a
/// replacement string (beginning from the start).
pub fn replace(&self, pattern: Pattern, with: Self, count: Option<usize>) -> Self {
match pattern {
Pattern::Str(pat) => match count {
Some(n) => self.0.replacen(pat.as_str(), &with, n).into(),
None => self.0.replace(pat.as_str(), &with).into(),
},
Pattern::Regex(re) => match count {
Some(n) => re.replacen(self, n, with.as_str()).into(),
None => re.replace(self, with.as_str()).into(),
},
}
}
/// Repeat the string a number of times.
pub fn repeat(&self, n: i64) -> StrResult<Self> {
let n = usize::try_from(n)
.ok()
.and_then(|n| self.0.len().checked_mul(n).map(|_| n))
.ok_or_else(|| format!("cannot repeat this string {} times", n))?;
Ok(Self(self.0.repeat(n)))
}
}
/// The out of bounds access error message.
#[cold]
fn out_of_bounds(index: i64, len: i64) -> String {
format!("string index out of bounds (index: {}, len: {})", index, len)
}
/// Convert an item of std's `match_indices` to a dictionary.
fn match_to_dict((start, text): (usize, &str)) -> Dict {
dict! {
"start" => Value::Int(start as i64),
"end" => Value::Int((start + text.len()) as i64),
"text" => Value::Str(text.into()),
"captures" => Value::Array(Array::new()),
}
}
/// Convert regex captures to a dictionary.
fn captures_to_dict(cap: regex::Captures) -> Dict {
let m = cap.get(0).expect("missing first match");
dict! {
"start" => Value::Int(m.start() as i64),
"end" => Value::Int(m.end() as i64),
"text" => Value::Str(m.as_str().into()),
"captures" => Value::Array(
cap.iter()
.skip(1)
.map(|opt| opt.map_or(Value::None, |m| m.as_str().into()))
.collect(),
),
}
}
impl Deref for Str {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
impl Debug for Str {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_char('"')?;
for c in self.chars() {
match c {
'\\' => f.write_str(r"\\")?,
'"' => f.write_str(r#"\""#)?,
'\n' => f.write_str(r"\n")?,
'\r' => f.write_str(r"\r")?,
'\t' => f.write_str(r"\t")?,
_ => f.write_char(c)?,
}
}
f.write_char('"')
}
}
impl Add for Str {
type Output = Self;
fn add(mut self, rhs: Self) -> Self::Output {
self += rhs;
self
}
}
impl AddAssign for Str {
fn add_assign(&mut self, rhs: Self) {
self.0.push_str(rhs.as_str());
}
}
impl AsRef<str> for Str {
fn as_ref(&self) -> &str {
self
}
}
impl Borrow<str> for Str {
fn borrow(&self) -> &str {
self
}
}
impl From<char> for Str {
fn from(c: char) -> Self {
Self(c.into())
}
}
impl From<&str> for Str {
fn from(s: &str) -> Self {
Self(s.into())
}
}
impl From<EcoString> for Str {
fn from(s: EcoString) -> Self {
Self(s)
}
}
impl From<String> for Str {
fn from(s: String) -> Self {
Self(s.into())
}
}
impl From<Cow<'_, str>> for Str {
fn from(s: Cow<str>) -> Self {
Self(s.into())
}
}
impl FromIterator<char> for Str {
fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}
impl From<Str> for EcoString {
fn from(str: Str) -> Self {
str.0
}
}
impl From<Str> for String {
fn from(s: Str) -> Self {
s.0.into()
}
}
/// A regular expression.
#[derive(Clone)]
pub struct Regex(regex::Regex);
impl Regex {
/// Create a new regular expression.
pub fn new(re: &str) -> StrResult<Self> {
regex::Regex::new(re).map(Self).map_err(|err| err.to_string())
}
}
impl Deref for Regex {
type Target = regex::Regex;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Debug for Regex {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "regex({:?})", self.0.as_str())
}
}
impl PartialEq for Regex {
fn eq(&self, other: &Self) -> bool {
self.0.as_str() == other.0.as_str()
}
}
impl Hash for Regex {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.as_str().hash(state);
}
}
/// A pattern which can be searched for in a string.
#[derive(Debug, Clone)]
pub enum Pattern {
/// Just a string.
Str(Str),
/// A regular expression.
Regex(Regex),
}
castable! {
Pattern,
Expected: "string or regular expression",
Value::Str(text) => Self::Str(text),
@regex: Regex => Self::Regex(regex.clone()),
}
/// A side of a string.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum StrSide {
/// The logical start of the string, may be left or right depending on the
/// language.
Start,
/// The logical end of the string.
End,
}
castable! {
StrSide,
Expected: "start or end",
@align: GenAlign => match align {
GenAlign::Start => Self::Start,
GenAlign::End => Self::End,
_ => Err("expected either `start` or `end`")?,
},
}
|