Skip to content

Commit

Permalink
bug: fix substr to allow 0 and -1 as index
Browse files Browse the repository at this point in the history
  • Loading branch information
linux-china committed Dec 25, 2024
1 parent 93aa368 commit 163322b
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/codegen/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2270,7 +2270,20 @@ pub(crate) unsafe extern "C" fn escape_tsv(s: *mut U128) -> U128 {

pub(crate) unsafe extern "C" fn substr(base: *mut U128, l: Int, r: Int) -> U128 {
let base = &*(base as *mut Str);
let res = base.sub_str((l - 1) as usize, r as usize);
let mut pos = l;
if pos == 0 {
pos = 1;
}
let res = if pos > 0 {
base.sub_str((pos - 1) as usize, r as usize)
} else {
// reverse
let mut new_pos = base.len() as Int + pos - 1;
if new_pos < 0 {
new_pos = 0;
}
base.sub_str(new_pos as usize, r as usize)
};
mem::transmute::<Str, U128>(res)
}

Expand Down

0 comments on commit 163322b

Please sign in to comment.