Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue-2362: Add support for regexp needles to String#index and String#rindex #2652

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions artichoke-backend/src/extn/core/regexp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ impl Regexp {
pub fn match_(
&self,
interp: &mut Artichoke,
pattern: Option<&[u8]>,
haystack: Option<&[u8]>,
pos: Option<i64>,
block: Option<Block>,
) -> Result<Value, Error> {
if let Some(pattern) = pattern {
self.0.match_(interp, pattern, pos, block)
if let Some(haystack) = haystack {
self.0.match_(interp, haystack, pos, block)
} else {
interp.unset_global_variable(LAST_MATCH)?;
Ok(Value::nil())
Expand Down
16 changes: 12 additions & 4 deletions artichoke-backend/src/extn/core/string/trampoline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1191,8 +1191,12 @@ pub fn index(
) -> Result<Value, Error> {
let s = unsafe { super::String::unbox_from_value(&mut value, interp)? };
#[cfg(feature = "core-regexp")]
if let Ok(_pattern) = unsafe { Regexp::unbox_from_value(&mut needle, interp) } {
return Err(NotImplementedError::from("String#index with Regexp pattern").into());
if let Ok(regexp) = unsafe { Regexp::unbox_from_value(&mut needle, interp) } {
let match_data = regexp.match_(interp, Some(s.as_slice()), None, None)?;
if match_data.is_nil(){
return Ok(Value::nil());
}
return matchdata::trampoline::begin(interp, match_data, interp.convert(0));
}
let needle = unsafe { implicitly_convert_to_string(interp, &mut needle)? };
let index = if let Some(offset) = offset {
Expand Down Expand Up @@ -1351,8 +1355,12 @@ pub fn rindex(
) -> Result<Value, Error> {
let s = unsafe { super::String::unbox_from_value(&mut value, interp)? };
#[cfg(feature = "core-regexp")]
if let Ok(_pattern) = unsafe { Regexp::unbox_from_value(&mut needle, interp) } {
return Err(NotImplementedError::from("String#index with Regexp pattern").into());
if let Ok(regexp) = unsafe { Regexp::unbox_from_value(&mut needle, interp) } {
let match_data = regexp.match_(interp, Some(s.as_slice()), None, None)?;
if match_data.is_nil(){
return Ok(Value::nil());
}
return matchdata::trampoline::end(interp, match_data, interp.convert(0));
}
let needle = unsafe { implicitly_convert_to_string(interp, &mut needle)? };
let index = if let Some(offset) = offset {
Expand Down