Skip to content

Commit

Permalink
fix index of str.ends_with
Browse files Browse the repository at this point in the history
poppingmoon committed Aug 24, 2024
1 parent fc97648 commit c819a36
Showing 2 changed files with 12 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/interpreter/primitive_props.rs
Original file line number Diff line number Diff line change
@@ -277,14 +277,14 @@ pub fn get_prim_prop(target: Value, name: String) -> Result<Value, AiScriptError
.next()
.map(f64::try_from)
.map_or(Ok(None), |r| r.map(|i| Some(i as isize)))?
.unwrap_or(0);
.unwrap_or(target_len);
if raw_index < -target_len || target_len < raw_index {
return Ok(Value::bool(false));
}
let index = if raw_index >= 0 {
target_len - raw_index
raw_index
} else {
-raw_index
target_len + raw_index
} as usize;

Ok(Value::bool(
18 changes: 9 additions & 9 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -3964,9 +3964,9 @@ mod primitive_props {
let str = "hello"
let empty = ""
<: [
// str.ends_with("", 3), str.ends_with("lo", 5),
// str.ends_with("ll", 4), str.ends_with("he", 2),
// str.ends_with("ll", -1), str.ends_with("he", -3),
str.ends_with("", 3), str.ends_with("lo", 5),
str.ends_with("ll", 4), str.ends_with("he", 2),
str.ends_with("ll", -1), str.ends_with("he", -3),
str.ends_with("he", 5), str.ends_with("lo", 3),
str.ends_with("lo", -6), str.ends_with("", -7),
str.ends_with("lo", 6), str.ends_with("", 7),
@@ -3977,12 +3977,12 @@ mod primitive_props {
assert_eq!(
res,
arr([
// bool(true),
// bool(true),
// bool(true),
// bool(true),
// bool(true),
// bool(true),
bool(true),
bool(true),
bool(true),
bool(true),
bool(true),
bool(true),
bool(false),
bool(false),
bool(false),

0 comments on commit c819a36

Please sign in to comment.