Skip to content

Commit

Permalink
[Rust] Added String.Split with separator array
Browse files Browse the repository at this point in the history
  • Loading branch information
ncave committed Oct 13, 2024
1 parent a907c53 commit 69c237f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
16 changes: 12 additions & 4 deletions src/Fable.Transforms/Rust/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -893,16 +893,21 @@ let fsFormat (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr op
| ("PrintFormatThen" | "PrintFormatToStringThen"), None, [ cont; MaybeCasted(template) ] ->
Helper.Application(cont, t, [ template ], ?loc = r) |> Some
| "PrintFormatToError", None, [ StringConst fmt ] -> "eprintf!" |> makeRustFormatExpr com r t fmt [] |> Some
| "PrintFormatToError", None, [ StringTempl _ ] -> "eprintf!" |> emitFormat com r t args |> Some
| "PrintFormatToError", None, _ -> "eprintf!" |> emitFormat com r t args |> Some
| "PrintFormatLineToError", None, [ StringConst fmt ] -> "eprintfn!" |> makeRustFormatExpr com r t fmt [] |> Some
| "PrintFormatLineToError", None, _ -> "eprintfn!" |> emitFormat com r t args |> Some
| "PrintFormat", None, [ StringConst fmt ] -> "printf!" |> makeRustFormatExpr com r t fmt [] |> Some
| "PrintFormat", None, [ StringTempl _ ] -> "printf!" |> emitFormat com r t args |> Some
| "PrintFormat", None, _ -> "printf!" |> emitFormat com r t args |> Some
| "PrintFormatLine", None, [ StringConst fmt ] -> "printfn!" |> makeRustFormatExpr com r t fmt [] |> Some
| "PrintFormatLine", None, [ StringTempl _ ] -> "printfn!" |> emitFormat com r t args |> Some
| "PrintFormatLine", None, _ -> "printfn!" |> emitFormat com r t args |> Some
| "PrintFormatToTextWriter", None, [ StringConst fmt ] -> "printf!" |> makeRustFormatExpr com r t fmt [] |> Some
| "PrintFormatToTextWriter", None, _ -> "printf!" |> emitFormat com r t args |> Some
| "PrintFormatLineToTextWriter", None, [ StringConst fmt ] ->
"printfn!" |> makeRustFormatExpr com r t fmt [] |> Some
| "PrintFormatLineToTextWriter", None, _ -> "printfn!" |> emitFormat com r t args |> Some
| "PrintFormatToStringThenFail", None, [ StringConst fmt ] ->
"failwithf!" |> makeRustFormatExpr com r t fmt [] |> Some
| "PrintFormatToStringThenFail", None, [ StringTempl _ ] -> "failwithf!" |> emitFormat com r t args |> Some
| "PrintFormatToStringThenFail", None, _ -> "failwithf!" |> emitFormat com r t args |> Some
| "PrintFormatToStringBuilder", None, [ sb; StringConst fmt ] ->
let cont = Helper.LibCall(com, "Util", "bprintf", t, [ sb ])
"kprintf!" |> makeRustFormatExpr com r t fmt [ cont ] |> Some
Expand Down Expand Up @@ -1430,6 +1435,9 @@ let strings (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr opt
| [ Value(NewArray(ArrayValues [ arg1 ], String, _), _); ExprTypeAs(Number(_, NumberInfo.IsEnum _), arg2) ] ->
Helper.LibCall(com, "String", "split", t, [ c; arg1; makeIntConst -1; arg2 ], ?loc = r)
|> Some
| [ ExprTypeAs(Array(String, _), arg1); ExprTypeAs(Number(_, NumberInfo.IsEnum _), arg2) ] ->
Helper.LibCall(com, "String", "splitStrings", t, [ c; arg1; arg2 ], ?loc = r)
|> Some
| [ Value(NewArray(ArrayValues [ arg1 ], String, _), _)
ExprTypeAs(Number(Int32, _), arg2)
ExprTypeAs(Number(_, NumberInfo.IsEnum _), arg3) ] ->
Expand Down
8 changes: 8 additions & 0 deletions src/fable-library-rust/src/String.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,14 @@ pub mod String_ {
withSplitOptions(a, count, options)
}

pub fn splitStrings(s: string, ps: Array<string>, options: i32) -> Array<string> {
let mut a: Vec<&str> = vec![s.as_str()];
for p in ps.iter() {
a = a.iter().flat_map(|&s| s.split(p.as_str())).collect();
}
withSplitOptions(a, -1, options)
}

pub fn toCharArray(s: string) -> Array<char> {
array_from(s.chars().collect())
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Rust/tests/src/StringTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -895,10 +895,10 @@ let ``String.Split with single separator works`` () =
let ``String.Split with multiple char args works`` () =
"a;b,c".Split(',', ';') |> equal [|"a"; "b"; "c"|]

// [<Fact>]
// let ``String.Split with string array works`` () =
// "a;b,c".Split([|","; ";"|], StringSplitOptions.None)
// |> equal [|"a"; "b"; "c"|]
[<Fact>]
let ``String.Split with string array works`` () =
"a;b,c".Split([|","; ";"|], StringSplitOptions.None)
|> equal [|"a"; "b"; "c"|]

[<Fact>]
let ``String.Split with RemoveEmptyEntries works`` () =
Expand Down

0 comments on commit 69c237f

Please sign in to comment.