aws-smt-strings 0.7.0

A library for manipulating SMT-LIB strings and regular expressions
Documentation
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//!
//! Regular expressions as defined in the SMT-LIB QF_S Theory
//! The alphabet is the set of u32 between 0 and 0x2FFFF.
//!

use crate::matcher::{naive_re_search, SearchResult};
use crate::regular_expressions::*;
use crate::smt_strings::*;
use std::cell::RefCell;

//
// RegLan and ReManager are not thread safe (because we use Rc not Arc).
// We keep a reference to a global ReManager as a thread-local object.
//
thread_local!(static MANAGER : RefCell<ReManager> = RefCell::new(ReManager::new()));

///
/// Singleton language
///
/// # Example
/// ```
/// use aws_smt_strings::{smt_strings::*, smt_regular_expressions::*};
///
/// let test = SmtString::from("abcde");
/// let re = str_to_re(&test);
/// assert!(str_in_re(&test, re));
/// ```
pub fn str_to_re(s: &SmtString) -> RegLan {
    MANAGER.with(|m| m.borrow_mut().str(s))
}

///
/// Empty language
///
/// # Example
///
/// ```
/// use aws_smt_strings::{smt_strings::*, smt_regular_expressions::*};
///
/// let test = EMPTY;
/// assert!(! str_in_re(&test, re_none()));
/// ```
pub fn re_none() -> RegLan {
    MANAGER.with(|m| m.borrow().empty())
}

///
/// Full language
///
/// # Example
///
/// ```
/// use aws_smt_strings::{smt_strings::*, smt_regular_expressions::*};
///
/// assert!(str_in_re(&EMPTY, re_all()));
/// assert!(str_in_re(&"18938".into(), re_all()));
/// ```
pub fn re_all() -> RegLan {
    MANAGER.with(|m| m.borrow().full())
}

///
/// All strings of length 1
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let r = re_allchar();
/// assert!(str_in_re(&"A".into(), r));
/// assert!(! str_in_re(&"AA".into(), r));
/// ```
///
pub fn re_allchar() -> RegLan {
    MANAGER.with(|m| m.borrow_mut().all_chars())
}

///
/// Concatenation of two regular expressions
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let aaa = str_to_re(&"aaa".into());
/// let bbb = str_to_re(&"bbb".into());
/// assert!(str_in_re(&"aaabbb".into(), re_concat(aaa, bbb)));
/// ```
pub fn re_concat(r1: RegLan, r2: RegLan) -> RegLan {
    MANAGER.with(|m| m.borrow_mut().concat(r1, r2))
}

///
/// Concatenation of several regular expressions
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let r1 = str_to_re(&"abc".into());
/// let r2 = re_allchar();
/// let c = re_concat_list([r1, r2, r2]);
/// assert!(str_in_re(&"abcXY".into(), c));
/// ```
pub fn re_concat_list(a: impl IntoIterator<Item = RegLan>) -> RegLan {
    MANAGER.with(|m| m.borrow_mut().concat_list(a))
}

///
/// Union of two regular expressions
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let r1 = re_star(str_to_re(&"a".into()));
/// let r2 = re_star(str_to_re(&"b".into()));
/// let u = re_union(r1, r2);
/// assert!(str_in_re(&"aaaa".into(), u));
/// assert!(str_in_re(&"bbb".into(), u));
/// assert!(str_in_re(&"".into(), u));
/// ```
pub fn re_union(r1: RegLan, r2: RegLan) -> RegLan {
    MANAGER.with(|m| m.borrow_mut().union(r1, r2))
}

///
/// Union of several regular expressions
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let r1 = re_plus(str_to_re(&"a".into()));
/// let r2 = re_plus(str_to_re(&"b".into()));
/// let r3 = re_plus(str_to_re(&"c".into()));
/// let u = re_union_list([r1, r2, r3]);
/// assert!(str_in_re(&"cccc".into(), u));
/// ```
pub fn re_union_list(a: impl IntoIterator<Item = RegLan>) -> RegLan {
    MANAGER.with(|m| m.borrow_mut().union_list(a))
}

