Skip to content

Commit

Permalink
Matching substrings if next string is found
Browse files Browse the repository at this point in the history
contains [x:"test":y] = x + y
contains "xtesty" == "xy"
contains "x0test1y" == "x01y"
  • Loading branch information
codefionn committed Dec 25, 2023
1 parent 36132e5 commit 8366ce9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,18 @@ pub async fn set_values_in_context_one(

len
}
_ => 1,
_ => match &lst0[i + 1] {
Syntax::ValStr(s) if s.len() > 0 => {
let s: Vec<char> = s.chars().collect();
match str1.windows(s.len()).position(|x| x == s) {
Some(idx) => idx,
None => {
return ComparisonResult::Matched(false);
}
}
}
_ => 1,
},
};

let c: String = str1.drain(0..len).collect();
Expand Down
58 changes: 58 additions & 0 deletions src/tests/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2310,3 +2310,61 @@ async fn test_infinite_match_map() {
.await
);
}

#[tokio::test]
async fn test_contains_string() {
assert_eq!(
Ok("\"xy\"".to_string()),
parse_to_str(
"if let [x:\"test\":y] = \"xtesty\" then x + y else @false",
&mut ContextHandler::async_default().await,
&mut SystemHandler::default()
)
.await
);
assert_eq!(
Ok("\"x\"".to_string()),
parse_to_str(
"if let [x:\"test\":y] = \"xtest\" then x + y else @false",
&mut ContextHandler::async_default().await,
&mut SystemHandler::default()
)
.await
);
assert_eq!(
Ok("\"x01y\"".to_string()),
parse_to_str(
"if let [x:\"test\":y] = \"x0test1y\" then x + y else @false",
&mut ContextHandler::async_default().await,
&mut SystemHandler::default()
)
.await
);
assert_eq!(
Ok("(\"x0\", \"1y\")".to_string()),
parse_to_str(
"if let [x:\"test\":y] = \"x0test1y\" then (x, y) else @false",
&mut ContextHandler::async_default().await,
&mut SystemHandler::default()
)
.await
);
assert_eq!(
Ok("@false".to_string()),
parse_to_str(
"if let [x:\"test\":y] = \"x0tes1y\" then x + y else @false",
&mut ContextHandler::async_default().await,
&mut SystemHandler::default()
)
.await
);
assert_eq!(
Ok("((\"x\", \"0\"), \"1y\")".to_string()),
parse_to_str(
"if let [z:x:\"test\":y] = \"x0test1y\" then (z, x, y) else @false",
&mut ContextHandler::async_default().await,
&mut SystemHandler::default()
)
.await
);
}

0 comments on commit 8366ce9

Please sign in to comment.