Support negative index counting from end.

This commit is contained in:
Stephen Chung
2021-04-10 15:00:03 +08:00
parent 716e9cf779
commit 40fda5139d
9 changed files with 273 additions and 83 deletions

View File

@@ -12,6 +12,8 @@ fn test_arrays() -> Result<(), Box<EvalAltResult>> {
engine.eval::<char>(r#"let y = [1, [ 42, 88, "93" ], 3]; y[1][2][1]"#)?,
'3'
);
assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y[0]")?, 1);
assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y[-1]")?, 3);
assert!(engine.eval::<bool>("let y = [1, 2, 3]; 2 in y")?);
assert_eq!(engine.eval::<INT>("let y = [1, 2, 3]; y += 4; y[3]")?, 4);

View File

@@ -46,7 +46,11 @@ fn test_string() -> Result<(), Box<EvalAltResult>> {
assert_eq!(engine.eval::<String>("to_string(42)")?, "42");
#[cfg(not(feature = "no_index"))]
assert_eq!(engine.eval::<char>(r#"let y = "hello"; y[1]"#)?, 'e');
{
assert_eq!(engine.eval::<char>(r#"let y = "hello"; y[1]"#)?, 'e');
assert_eq!(engine.eval::<char>(r#"let y = "hello"; y[-1]"#)?, 'o');
assert_eq!(engine.eval::<char>(r#"let y = "hello"; y[-4]"#)?, 'e');
}
#[cfg(not(feature = "no_object"))]
assert_eq!(engine.eval::<INT>(r#"let y = "hello"; y.len"#)?, 5);
@@ -109,9 +113,7 @@ fn test_string_substring() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
assert_eq!(
engine.eval::<String>(
r#"let x = "\u2764\u2764\u2764 hello! \u2764\u2764\u2764"; x.sub_string(-1, 2)"#
)?,
engine.eval::<String>(r#"let x = "hello! \u2764\u2764\u2764"; x.sub_string(-2, 2)"#)?,
"❤❤"
);
@@ -200,9 +202,9 @@ fn test_string_substring() -> Result<(), Box<EvalAltResult>> {
assert_eq!(
engine.eval::<INT>(
r#"let x = "\u2764\u2764\u2764 hello! \u2764\u2764\u2764"; x.index_of('\u2764', -1)"#
r#"let x = "\u2764\u2764\u2764 hello! \u2764\u2764\u2764"; x.index_of('\u2764', -6)"#
)?,
0
11
);
assert_eq!(