///
/// Intersection of two regular expressions
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let r1 = re_star(str_to_re(&"a".into()));
/// let r2 = re_star(str_to_re(&"b".into()));
/// let u = re_inter(r1, r2);
/// assert!(str_in_re(&"".into(), u));
/// assert!(! str_in_re(&"aaa".into(), u));
/// ```
pub fn re_inter(r1: RegLan, r2: RegLan) -> RegLan {
    MANAGER.with(|m| m.borrow_mut().inter(r1, r2))
}

///
/// Intersection of several regular expressions
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let r1 = re_concat(str_to_re(&"aaaa".into()), re_all());
/// let r2 = re_concat(str_to_re(&"aaa".into()), re_all());
/// let r3 = re_concat(str_to_re(&"aa".into()), re_all());
/// let r = re_inter_list([r1, r2, r3]);
/// assert!(str_in_re(&"aaaabb".into(), r));
/// ```
pub fn re_inter_list(a: impl IntoIterator<Item = RegLan>) -> RegLan {
    MANAGER.with(|m| m.borrow_mut().inter_list(a))
}

///
/// Kleene closure
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let r = re_star(re_union(str_to_re(&"aba".into()), str_to_re(&"cc".into())));
/// assert!(str_in_re(&"ccccabacc".into(), r));
/// ```
pub fn re_star(r: RegLan) -> RegLan {
    MANAGER.with(|m| m.borrow_mut().star(r))
}

///
/// Complement
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let r = re_comp(str_to_re(&"abcd".into()));
/// assert!(! str_in_re(&"abcd".into(), r));
/// assert!(str_in_re(&"abcdef".into(), r));
/// ```
pub fn re_comp(r: RegLan) -> RegLan {
    MANAGER.with(|m| m.borrow_mut().complement(r))
}

///
/// Difference of two regular expressions
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let r1 = re_all();
/// let r2 = re_concat(re_allchar(), re_allchar());
/// let r = re_diff(r1, r2);
/// assert!(str_in_re(&"X".into(), r));
/// assert!(str_in_re(&"XYZ".into(), r));
/// ```
///
pub fn re_diff(r1: RegLan, r2: RegLan) -> RegLan {
    MANAGER.with(|m| m.borrow_mut().diff(r1, r2))
}

///
/// Difference of several languages
///
/// `re_diff_list(r, a)` is the same as `re_diff(r, re_inter_list(a))`
///
pub fn re_diff_list(r: RegLan, a: impl IntoIterator<Item = RegLan>) -> RegLan {
    MANAGER.with(|m| m.borrow_mut().diff_list(r, a))
}

///
/// Kleene cross
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let r = re_union(str_to_re(&"ab".into()), str_to_re(&"cde".into()));
/// assert!(str_in_re(&"abcdeab".into(), re_plus(r)));
/// ```
pub fn re_plus(r: RegLan) -> RegLan {
    MANAGER.with(|m| m.borrow_mut().plus(r))
}

///
/// Option
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let r = str_to_re(&"abcd".into());
/// let opt_r = re_opt(r);
/// assert!(str_in_re(&"".into(), opt_r));
/// assert!(str_in_re(&"abcd".into(), opt_r));
/// ```
pub fn re_opt(r: RegLan) -> RegLan {
    MANAGER.with(|m| m.borrow_mut().opt(r))
}

///
/// Range
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let digit = re_range(&"0".into(), &"9".into());
/// assert!(str_in_re(&"6".into(), digit));
/// ```
///
pub fn re_range(s1: &SmtString, s2: &SmtString) -> RegLan {
    MANAGER.with(|m| m.borrow_mut().smt_range(s1, s2))
}

