Skip to content

Commit

Permalink
MatchData#offset return utf8 character offset
Browse files Browse the repository at this point in the history
  • Loading branch information
jarkonik committed May 31, 2023
1 parent c1cd94d commit 0c99a8c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
11 changes: 11 additions & 0 deletions artichoke-backend/src/extn/core/matchdata/matchdata_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

def spec
offset_returns_utf8_character_index

true
end

def offset_returns_utf8_character_index
raise unless 'тест'.match('с').offset(0) == [2, 3]
end
26 changes: 24 additions & 2 deletions artichoke-backend/src/extn/core/matchdata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::fmt::Write as _;
use std::ops::{Bound, RangeBounds};
use std::str;

use bstr::BString;
use bstr::{BString, ByteSlice};
use scolapasta_string_escape::format_debug_escape_into;

use crate::convert::{implicitly_convert_to_int, implicitly_convert_to_string};
Expand Down Expand Up @@ -308,7 +308,12 @@ impl MatchData {
} else {
haystack.len()
};
let offset = self.region.offset();
let offset = self
.haystack
.char_indices()
.position(|n| n.0 >= self.region.offset())
.unwrap();

Ok(Some([offset + begin, offset + end]))
} else {
Ok(None)
Expand Down Expand Up @@ -363,3 +368,20 @@ impl MatchData {
self.regexp.inner().capture0(haystack)
}
}

#[cfg(test)]
mod tests {
use crate::test::prelude::*;

const SUBJECT: &str = "MatchData";
const FUNCTIONAL_TEST: &[u8] = include_bytes!("matchdata_test.rb");

#[test]
fn functional() {
let mut interp = interpreter();
let result = interp.eval(FUNCTIONAL_TEST);
unwrap_or_panic_with_backtrace(&mut interp, SUBJECT, result);
let result = interp.eval(b"spec");
unwrap_or_panic_with_backtrace(&mut interp, SUBJECT, result);
}
}

0 comments on commit 0c99a8c

Please sign in to comment.