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
use libc::{c_int, c_uint, c_void};
use std::{error, fmt, str, ptr};

use super::{Region, Encoding, Options, Syntax, ENCODING_UTF8, SYNTAX_RUBY, OPTION_NONE};

type OnigRegex = *const c_void;

#[link(name="onig")]
extern {
    fn onig_error_code_to_str(err_buff: *mut u8, err_code: c_int, ...) -> c_int;

    fn onig_new(
        reg: *mut OnigRegex,
        pattern: *const u8,
        pattern_end: *const u8,
        option: c_uint,
        enc: *const Encoding,
        syntax: *const Syntax,
        err_info: *mut OnigErrorInfo
    ) -> c_int;

    fn onig_search(
        reg: OnigRegex,
        str: *const u8,
        end: *const u8,
        start: *const u8,
        range: *const u8,
        region: *const Region,
        option: c_uint
    ) -> c_int;

    fn onig_match(
        reg: OnigRegex,
        str: *const u8,
        end: *const u8,
        at: *const u8,
        region: *const Region,
        option: c_uint
    ) -> c_int;

    fn onig_free(reg: OnigRegex);
}

#[repr(C)]
#[derive(Debug)]
struct OnigErrorInfo {
    enc: *const c_void, // TODO: change type to Encoding
    par: *const u8,
    par_end: *const u8
}

/// An error that occurred during parsing, compiling or evaluating
/// a regular expression.
pub struct Error {
    error: c_int,
    description: String,
}

impl Error {
    fn new(error: c_int, info: Option<OnigErrorInfo>) -> Error {
        let mut err_buff = &mut [0 as u8; 90];
        let len = unsafe {
            match info {
                Some(ref error_info) =>
                    onig_error_code_to_str(
                        err_buff.as_mut_ptr(),
                        error,
                        error_info as *const OnigErrorInfo
                    ),
                None => onig_error_code_to_str(err_buff.as_mut_ptr(), error)
            }
        };
        let description = str::from_utf8(&err_buff[..len as usize]).unwrap();
        Error { error: error, description: description.to_owned() }
    }
}

impl Error {
    /// Return Oniguruma engine error code.
    pub fn code(&self) -> isize {
        self.error as isize
    }
}

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Error({}, {})", self.error, self.description)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Oniguruma error: {}", self.description)
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        &self.description
    }

    fn cause(&self) -> Option<&error::Error> {
        None
    }
}

pub struct RegexConfig<'a> {
    pub options: Options,
    pub syntax: &'a Syntax
    // TODO: add encoding here
    // TODO: should we add case fold flag and other compile time info
    // (see: `onig_new_deluxe`)?
}

impl Default for RegexConfig<'static> {
    fn default() -> RegexConfig<'static> {
        RegexConfig {
            options: OPTION_NONE,
            syntax: SYNTAX_RUBY
        }
    }
}

/// A compiled Oniguruma regular expression.
#[derive(Debug)]
pub struct Regex {
    raw: OnigRegex
}

impl Regex {
    /// Compiles a regular expression with default options. Default syntax is
    /// `SYNTAX_RUBY`.
    ///
    /// Once compiled, it can be used repeatedly to search in a string. If an
    /// invalid expression is given, then an error is returned.
    pub fn new(pattern: &str) -> Result<Regex, Error> {
        Regex::new_with_config(pattern, RegexConfig::default())
    }

    pub fn new_with_config<'a>(pattern: &str, config: RegexConfig<'a>) -> Result<Regex, Error> {
        // Convert the rust types to those required for the call to
        // `onig_new`.
        let pattern_bytes = pattern.as_bytes();
        let (start, end) = (
            pattern_bytes.as_ptr(),
            pattern_bytes[pattern_bytes.len()..].as_ptr()
        );
        let mut reg: OnigRegex = ptr::null();
        let reg_ptr = &mut reg as *mut OnigRegex;

        // We can use this later to get an error message to pass back
        // if regex creation fails.
        let mut error = OnigErrorInfo {
            enc: ptr::null(),
            par: ptr::null(),
            par_end: ptr::null()
        };

        let err = unsafe {
            onig_new(
                reg_ptr,
                start,
                end,
                config.options.bits(),
                ENCODING_UTF8,
                config.syntax,
                &mut error)
        };

        if err == 0 {
            Ok(Regex{ raw: reg })
        } else {
            Err(Error::new(err, Some(error)))
        }
    }

    /// Search pattern in string and store search result into region object.
    ///
    /// Returns match position offset if pattern is found, otherwise return
    /// `None`. You also can use search time options: `OPTION_NOTBOL` and
    /// `OPTION_NOTEOL`.
    pub fn search_with_region(&self,
                              text: &str,
                              region: &mut Region,
                              options: Options)
                              -> Result<Option<usize>, Error> {
        let text_bytes = text.as_bytes();
        let (start, end) = (
            text_bytes.as_ptr(),
            text_bytes[text_bytes.len()..].as_ptr()
        );

        let r = unsafe {
            onig_search(
                self.raw,
                start,
                end,
                start,
                end,
                region,
                options.bits()
            )
        };

        if r >= 0 {
            Ok(Some(r as usize))
        } else if r == -1 {
            Ok(None)
        } else {
            Err(Error::new(r, None))
        }
    }

    /// Match string and store search result into region object.
    ///
    /// Returns match length if pattern is found, otherwise return `None`.
    /// You also can use search time options: `OPTION_NOTBOL` and
    /// `OPTION_NOTEOL`.
    pub fn match_with_region(&self,
                             text: &str,
                             region: &mut Region,
                             options: Options)
                             -> Result<Option<usize>, Error> {
        let text_bytes = text.as_bytes();
        let (start, end) = (
            text_bytes.as_ptr(),
            text_bytes[text_bytes.len()..].as_ptr()
        );

        let r = unsafe {
            onig_match(
                self.raw,
                start,
                end,
                start,
                region,
                options.bits()
            )
        };

        if r >= 0 {
            Ok(Some(r as usize))
        } else if r == -1 {
            Ok(None)
        } else {
            Err(Error::new(r, None))
        }
    }

    /// Returns true if and only if the regex matches the string given.
    ///
    /// # Panics
    ///
    /// This method may panic in the case of memory overflow during execution or
    /// other internal errors of Oniguruma engine.
    pub fn is_match(&self, text: &str) -> bool {
        let mut region = Region::new();
        self.match_with_region(text, &mut region, OPTION_NONE)
            .unwrap()
            .map(|r| r == text.len())
            .unwrap_or(false)
    }

    /// Returns the start and end byte range of the leftmost-first match in
    /// `text`. If no match exists, then `None` is returned.
    ///
    /// Note that this should only be used if you want to discover the position
    /// of the match. Testing the existence of a match is faster if you use
    /// `is_match`.
    ///
    /// # Panics
    ///
    /// This method may panic in the case of memory overflow during execution or
    /// other internal errors of Oniguruma engine.
    pub fn find(&self, text: &str) -> Option<(usize, usize)> {
        let mut region = Region::new();
        self.search_with_region(text, &mut region, OPTION_NONE)
            .unwrap()
            .map(|_| region.pos(0))
            .unwrap_or(None)
    }
}

impl Drop for Regex {
    fn drop(&mut self) {
        unsafe {
            onig_free(self.raw);
        }
    }
}