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
//! Parse options parameter to `Regexp#initialize` and `Regexp::compile`.

use onig::RegexOptions;

use crate::extn::core::regexp::Regexp;
use crate::value::Value;

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct Options {
    pub multiline: bool,
    pub ignore_case: bool,
    pub extended: bool,
}

impl Options {
    pub fn ignore_case() -> Self {
        let mut opts = Self::default();
        opts.ignore_case = true;
        opts
    }

    pub fn flags(self) -> RegexOptions {
        let mut bits = RegexOptions::REGEX_OPTION_NONE;
        if self.multiline {
            bits |= RegexOptions::REGEX_OPTION_MULTILINE;
        }
        if self.ignore_case {
            bits |= RegexOptions::REGEX_OPTION_IGNORECASE;
        }
        if self.extended {
            bits |= RegexOptions::REGEX_OPTION_EXTEND;
        }
        bits
    }

    pub fn modifier_string(self) -> &'static str {
        match (self.multiline, self.ignore_case, self.extended) {
            (true, true, true) => "mix",
            (true, true, false) => "mi",
            (true, false, true) => "mx",
            (true, false, false) => "m",
            (false, true, true) => "ix",
            (false, true, false) => "i",
            (false, false, true) => "x",
            (false, false, false) => "",
        }
    }

    fn onig_string(self) -> &'static str {
        match (self.multiline, self.ignore_case, self.extended) {
            (true, true, true) => "mix",
            (true, true, false) => "mi-x",
            (true, false, true) => "mx-i",
            (true, false, false) => "m-ix",
            (false, true, true) => "ix-m",
            (false, true, false) => "i-mx",
            (false, false, true) => "x-mi",
            (false, false, false) => "-mix",
        }
    }
}

pub fn parse(value: &Value) -> Options {
    // If options is an Integer, it should be one or more of the constants
    // Regexp::EXTENDED, Regexp::IGNORECASE, and Regexp::MULTILINE, logically
    // or-ed together. Otherwise, if options is not nil or false, the regexp
    // will be case insensitive.
    if let Ok(options) = value.itself::<i64>() {
        // Only deal with Regexp opts
        let options = options & Regexp::ALL_REGEXP_OPTS;
        Options {
            multiline: options & Regexp::MULTILINE > 0,
            ignore_case: options & Regexp::IGNORECASE > 0,
            extended: options & Regexp::EXTENDED > 0,
        }
    } else if let Ok(options) = value.itself::<Option<bool>>() {
        match options {
            Some(false) | None => Options::default(),
            _ => Options::ignore_case(),
        }
    } else if let Ok(options) = value.itself::<Option<String>>() {
        if let Some(options) = options {
            Options {
                multiline: options.contains('m'),
                ignore_case: options.contains('i'),
                extended: options.contains('x'),
            }
        } else {
            Options::default()
        }
    } else {
        Options::ignore_case()
    }
}

// TODO: Add tests for this parse_pattern, see GH-157.
pub fn parse_pattern(pattern: &str, mut opts: Options) -> (String, Options) {
    let orig_opts = opts;
    let mut chars = pattern.chars();
    let mut enabled = true;
    let mut pat_buf = String::new();
    let mut pointer = 0;
    match chars.next() {
        None => {
            pat_buf.push_str("(?");
            pat_buf.push_str(opts.onig_string());
            pat_buf.push(':');
            pat_buf.push(')');
            return (pat_buf, opts);
        }
        Some(token) if token != '(' => {
            pat_buf.push_str("(?");
            pat_buf.push_str(opts.onig_string());
            pat_buf.push(':');
            pat_buf.push_str(pattern);
            pat_buf.push(')');
            return (pat_buf, opts);
        }
        _ => (),
    };
    pointer += 1;
    match chars.next() {
        None => {
            pat_buf.push_str("(?");
            pat_buf.push_str(opts.onig_string());
            pat_buf.push(':');
            pat_buf.push_str(pattern);
            pat_buf.push(')');
            return (pat_buf, opts);
        }
        Some(token) if token != '?' => {
            pat_buf.push_str("(?");
            pat_buf.push_str(opts.onig_string());
            pat_buf.push(':');
            pat_buf.push_str(pattern);
            pat_buf.push(')');
            return (pat_buf, opts);
        }
        _ => (),
    };
    pointer += 1;
    for token in chars {
        pointer += 1;
        match token {
            '-' => enabled = false,
            'i' => {
                opts.ignore_case = enabled;
            }
            'm' => {
                opts.multiline = enabled;
            }
            'x' => {
                opts.extended = enabled;
            }
            ':' => break,
            _ => {
                pat_buf.push_str("(?");
                pat_buf.push_str(opts.onig_string());
                pat_buf.push(':');
                pat_buf.push_str(pattern);
                pat_buf.push(')');
                return (pat_buf, opts);
            }
        }
    }
    let mut chars = pattern[pointer..].chars();
    let mut token = chars.next();
    let mut nest = 1;
    while token.is_some() {
        match token {
            Some(token) if token == '(' => nest += 1,
            Some(token) if token == ')' => {
                nest -= 1;
                if nest == 0 && chars.next().is_some() {
                    return (
                        format!("(?{}:{})", orig_opts.onig_string(), pattern),
                        orig_opts,
                    );
                }
                break;
            }
            _ => (),
        }
        token = chars.next();
    }

    (
        format!("(?{}:{}", opts.onig_string(), &pattern[pointer..]),
        opts,
    )
}