Skip to content

Commit

Permalink
use Transformer Replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
goswinr committed Sep 18, 2024
1 parent 3c9c5c0 commit 72fe819
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

dotnet tool restore
dotnet run --project src/Fable.Build/Fable.Build.fsproj -- $@
# dotnet run --project src/Fable.Build/Fable.Build.fsproj -- test javascript
# dotnet run --project src/Fable.Build/Fable.Build.fsproj -- test typescript
10 changes: 6 additions & 4 deletions src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,6 @@ let implementedStringFunctions =
[|
"Compare"
"CompareTo"
"EndsWith"
"Format"
"IndexOfAny"
"Insert"
Expand Down Expand Up @@ -1503,12 +1502,15 @@ let strings (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr opt

let left = Helper.InstanceCall(c, "indexOf", Int32.Number, [ arg ])
makeEqOp r left (makeIntConst 0) BinaryGreaterOrEqual |> Some
| "StartsWith", Some c, [ _str ] ->
let left = Helper.InstanceCall(c, "indexOf", Int32.Number, args)
makeEqOp r left (makeIntConst 0) BinaryEqual |> Some
| "StartsWith", Some c, [ _str ] -> Helper.InstanceCall(c, "startsWith", Boolean, args) |> Some
| "StartsWith", Some c, [ _str; _comp ] ->
Helper.LibCall(com, "String", "startsWith", t, args, i.SignatureArgTypes, thisArg = c, ?loc = r)
|> Some
| "EndsWith", Some c, [ _str ] -> Helper.InstanceCall(c, "endsWith", Boolean, args) |> Some
| "EndsWith", Some c, [ _str; _comp ] ->
Helper.LibCall(com, "String", "endsWith", t, args, i.SignatureArgTypes, thisArg = c, ?loc = r)
|> Some

| ReplaceName [ "ToUpper", "toLocaleUpperCase"
"ToUpperInvariant", "toUpperCase"
"ToLower", "toLocaleLowerCase"
Expand Down
19 changes: 13 additions & 6 deletions src/fable-library-ts/String.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,25 @@ export function compareTo(x: string, y: string) {
}

export function startsWith(str: string, pattern: string, ic: number) {
if (ic=== StringComparison.Ordinal){ // to avoid substring allocation
return str.startsWith(pattern) ;
}
if (str.length >= pattern.length) {
return cmp(str.substr(0, pattern.length), pattern, ic) === 0;
}
return false;
}

export function endsWith(str: string, pattern: string, ic: number) {
if (ic=== StringComparison.Ordinal){ // to avoid substring allocation
return str.endsWith(pattern) ;
}
if (str.length >= pattern.length) {
return cmp(str.substr(str.length-pattern.length, pattern.length), pattern, ic) === 0;
}
return false;
}

export function indexOfAny(str: string, anyOf: string[], ...args: number[]) {
if (str == null || str === "") {
return -1;
Expand Down Expand Up @@ -374,12 +387,6 @@ export function format(str: string | object, ...args: any[]) {
});
}

export function endsWith(str: string, pattern: string, ic: number) {
if (str.length >= pattern.length) {
return cmp(str.substr(str.length-1-pattern.length, pattern.length), pattern, ic) === 0;
}
return false;
}

export function initialize(n: number, f: (i: number) => string) {
if (n < 0) {
Expand Down

0 comments on commit 72fe819

Please sign in to comment.