Debian Codegraph @

src/_private/regex_syntax.rs

raw symbols SCIP
1 #![allow(clippy::all)]
2 use crate::_alloc_prelude::*;
3 // Copied from regex_syntax crate to avoid pulling in the whole crate just for a utility function
4 // https://github.com/rust-lang/regex/blob/431c4e4867e1eb33eb39b23ed47c9934b2672f8f/regex-syntax/src/lib.rs
5 //
6 // Copyright (c) 2014 The Rust Project Developers
7 //
8 // Permission is hereby granted, free of charge, to any
9 // person obtaining a copy of this software and associated
10 // documentation files (the "Software"), to deal in the
11 // Software without restriction, including without
12 // limitation the rights to use, copy, modify, merge,
13 // publish, distribute, sublicense, and/or sell copies of
14 // the Software, and to permit persons to whom the Software
15 // is furnished to do so, subject to the following
16 // conditions:
17 //
18 // The above copyright notice and this permission notice
19 // shall be included in all copies or substantial portions
20 // of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
23 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
24 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
25 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
26 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
27 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
29 // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31
32 pub fn escape(text: &str) -> String {
33 let mut quoted = String::new();
34 escape_into(text, &mut quoted);
35 quoted
36 }
37
38 fn escape_into(text: &str, buf: &mut String) {
39 buf.reserve(text.len());
40 for c in text.chars() {
41 if is_meta_character(c) {
42 buf.push('\\');
43 }
44 buf.push(c);
45 }
46 }
47
48 fn is_meta_character(c: char) -> bool {
49 match c {
50 '\\' | '.' | '+' | '*' | '?' | '(' | ')' | '|' | '[' | ']' | '{' | '}' | '^' | '$'
51 | '#' | '&' | '-' | '~' => true,
52 _ => false,
53 }
54 }