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
use std::cmp;
use std::convert::TryFrom;
use std::mem;
use crate::convert::{FromMrb, RustBackedValue, TryFromMrb};
use crate::extn::core::matchdata::MatchData;
use crate::extn::core::regexp::Regexp;
use crate::sys;
use crate::value::Value;
use crate::Mrb;
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum Error {
Fatal,
PosType,
StringType,
}
#[derive(Debug)]
pub struct Args {
pub string: Option<String>,
pub pos: Option<i64>,
pub block: Option<Value>,
}
impl Args {
const ARGSPEC: &'static [u8] = b"o&|o?\0";
pub unsafe fn extract(interp: &Mrb) -> Result<Self, Error> {
let mut string = <mem::MaybeUninit<sys::mrb_value>>::uninit();
let mut pos = <mem::MaybeUninit<sys::mrb_value>>::uninit();
let mut has_pos = <mem::MaybeUninit<sys::mrb_bool>>::uninit();
let mut block = <mem::MaybeUninit<sys::mrb_value>>::uninit();
sys::mrb_get_args(
interp.borrow().mrb,
Self::ARGSPEC.as_ptr() as *const i8,
string.as_mut_ptr(),
block.as_mut_ptr(),
pos.as_mut_ptr(),
has_pos.as_mut_ptr(),
);
let string = string.assume_init();
let block = block.assume_init();
let has_pos = has_pos.assume_init() != 0;
let string = if let Ok(string) =
<Option<String>>::try_from_mrb(&interp, Value::new(interp, string))
{
string
} else {
return Err(Error::StringType);
};
let pos = if has_pos {
let pos = i64::try_from_mrb(&interp, Value::new(&interp, pos.assume_init()))
.map_err(|_| Error::PosType)?;
Some(pos)
} else {
None
};
let block = if sys::mrb_sys_value_is_nil(block) {
None
} else {
Some(Value::new(interp, block))
};
Ok(Self { string, pos, block })
}
}
pub fn method(interp: &Mrb, args: Args, value: &Value) -> Result<Value, Error> {
let mrb = interp.borrow().mrb;
let data = unsafe { Regexp::try_from_ruby(interp, value) }.map_err(|_| Error::Fatal)?;
let string = if let Some(string) = args.string {
string
} else {
unsafe {
let matchdata = Value::from_mrb(interp, None::<Value>);
sys::mrb_gv_set(mrb, interp.borrow_mut().sym_intern("$~"), matchdata.inner());
return Ok(matchdata);
}
};
let pos = args.pos.unwrap_or_default();
let pos = if pos < 0 {
let strlen = i64::try_from(string.chars().count()).unwrap_or_default();
let pos = strlen + pos;
if pos < 0 {
return Ok(Value::from_mrb(interp, None::<Value>));
}
usize::try_from(pos).map_err(|_| Error::Fatal)?
} else {
usize::try_from(pos).map_err(|_| Error::Fatal)?
};
if pos > string.chars().count() {
return Ok(Value::from_mrb(interp, None::<Value>));
}
let byte_offset = string.chars().take(pos).collect::<String>().len();
let borrow = data.borrow();
let regex = (*borrow.regex).as_ref().ok_or(Error::Fatal)?;
let match_target = &string[byte_offset..];
if let Some(captures) = regex.captures(match_target) {
let num_regexp_globals_to_set = {
let num_previously_set_globals = interp.borrow().num_set_regexp_capture_globals;
cmp::max(num_previously_set_globals, captures.len())
};
for group in 0..num_regexp_globals_to_set {
let sym = if group == 0 {
interp.borrow_mut().sym_intern("$&")
} else {
interp.borrow_mut().sym_intern(&format!("${}", group))
};
let value = Value::from_mrb(&interp, captures.at(group));
unsafe {
sys::mrb_gv_set(mrb, sym, value.inner());
}
}
interp.borrow_mut().num_set_regexp_capture_globals = captures.len();
let mut matchdata = MatchData::new(string.as_str(), borrow.clone(), 0, string.len());
if let Some(match_pos) = captures.pos(0) {
let pre_match = &match_target[..match_pos.0];
let post_match = &match_target[match_pos.1..];
unsafe {
let pre_match_sym = interp.borrow_mut().sym_intern("$`");
sys::mrb_gv_set(
mrb,
pre_match_sym,
Value::from_mrb(interp, pre_match).inner(),
);
let post_match_sym = interp.borrow_mut().sym_intern("$'");
sys::mrb_gv_set(
mrb,
post_match_sym,
Value::from_mrb(interp, post_match).inner(),
);
}
matchdata.set_region(byte_offset + match_pos.0, byte_offset + match_pos.1);
}
let data = unsafe { matchdata.try_into_ruby(interp, None) }.map_err(|_| Error::Fatal)?;
unsafe {
sys::mrb_gv_set(mrb, interp.borrow_mut().sym_intern("$~"), data.inner());
}
if let Some(block) = args.block {
Ok(Value::new(interp, unsafe {
sys::mrb_yield(mrb, block.inner(), data.inner())
}))
} else {
Ok(data)
}
} else {
unsafe {
let last_match_sym = interp.borrow_mut().sym_intern("$~");
sys::mrb_gv_set(
mrb,
last_match_sym,
Value::from_mrb(interp, None::<Value>).inner(),
);
let pre_match_sym = interp.borrow_mut().sym_intern("$`");
sys::mrb_gv_set(
mrb,
pre_match_sym,
Value::from_mrb(interp, None::<Value>).inner(),
);
let post_match_sym = interp.borrow_mut().sym_intern("$'");
sys::mrb_gv_set(
mrb,
post_match_sym,
Value::from_mrb(interp, None::<Value>).inner(),
);
}
Ok(Value::from_mrb(interp, None::<Value>))
}
}