|
| 1 | +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +#![allow(non_camel_case_types)] |
| 12 | + |
| 13 | +use libc::{c_uchar, c_int, c_void}; |
| 14 | + |
| 15 | +/// Regex wraps a std::regex regular expression. |
| 16 | +/// |
| 17 | +/// It cannot be used safely from multiple threads simultaneously. |
| 18 | +pub struct Regex { |
| 19 | + re: *mut stdcpp_regexp, |
| 20 | +} |
| 21 | + |
| 22 | +unsafe impl Send for Regex {} |
| 23 | + |
| 24 | +impl Drop for Regex { |
| 25 | + fn drop(&mut self) { |
| 26 | + unsafe { stdcpp_regexp_free(self.re); } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Debug)] |
| 31 | +pub struct Error(()); |
| 32 | + |
| 33 | +impl Regex { |
| 34 | + pub fn new(pattern: &str) -> Result<Regex, Error> { |
| 35 | + unsafe { Ok(Regex { re: stdcpp_regexp_new(pattern.into()) }) } |
| 36 | + } |
| 37 | + |
| 38 | + pub fn is_match(&self, text: &str) -> bool { |
| 39 | + unsafe { |
| 40 | + stdcpp_regexp_match(self.re, text.into(), 0, text.len() as c_int) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + pub fn find_iter<'r, 't>(&'r self, text: &'t str) -> FindMatches<'r, 't> { |
| 45 | + FindMatches { |
| 46 | + re: self, |
| 47 | + text: text, |
| 48 | + last_end: 0, |
| 49 | + last_match: None, |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + fn find_at(&self, text: &str, start: usize) -> Option<(usize, usize)> { |
| 54 | + let (mut s, mut e): (c_int, c_int) = (0, 0); |
| 55 | + let matched = unsafe { |
| 56 | + stdcpp_regexp_find( |
| 57 | + self.re, |
| 58 | + text.into(), |
| 59 | + start as c_int, |
| 60 | + text.len() as c_int, |
| 61 | + &mut s, |
| 62 | + &mut e, |
| 63 | + ) |
| 64 | + }; |
| 65 | + if matched { |
| 66 | + Some((s as usize, e as usize)) |
| 67 | + } else { |
| 68 | + None |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +pub struct FindMatches<'r, 't> { |
| 74 | + re: &'r Regex, |
| 75 | + text: &'t str, |
| 76 | + last_end: usize, |
| 77 | + last_match: Option<usize>, |
| 78 | +} |
| 79 | + |
| 80 | +// This implementation is identical to the one Rust uses, since both Rust's |
| 81 | +// regex engine and std::regex handle empty matches in the same way. |
| 82 | +impl<'r, 't> Iterator for FindMatches<'r, 't> { |
| 83 | + type Item = (usize, usize); |
| 84 | + |
| 85 | + fn next(&mut self) -> Option<(usize, usize)> { |
| 86 | + fn next_after_empty(text: &str, i: usize) -> usize { |
| 87 | + let b = match text.as_bytes().get(i) { |
| 88 | + None => return text.len() + 1, |
| 89 | + Some(&b) => b, |
| 90 | + }; |
| 91 | + let inc = if b <= 0x7F { |
| 92 | + 1 |
| 93 | + } else if b <= 0b110_11111 { |
| 94 | + 2 |
| 95 | + } else if b <= 0b1110_1111 { |
| 96 | + 3 |
| 97 | + } else { |
| 98 | + 4 |
| 99 | + }; |
| 100 | + i + inc |
| 101 | + } |
| 102 | + |
| 103 | + if self.last_end > self.text.len() { |
| 104 | + return None; |
| 105 | + } |
| 106 | + let (s, e) = match self.re.find_at(self.text, self.last_end) { |
| 107 | + None => return None, |
| 108 | + Some((s, e)) => (s, e), |
| 109 | + }; |
| 110 | + assert!(s >= self.last_end); |
| 111 | + if s == e { |
| 112 | + // This is an empty match. To ensure we make progress, start |
| 113 | + // the next search at the smallest possible starting position |
| 114 | + // of the next match following this one. |
| 115 | + self.last_end = next_after_empty(&self.text, e); |
| 116 | + // Don't accept empty matches immediately following a match. |
| 117 | + // Just move on to the next match. |
| 118 | + if Some(e) == self.last_match { |
| 119 | + return self.next(); |
| 120 | + } |
| 121 | + } else { |
| 122 | + self.last_end = e; |
| 123 | + } |
| 124 | + self.last_match = Some(self.last_end); |
| 125 | + Some((s, e)) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// stdcpp FFI is below. Note that this uses a hand-rolled C API that is defined |
| 130 | +// in stdcpp.cpp. |
| 131 | + |
| 132 | +type stdcpp_regexp = c_void; |
| 133 | + |
| 134 | +#[repr(C)] |
| 135 | +struct stdcpp_string { |
| 136 | + text: *const c_uchar, |
| 137 | + len: c_int, |
| 138 | +} |
| 139 | + |
| 140 | +impl<'a> From<&'a str> for stdcpp_string { |
| 141 | + fn from(s: &'a str) -> stdcpp_string { |
| 142 | + stdcpp_string { text: s.as_ptr(), len: s.len() as c_int } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +extern { |
| 147 | + fn stdcpp_regexp_new(pat: stdcpp_string) -> *mut stdcpp_regexp; |
| 148 | + fn stdcpp_regexp_free(re: *mut stdcpp_regexp); |
| 149 | + fn stdcpp_regexp_match( |
| 150 | + re: *mut stdcpp_regexp, |
| 151 | + text: stdcpp_string, |
| 152 | + startpos: c_int, |
| 153 | + endpos: c_int, |
| 154 | + ) -> bool; |
| 155 | + fn stdcpp_regexp_find( |
| 156 | + re: *mut stdcpp_regexp, |
| 157 | + text: stdcpp_string, |
| 158 | + startpos: c_int, |
| 159 | + endpos: c_int, |
| 160 | + match_start: *mut c_int, |
| 161 | + match_end: *mut c_int, |
| 162 | + ) -> bool; |
| 163 | +} |
0 commit comments