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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
pub struct Tokenization<'a> {
pub tokens: Vec<&'a str>,
pub trailing_space: bool,
}
/// Tokenizers pre-process the string into a vector of &str tokens for a parser. These tokens are
/// essentially a way to split apart a line into command and arguments. Effectively a tokenizer,
/// but it doesn't necessarily emit a variety of tokens, but serves a purpose similar to a
/// tokenizer, or I suppose, at least a scanner?
pub trait Tokenizer {
// Tokenize returns a vector of tokens (&str), and a bool to indicate if there was a trailing
// space.
fn tokenize<'a>(&self, line: &'a str) -> Tokenization<'a>;
}
/// DefaultTokenizer tokenizes an input string into tokens based on some default, basic rules.
///
/// Handles things like splitting by space, acknowledging quotation marks, etc.
pub struct DefaultTokenizer {
quotations: Vec<char>,
}
#[derive(Debug, PartialEq)]
/// Describes the position of a quotation mark.
///
/// Quotation marks are generally either `"` or `'`, but can be any character.
struct QuoteLoc {
pos: usize,
quotation: char,
}
#[derive(Debug)]
/// Describes a pair of quotes.
///
/// Quotation marks are generally either `"` or `'`, but can be any character.
struct QuotePair {
start: usize,
end: usize,
quotation: char,
}
#[derive(Debug, PartialEq)]
/// Describes a 'blob' of the input string.
///
/// A 'blob' can be thought of as a chunk or portion of the string. It can be defined as quoted
/// chunks of the string, or non-quoted chunks, with no other cases. Blobs are contiguous and thus
/// do not overlap.
enum Blob<'a> {
Normal(&'a str),
Quoted(&'a str),
}
/// Some shorthand functions for constructing Blobs.
#[cfg(test)]
impl<'a> Blob<'a> {
/// Constructs a Normal blob.
fn n(s: &str) -> Blob {
Blob::Normal(s)
}
/// Constructs a Quoted blob.
fn q(s: &str) -> Blob {
Blob::Quoted(s)
}
}
impl DefaultTokenizer {
/// Constructs a `DefaultTokenizer`.
pub fn new(quotations: Vec<char>) -> DefaultTokenizer {
DefaultTokenizer { quotations }
}
/// Finds quotes in the line string, and returns them.
///
/// This method does not have any intelligence around pairing of quotation marks, it simply
/// finds and returns the ones it sees.
///
/// # Arguments
/// `line` - The input line.
///
/// # Returns
/// `Vec<QuoteLoc>` - A listing of all the quotation marks. Pairs represent two elements in
/// this listing.
fn find_quotes(&self, line: &str) -> Vec<QuoteLoc> {
let mut quote_locs: Vec<QuoteLoc> = Vec::new();
for (i, ch) in line.char_indices() {
if self.quotations.contains(&ch) {
quote_locs.push(QuoteLoc {
pos: i,
quotation: ch,
})
}
}
quote_locs
}
/// Finds pairings of balanced quotes in the string, given a series of quote locations.
///
/// This method is the intelligent sibling of `find_quotes()`. It takes the `QuoteLoc`'s
/// returned by `find_quotes()` and pairs together the `QuoteLoc`'s into `QuotePair`'s.
///
/// # Arguments
/// `quote_locs` - The quote locations as returned by `find_quotes()`.
///
/// # Returns
/// `Vec<QuotePair>` - The paired couples of quotes based on the given quote locations.
fn find_quote_pairs(&self, quote_locs: Vec<QuoteLoc>) -> Vec<QuotePair> {
let mut quote_pairs: Vec<QuotePair> = Vec::new();
let mut start_idx = 0;
let mut next_idx = None;
// The algorithm here is that we will go through each of the quote locations, and for each
// of them, we will iterate the rest of the quote locations until we find a matching
// quotation character, upon which we will discard any quotations in between (since they
// are actually contained within the outer quotes), and add this pair.
//
// Then beginning from after the second QuoteLoc of the pair, we repeat until we've
// exhausted all the QuoteLocs.
while start_idx < quote_locs.len() {
// This .unwrap() is safe, because of the while condition.
let start = quote_locs.get(start_idx).unwrap();
for i in start_idx + 1..quote_locs.len() {
// This .unwrap() is safe, because of the for loops range being upper bounded by
// quote_locs.len() exclusively. For the lower bound, we know that start_idx+1 is
// within bounds, because of the outer while condition. If adding 1 brings it to
// quote_locs.len(), that would exceed the for range and this code would not be
// executed.
let current = quote_locs.get(i).unwrap();
if current.quotation == start.quotation {
quote_pairs.push(QuotePair {
start: start.pos,
end: current.pos,
quotation: current.quotation,
});
next_idx = Some(i + 1);
break;
}
if next_idx.is_none() {
next_idx = Some(i)
}
}
if let Some(idx) = next_idx {
start_idx = idx;
} else {
break;
}
next_idx = None;
}
quote_pairs
}
/// Creates blobs from the original line based on the given quote pairs.
///
/// This function essentially breaks apart the line into quoted and non-quoted pieces.
///
/// # Arguments
/// `line` - The input line.
/// `pairs` - The listing of quote pairs.
///
/// # Returns
/// `Vec<Blob>` - The listing of quoted & non-quoted blobs of the input line.
fn construct_slices_from_pairs<'a>(
&self,
line: &'a str,
pairs: Vec<QuotePair>,
) -> Vec<Blob<'a>> {
let mut blobs: Vec<Blob> = Vec::new();
let mut cur = 0;
// Now we have the pairs. Get the slices.
for pair in pairs.iter() {
// If the current position does not match the pair.start, that means that the region of
// the input from cur to pair.start is itself a blob, and it's unquoted. Let's make
// sure we don't forget that.
if cur != pair.start {
blobs.push(Blob::Normal(&line[cur..pair.start]));
}
// Of course, the quote pair describes a blob by its region in the line.
blobs.push(Blob::Quoted(&line[pair.start + 1..pair.end]));
cur = pair.end + 1;
}
// If a quote pair does not end at the end of a line (aka, the second quotation character
// in the pair is not the last character of the line), then that means there is an extra
// unquoted blob at the end of the line that we forgot about. Let's remember that here.
if let Some(quote_pair) = pairs.last() {
if quote_pair.end + 1 != line.len() {
blobs.push(Blob::Normal(&line[quote_pair.end + 1..]));
}
}
blobs
}
/// Globs together parts of the string that are surrounded by quotation marks, and returns a
/// series of blobs of the input line based on it.
///
/// In practice for shi, this refers to ASCII " and ', but it is written generally for any set
/// of quotation characters.
///
/// # Arguments
/// `line` - The input line.
///
/// # Returns
/// `Vec<Blob>` - The listing of quoted & non-quoted blobs of the input line.
fn split_into_quote_blobs<'a>(&self, line: &'a str) -> Vec<Blob<'a>> {
// This is not a particularly fast algorithm. But it doesn't need to be. Instead, we opt
// for clarity.
// First, identify where all the quotes are.
let quote_locs = self.find_quotes(line);
// Now, go through those quote locations and pair them accordingly.
let quote_pairs = self.find_quote_pairs(quote_locs);
// If no quotes matched, then just pretend we don't care (because we don't).
if quote_pairs.is_empty() {
return vec![Blob::Normal(line)];
}
// Finally, use the pair ranges to construct the individual slices.
self.construct_slices_from_pairs(line, quote_pairs)
}
/// Splits the given blobs by spaces, and returns the flattened vector of splits.
///
/// The key thing to note here is that a _quoted_ blob is not split, and maintained.
/// Whereas non-quoted blobs are split by space.
///
/// # Arguments
/// `line_blobs` - The blobs of an input line.
///
/// # Returns
/// `Vec<&str>` - A series of slices into an input line that represent its component tokens.
fn split_by_space<'a>(&self, line_blobs: Vec<Blob<'a>>) -> Vec<&'a str> {
let mut splitted_parts: Vec<&str> = Vec::new();
for blob in line_blobs {
match blob {
Blob::Normal(s) => {
// Since this is not protected by surrounding quotes, we _do_ want to split
// this. We simply do it by space, and iterate the split result, adding them
// onto splitted parts. extend() helps us do this elegantly.
// Small note though: we don't want to add empty strings, since they are
// meaningless and are likely just the result of trailing/leading whitespace.
splitted_parts.extend(s.split(' ').filter(|s| !s.is_empty()));
}
Blob::Quoted(s) => {
// We don't want to split inside the quote, so just add this immediately.
splitted_parts.push(s);
}
}
}
splitted_parts
}
}
#[cfg(test)]
#[test]
fn test_find_quotes() {
let tokenizer = DefaultTokenizer::new(vec!['\'']);
let quote_locs = tokenizer.find_quotes("hello 'how are' you?");
assert_eq!(
quote_locs,
vec![
QuoteLoc {
pos: 6,
quotation: '\''
},
QuoteLoc {
pos: 14,
quotation: '\''
}
]
);
}
impl Tokenizer for DefaultTokenizer {
/// Tokenizes the given input line into its constituent components.
///
/// In particular, this preserves quoted strings and does not split inside of them, but
/// outside, splits them, by space.
///
/// # Arguments
/// `line` - The input line.
///
/// # Returns
/// `Vec<&str>` - A series of slices into an input line that represent its component tokens.
fn tokenize<'a>(&self, line: &'a str) -> Tokenization<'a> {
let line_bits_with_quotes_globbed = self.split_into_quote_blobs(line);
Tokenization {
tokens: self.split_by_space(line_bits_with_quotes_globbed),
trailing_space: line.ends_with(' '),
}
}
}
#[cfg(test)]
mod test {
use super::*;
// Since we test the two functions that comprise this individually, and the implementation of
// this function is just a composition, most of the coverage is already handled.
// So, we give one big and complex case.
#[test]
fn tokenize() {
use pretty_assertions::assert_eq;
let tokenizer = DefaultTokenizer::new(vec!['"', '\'', '|', '-']);
assert_eq!(
tokenizer.tokenize(
"bar 'foo is here' and quux is not\n necessarily 'here'\" b\"ut you co|uld say 'there'-",
).tokens,
vec![
"bar",
"foo is here",
"and",
"quux",
"is",
"not\n",
"necessarily",
"here",
" b",
"ut",
"you",
"co|uld",
"say",
"there",
"-",
]
)
}
mod glob_quotes {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn basic_single() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("foo 'hi there!' btw hello"),
vec![Blob::n("foo "), Blob::q("hi there!"), Blob::n(" btw hello")]
);
}
#[test]
fn basic_double() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("foo \"hi there!\" btw hello"),
vec![Blob::n("foo "), Blob::q("hi there!"), Blob::n(" btw hello")]
);
}
#[test]
fn no_quotes() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("foo hi there! btw hello"),
vec![Blob::n("foo hi there! btw hello")]
);
}
#[test]
fn quote_at_left() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("'foo hi' there! btw hello"),
vec![Blob::q("foo hi"), Blob::n(" there! btw hello")]
);
}
#[test]
fn quote_at_right() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("there! btw hello 'foo hi'"),
vec![Blob::n("there! btw hello "), Blob::q("foo hi")]
);
}
#[test]
fn single_dangling() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("there! btw hello 'foo hi"),
vec![Blob::n("there! btw hello 'foo hi")]
);
}
#[test]
fn multiple_dangling() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'', '|']);
assert_eq!(
tokenizer.split_into_quote_blobs("abc'defghijklmnopq\"rstuvwxyz|vvvv|v"),
vec![
Blob::n("abc'defghijklmnopq\"rstuvwxyz"),
Blob::q("vvvv"),
Blob::n("v")
]
);
}
#[test]
fn one_success_amongst_dangling() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'', '|', '-', '.']);
assert_eq!(
tokenizer.split_into_quote_blobs("abc'defghi.jklmnopq\"rstu\"vwx-yz|vvvv|v"),
vec![
Blob::n("abc'defghi.jklmnopq"),
Blob::q("rstu"),
Blob::n("vwx-yz"),
Blob::q("vvvv"),
Blob::n("v")
]
);
}
#[test]
fn dangling_inside_matched_quotes() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("there! btw hello 'foo\" hi'"),
vec![Blob::n("there! btw hello "), Blob::q("foo\" hi")]
);
}
#[test]
fn dangling_after_matched_quotes() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("there! btw 'foo hi' \" hello"),
vec![
Blob::n("there! btw "),
Blob::q("foo hi"),
Blob::n(" \" hello")
]
);
}
#[test]
fn dangling_before_matched_quotes() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("there!\" btw 'foo hi' hello"),
vec![
Blob::n("there!\" btw "),
Blob::q("foo hi"),
Blob::n(" hello")
]
);
}
#[test]
fn dangling_at_start() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("'there! btw foo hi hello"),
vec![Blob::n("'there! btw foo hi hello")]
);
}
#[test]
fn dangling_at_end() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("there! btw foo hi hello'"),
vec![Blob::n("there! btw foo hi hello'")]
);
}
#[test]
fn dangling_at_start_with_pair() {
let tokenizer = DefaultTokenizer::new(vec!['|', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("'there! btw |foo |hi hello"),
vec![
Blob::n("'there! btw "),
Blob::q("foo "),
Blob::n("hi hello")
]
);
}
#[test]
fn dangling_at_end_with_pair() {
let tokenizer = DefaultTokenizer::new(vec!['|', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("there! btw |foo |hi hello'"),
vec![
Blob::n("there! btw "),
Blob::q("foo "),
Blob::n("hi hello'")
]
);
}
#[test]
fn multiple_non_overlapping_pairs() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("abc'defg'hijk'lmno'pqr'stuvwx'yz"),
vec![
Blob::n("abc"),
Blob::q("defg"),
Blob::n("hijk"),
Blob::q("lmno"),
Blob::n("pqr"),
Blob::q("stuvwx"),
Blob::n("yz")
]
);
}
#[test]
fn many_kinds_of_quotes() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'', '|']);
assert_eq!(
tokenizer.split_into_quote_blobs("abc'defg'hijk'lmno'pqr'stuvwx'yz|vvvv|v"),
vec![
Blob::n("abc"),
Blob::q("defg"),
Blob::n("hijk"),
Blob::q("lmno"),
Blob::n("pqr"),
Blob::q("stuvwx"),
Blob::n("yz"),
Blob::q("vvvv"),
Blob::n("v")
]
);
}
#[test]
fn only_one_quote() {
let tokenizer = DefaultTokenizer::new(vec!['|']);
assert_eq!(
tokenizer.split_into_quote_blobs("abc'defg'hijk'lmno'pqr'stuvwx'yz|vvvv|v"),
vec![
Blob::n("abc'defg'hijk'lmno'pqr'stuvwx'yz"),
Blob::q("vvvv"),
Blob::n("v")
]
);
}
#[test]
fn multiple_kinds_of_quotes() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("abc'defg'hijklmnopqr\"stuvwx\"yz"),
vec![
Blob::n("abc"),
Blob::q("defg"),
Blob::n("hijklmnopqr"),
Blob::q("stuvwx"),
Blob::n("yz")
]
);
}
#[test]
fn empty_string() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(tokenizer.split_into_quote_blobs(""), vec![Blob::n("")]);
}
#[test]
fn only_quotes() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("''''''''"),
// There are 4 pairs of single quotes above, so 4 blobs:
vec![Blob::q(""), Blob::q(""), Blob::q(""), Blob::q("")]
);
}
#[test]
fn mixture_of_only_quotes() {
let tokenizer = DefaultTokenizer::new(vec!['|', '\'']);
assert_eq!(
tokenizer.split_into_quote_blobs("''||'|''|'||''|'|"),
vec![
Blob::q(""),
Blob::q(""),
Blob::q("|"),
Blob::q("|"),
Blob::q(""),
Blob::q(""),
Blob::q("'")
]
);
}
}
mod split_by_space {
use super::*;
#[test]
fn empty_string() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
let empty_vec: Vec<&str> = Vec::new();
assert_eq!(tokenizer.split_by_space(vec![]), empty_vec);
}
#[test]
fn empty_blob() {
// I don't think this is actually ever possible if we take blobs from
// split_into_quote_blobs(), but whatever.
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
// We expect us to not include the empty string, since the tokenizer considers it
// useless.
let empty_vec: Vec<&str> = Vec::new();
assert_eq!(tokenizer.split_by_space(vec![Blob::n("")]), empty_vec);
}
#[test]
fn multiple_empty_blobs() {
// Ditto comments in the empty_blob() test.
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
let empty_vec: Vec<&str> = Vec::new();
assert_eq!(
tokenizer.split_by_space(vec![Blob::n(""), Blob::n(""), Blob::n("")]),
empty_vec
);
}
#[test]
fn only_normals() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_by_space(vec![Blob::n("hi there"), Blob::n("euler is cool")]),
vec!["hi", "there", "euler", "is", "cool"]
);
}
#[test]
fn only_quoteds() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_by_space(vec![Blob::q("hi there"), Blob::q("euler is cool")]),
vec!["hi there", "euler is cool"]
);
}
#[test]
fn quoted_then_normal() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_by_space(vec![Blob::q("hi there!"), Blob::n("euler is cool")]),
vec!["hi there!", "euler", "is", "cool"]
);
}
#[test]
fn normal_then_quoted() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_by_space(vec![Blob::n("euler is cool"), Blob::q("hi there!")]),
vec!["euler", "is", "cool", "hi there!"]
);
}
#[test]
fn quoted_surrounded_by_normals() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_by_space(vec![
Blob::n("euler is cool"),
Blob::q("hi there!"),
Blob::n("euler is cool")
]),
vec!["euler", "is", "cool", "hi there!", "euler", "is", "cool"]
);
}
#[test]
fn normal_surrounded_by_quoteds() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_by_space(vec![
Blob::q("hi there!"),
Blob::n("euler is cool"),
Blob::q("hi there!")
]),
vec!["hi there!", "euler", "is", "cool", "hi there!"]
);
}
#[test]
fn trailing_spaces() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_by_space(vec![
Blob::q("hi there!"),
Blob::n("euler is cool "),
Blob::q("hi there!")
]),
vec!["hi there!", "euler", "is", "cool", "hi there!"]
);
}
#[test]
fn with_newline() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_by_space(vec![
Blob::q("hi there!"),
// We expect the newline to not be used as a splitting term.
Blob::n("euler is\ncool "),
Blob::q("hi there!")
]),
vec!["hi there!", "euler", "is\ncool", "hi there!"]
);
}
#[test]
fn with_tab() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_by_space(vec![
Blob::q("hi there!"),
// We expect the tab to not be used as a splitting term.
Blob::n("euler is\tcool "),
Blob::q("hi there!")
]),
vec!["hi there!", "euler", "is\tcool", "hi there!"]
);
}
#[test]
fn multiple_spaces() {
let tokenizer = DefaultTokenizer::new(vec!['"', '\'']);
assert_eq!(
tokenizer.split_by_space(vec![
Blob::q("hi there!"),
// We expect the tab to not be used as a splitting term.
Blob::n("euler is cool "),
Blob::q("hi there!")
]),
vec!["hi there!", "euler", "is", "cool", "hi there!"]
);
}
}
}