forked from brson/rustpcre
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pcre.rc
executable file
·188 lines (156 loc) · 4.73 KB
/
pcre.rc
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
/* rustpcre - rust PCRE bindings
*
* Copyright 2011 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#[link(name = "pcre",
vers = "0.2",
uuid = "54aba0f8-a7b1-4beb-92f1-4cf625264841")];
#[crate_type = "lib"];
#[allow(non_camel_case_types)]
extern mod std;
use libc::{c_int, c_void, c_char};
type pcre_t = *c_void;
type pcre_extra_t = *c_void;
extern mod pcre {
fn pcre_compile(pattern: *c_char, options: c_int, errptr: &*c_char,
erroffset: &c_int, tableptr: *u8) -> *pcre_t;
fn pcre_exec(re: *pcre_t, extra: *pcre_extra_t, subject: *c_char,
length: c_int, startoffset: c_int, options: c_int,
ovector: *c_int, ovecsize: c_int) -> c_int;
fn pcre_get_stringnumber(re: *pcre_t, name: *c_char) -> c_int;
fn pcre_refcount(re: *pcre_t, adj: c_int) -> c_int;
}
pub struct Pcre {
priv re: *pcre_t,
}
pub impl Pcre {
static fn new(pattern: &str) -> Result<Pcre, ~str> unsafe {
do str::as_c_str(pattern) |pattern| {
let errv = ptr::null();
let erroff = 0 as c_int;
let re = pcre::pcre_compile(
pattern,
0 as c_int,
&errv,
&erroff,
ptr::null()
);
if re == ptr::null() {
Err(str::raw::from_c_str(errv))
} else {
Ok(Pcre { re: re })
}
}
}
fn exec(target: &str) -> Option<Match> unsafe {
let oveclen = 30;
let mut ovec = vec::from_elem(oveclen as uint, 0i32);
let ovecp = vec::raw::to_ptr(ovec);
let r = do str::as_c_str(target) |p| {
pcre::pcre_exec(
self.re,
ptr::null(),
p,
target.len() as c_int,
0 as c_int,
0 as c_int,
ovecp,
oveclen as c_int
)
};
if r < 0i32 { return None; }
let mut idx = 2; // skip the whole-string match at the start
let mut substrings = ~[];
while idx < oveclen * 2 / 3 {
let start = ovec[idx];
let end = ovec[idx + 1];
idx = idx + 2;
if start != end && start >= 0i32 && end >= 0i32 {
substrings.push(@target.slice(start as uint, end as uint));
}
}
// Make sure we let pcre know that we're sharing the pcre_t pointer.
pcre::pcre_refcount(self.re, 1 as c_int);
Some(Match {
substrings: substrings,
re: self.re
})
}
}
impl Pcre: Drop {
fn finalize(&self) {
pcre::pcre_refcount(self.re, -1 as c_int);
}
}
pub struct Match {
priv re: *pcre_t,
substrings: ~[@~str],
}
pub impl Match {
pure fn named(name: &str) -> Option<@~str> {
let idx = do str::as_c_str(name) |name| {
unsafe { pcre::pcre_get_stringnumber(self.re, name) as int }
};
if idx > 0 {
Some(self[idx as uint - 1])
} else {
None
}
}
}
impl Match: Drop {
fn finalize(&self) {
pcre::pcre_refcount(self.re, -1 as c_int);
}
}
pub impl Match: ops::Index<uint, @~str> {
pure fn index(&self, idx: uint) -> @~str {
self.substrings[idx]
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_compile_fail() {
assert result::unwrap_err(Pcre::new("(")) == ~"missing )";
}
#[test]
fn test_match_basic() {
let r = Pcre::new("...").unwrap();
let m = r.exec("abc").unwrap();
assert m.substrings.is_empty();
}
#[test]
fn test_match_fail() {
let r = Pcre::new("....").unwrap();
let m = r.exec("ab");
assert m.is_none();
}
#[test]
fn test_substring() {
let r = Pcre::new("(.)bcd(e.g)").unwrap();
let m = r.exec("abcdefg").unwrap();
assert *m[0u] == ~"a";
assert *m[1u] == ~"efg";
}
#[test]
fn test_named() {
let r = Pcre::new("(?<foo>..).(?<bar>..)").unwrap();
let m = r.exec("abcde").unwrap();
assert m.named("foo") == Some(@~"ab");
assert m.named("bar") == Some(@~"de");
assert m.named("baz") == None;
}
}