///
/// Power
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let digit = re_range(&"0".into(), &"9".into());
/// let three_digits = re_power(digit, 3);
/// assert!(str_in_re(&"124".into(), three_digits));
/// ```
pub fn re_power(r: RegLan, k: u32) -> RegLan {
    MANAGER.with(|m| m.borrow_mut().exp(r, k))
}

///
/// Loop
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let digit = re_range(&"0".into(), &"9".into());
/// let two_to_five_digits = re_loop(digit, 2, 5);
/// assert!(str_in_re(&"98".into(), two_to_five_digits));
/// assert!(str_in_re(&"98765".into(), two_to_five_digits));
/// ```
///
pub fn re_loop(r: RegLan, i: u32, j: u32) -> RegLan {
    MANAGER.with(|m| m.borrow_mut().smt_loop(r, i, j))
}

///
/// Check whether a string is in a regular expression
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let any = re_allchar();
/// let all = re_all();
/// let sep = str_to_re(&":".into());
/// let aaa = str_to_re(&"aaa".into());
/// let t = re_concat_list([any, any, sep, aaa, sep, all, sep, aaa, sep, all]);
///
/// assert!(str_in_re(&"ij:aaa::aaa:cdef".into(), t));
/// ```
///
pub fn str_in_re(s: &SmtString, r: RegLan) -> bool {
    MANAGER.with(|m| m.borrow_mut().str_in_re(s, r))
}

///
/// Search for a match of r in s starting at index k
///
fn find_match(r: RegLan, s: &[u32], k: usize, allow_empty: bool) -> SearchResult {
    MANAGER.with(|m| naive_re_search(&mut m.borrow_mut(), r, s, k, allow_empty))
}

///
/// Replace the first and shortest occurrence of r by s2 in s1
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let a_star = re_star(str_to_re(&"a".into()));
/// assert_eq!(str_replace_re(&"baab".into(), a_star, &"cc".into()), "ccbaab".into());
///
/// let a_plus = re_plus(str_to_re(&"a".into()));
/// assert_eq!(str_replace_re(&"baab".into(), a_plus, &"cc".into()), "bccab".into());
///
/// assert_eq!(str_replace_re(&"nomtch".into(), a_plus, &"cc".into()), "nomtch".into());
/// ```
///
pub fn str_replace_re(s1: &SmtString, r: RegLan, s2: &SmtString) -> SmtString {
    let s1 = s1.as_ref();
    match find_match(r, s1, 0, true) {
        SearchResult::NotFound => s1.into(),
        SearchResult::Found(i, j) => {
            let mut x = Vec::new();
            x.extend_from_slice(&s1[..i]);
            x.extend_from_slice(s2.as_ref());
            x.extend_from_slice(&s1[j..]);
            x.into()
        }
    }
}

///
/// Replace all non-empty matches of r by s2 in s1
///
/// # Example
///
/// ```
/// use aws_smt_strings::smt_regular_expressions::*;
///
/// let a_star = re_star(str_to_re(&"a".into()));
/// assert_eq!(str_replace_re_all(&"baab".into(), a_star, &"cd".into()), "bcdcdb".into());
///
/// let digits = re_plus(re_range(&"0".into(), &"9".into()));
/// let pattern = re_concat(str_to_re(&"pre".into()), digits);
/// assert_eq!(str_replace_re_all(&"10pre129prepre0xx".into(), pattern, &"Z".into()),
///            "10Z29preZxx".into());
/// ```
pub fn str_replace_re_all(s1: &SmtString, r: RegLan, s2: &SmtString) -> SmtString {
    let s1 = s1.as_ref();
    let s2 = s2.as_ref();
    let mut x = Vec::new();
    let mut i = 0;
    while let SearchResult::Found(j, k) = find_match(r, s1, i, false) {
        // s[j .. k] == p
        x.extend_from_slice(&s1[i..j]);
        x.extend_from_slice(s2);
        i = k;
    }
    x.extend_from_slice(&s1[i..]);
    x.into()
}