Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rust] Added StringBuilder.Chars #3761

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ project.lock.json
# Python
.eggs/
__pycache__/
.pytest_cache
.python-version
.hypothesis

Expand Down
4 changes: 2 additions & 2 deletions src/Fable.Transforms/Dart/Fable2Dart.fs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ module Util =
Expression.listLiteral (values, typ)

let tryGetEntityIdent (com: IDartCompiler) ctx ent =
Dart.Replacements.tryEntityIdent com ent
Dart.Replacements.tryConstructor com ent
|> Option.bind (fun entRef ->
match transformAndCaptureExpr com ctx entRef with
| [], IdentExpression ident -> Some ident
Expand All @@ -187,7 +187,7 @@ module Util =
match tryGetEntityIdent com ctx ent with
| Some ident -> ident
| None ->
addError com [] None $"Cannot find reference for {ent.FullName}"
// addError com [] None $"Cannot find reference for {ent.FullName}"
makeImmutableIdent MetaType ent.DisplayName

let transformTupleType com ctx genArgs =
Expand Down
18 changes: 9 additions & 9 deletions src/Fable.Transforms/Dart/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ let injectArg (com: ICompiler) (ctx: Context) r moduleName methName (genArgs: Ty
|> Option.bind (injectArgInner args)
|> Option.defaultValue args

let tryReplacedEntityRef (com: Compiler) entFullName =
let tryEntityIdent (com: Compiler) entFullName =
match entFullName with
| "Fable.Core.Dart.Future`1" -> makeIdentExpr "Future" |> Some
| "Fable.Core.Dart.Stream`1" -> makeIdentExpr "Stream" |> Some
Expand Down Expand Up @@ -683,16 +683,16 @@ let tryReplacedEntityRef (com: Compiler) entFullName =
| "System.Lazy`1" -> makeImportLib com MetaType "Lazy" "FSharp.Core" |> Some
| _ -> None

let tryEntityIdent com (ent: Entity) =
let tryConstructor com (ent: Entity) =
if FSharp2Fable.Util.isReplacementCandidate ent.Ref then
tryReplacedEntityRef com ent.FullName
tryEntityIdent com ent.FullName
else
FSharp2Fable.Util.tryEntityIdentMaybeGlobalOrImported com ent

let entityIdent com ent =
match tryEntityIdent com ent with
let constructor com ent =
match tryConstructor com ent with
| Some r -> r
| None -> $"Cannot find {ent.FullName} reference" |> addErrorAndReturnNull com [] None
| None -> $"Cannot find %s{ent.FullName} constructor" |> addErrorAndReturnNull com [] None

let tryOp com r t op args =
Helper.LibCall(com, "Option", "tryOp", t, op :: args, ?loc = r)
Expand Down Expand Up @@ -2370,7 +2370,7 @@ let intrinsicFunctions (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisAr
match genArg com ctx r 0 i.GenericArgs with
| DeclaredType(ent, _) ->
let ent = com.GetEntity(ent)
Helper.ConstructorCall(entityIdent com ent, t, [], ?loc = r) |> Some
Helper.ConstructorCall(constructor com ent, t, [], ?loc = r) |> Some
| t ->
$"Cannot create instance of type unresolved at compile time: %A{t}"
|> addErrorAndReturnNull com ctx.InlinePath r
Expand Down Expand Up @@ -4113,8 +4113,8 @@ let tryBaseConstructor com ctx (ent: EntityRef) (argTypes: Lazy<Type list>) genA
| _ -> None
| _ -> None

let tryType =
function
let tryType typ =
match typ with
| Boolean -> Some(Types.bool, parseBool, [])
| Number(kind, info) ->
let f =
Expand Down
9 changes: 3 additions & 6 deletions src/Fable.Transforms/Python/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -812,10 +812,7 @@ let tryConstructor com (ent: Entity) =
let constructor com ent =
match tryConstructor com ent with
| Some e -> e
| None ->
ent.FullName
|> sprintf "Cannot find %s constructor"
|> addErrorAndReturnNull com [] None
| None -> $"Cannot find %s{ent.FullName} constructor" |> addErrorAndReturnNull com [] None

let tryOp com r t op args =
Helper.LibCall(com, "option", "tryOp", t, op :: args, ?loc = r)
Expand Down Expand Up @@ -3717,8 +3714,8 @@ let tryBaseConstructor com ctx (ent: EntityRef) (argTypes: Lazy<Type list>) genA
Some(makeImportLib com Any entityName "MutableSet", args)
| _ -> None

let tryType =
function
let tryType typ =
match typ with
| Boolean -> Some(Types.bool, parseBool, [])
| Number(kind, info) ->
let f =
Expand Down
12 changes: 3 additions & 9 deletions src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,13 @@ let coreModFor =

let makeDecimal com r t (x: decimal) =
let str = x.ToString(System.Globalization.CultureInfo.InvariantCulture)

Helper.LibCall(com, "Decimal", "default", t, [ makeStrConst str ], isConstructor = true, ?loc = r)

let makeDecimalFromExpr com r t (e: Expr) =
Helper.LibCall(com, "Decimal", "default", t, [ e ], isConstructor = true, ?loc = r)

let createAtom com (value: Expr) =
let typ = value.Type

Helper.LibCall(com, "Util", "createAtom", typ, [ value ], [ typ ], genArgs = [ typ ])

let getRefCell com r typ (expr: Expr) = getFieldWith r typ expr "contents"
Expand All @@ -111,7 +109,6 @@ let setRefCell com r (expr: Expr) (value: Expr) =

let makeRefCell com r genArg args =
let typ = makeFSharpCoreType [ genArg ] Types.refCell

Helper.LibCall(com, "Types", "FSharpRef", typ, args, isConstructor = true, ?loc = r)

let makeRefCellFromValue com r (value: Expr) =
Expand Down Expand Up @@ -859,10 +856,7 @@ let tryConstructor com (ent: Entity) =
let constructor com ent =
match tryConstructor com ent with
| Some e -> e
| None ->
ent.FullName
|> sprintf "Cannot find %s constructor"
|> addErrorAndReturnNull com [] None
| None -> $"Cannot find %s{ent.FullName} constructor" |> addErrorAndReturnNull com [] None

let tryOp com r t op args =
Helper.LibCall(com, "Option", "tryOp", t, op :: args, ?loc = r)
Expand Down Expand Up @@ -4026,8 +4020,8 @@ let tryBaseConstructor com ctx (ent: EntityRef) (argTypes: Lazy<Type list>) genA
| _ -> None
| _ -> None

let tryType =
function
let tryType typ =
match typ with
| Boolean -> Some(Types.bool, parseBool, [])
| Number(kind, info) ->
let f =
Expand Down
1 change: 1 addition & 0 deletions src/fable-library-dart/Fable.Library.Dart.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<Compile Include="Set.fs" />
<Compile Include="Map.fs" />
<Compile Include="Range.fs" />
<Compile Include="System.Text.fs" />
<Content Include="Date.dart" />
<Content Include="RegExp.dart" />
<Content Include="String.dart" />
Expand Down
150 changes: 150 additions & 0 deletions src/fable-library-dart/System.Text.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
namespace System.Text

type StringBuilder(value: string, capacity: int) =
let buf = ResizeArray<string>(capacity)

do
if not (System.String.IsNullOrEmpty value) then
buf.Add(value)

new(capacity: int) = StringBuilder("", capacity)
new(value: string) = StringBuilder(value, 16)
new() = StringBuilder("", 16)

member x.Append(s: string) =
buf.Add(s)
x

member x.Append(s: string, startIndex: int, count: int) =
buf.Add(s.Substring(startIndex, count))
x

member x.Append(c: char) =
buf.Add(string<char> c)
x

member x.Append(o: int) =
buf.Add(string<int> o)
x

member x.Append(o: float) =
buf.Add(string<float> o)
x

member x.Append(o: bool) =
buf.Add(string<bool> o)
x

member x.Append(o: obj) =
buf.Add(string<obj> o)
x

member x.Append(cs: char[]) =
buf.Add(System.String(cs))
x

member x.Append(s: StringBuilder) =
buf.Add(s.ToString())
x

// member x.AppendFormat(fmt: string, o: obj) =
// buf.Add(System.String.Format(fmt, o))
// x

// member x.AppendFormat(fmt: string, o1: obj, o2: obj) =
// buf.Add(System.String.Format(fmt, o1, o2))
// x

// member x.AppendFormat(fmt: string, o1: obj, o2: obj, o3: obj) =
// buf.Add(System.String.Format(fmt, o1, o2, o3))
// x

// member x.AppendFormat(fmt: string, arr: obj[]) =
// buf.Add(System.String.Format(fmt, arr))
// x

// member x.AppendFormat(provider: System.IFormatProvider, fmt: string, o: obj) =
// buf.Add(System.String.Format(provider, fmt, o))
// x

// member x.AppendFormat(provider: System.IFormatProvider, fmt: string, o1: obj, o2: obj) =
// buf.Add(System.String.Format(provider, fmt, o1, o2))
// x

// member x.AppendFormat(provider: System.IFormatProvider, fmt: string, o1: obj, o2: obj, o3: obj) =
// buf.Add(System.String.Format(provider, fmt, o1, o2, o3))
// x

// member x.AppendFormat(provider: System.IFormatProvider, fmt: string, arr: obj[]) =
// buf.Add(System.String.Format(provider, fmt, arr))
// x

member x.AppendLine() =
buf.Add(System.Environment.NewLine)
x

member x.AppendLine(s: string) =
buf.Add(s)
buf.Add(System.Environment.NewLine)
x

member x.Clear() =
buf.Clear()
x

member x.Chars

with get (index: int) =
let mutable len = 0
let mutable i = -1

while i + 1 < buf.Count && len < index do
i <- i + 1
len <- len + buf[i].Length

if index < 0 || i < 0 || i >= buf.Count then
failwith "Index was outside the bounds of the array"
else
let pos = len - index - 1
buf[i][pos]

and set (index: int) (value: char) =
let mutable len = 0
let mutable i = -1

while i + 1 < buf.Count && len < index do
i <- i + 1
len <- len + buf[i].Length

if index < 0 || i < 0 || i >= buf.Count then
failwith "Index was outside the bounds of the array"
else
let pos = len - index - 1
buf[i] <- buf[i][0 .. (pos - 1)] + (string value) + buf[i][(pos + 1) ..]

member x.Replace(oldValue: char, newValue: char) =
let oldValue = string oldValue
let newValue = string newValue

for i = buf.Count - 1 downto 0 do
buf[i] <- buf[i].Replace(oldValue, newValue)

x

member x.Replace(oldValue: string, newValue: string) =
let str = x.ToString().Replace(oldValue, newValue)
x.Clear().Append(str)

member x.Length =
let mutable len = 0

for i = buf.Count - 1 downto 0 do
len <- len + buf[i].Length

len

override _.ToString() = System.String.Concat(buf)

member x.ToString(firstIndex: int, length: int) =
let str = x.ToString()
str.Substring(firstIndex, length)
55 changes: 54 additions & 1 deletion src/fable-library-rust/src/System.Text.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,69 @@ type StringBuilder(value: string, capacity: int) =
member x.Append(cs: char[]) = x.Append(System.String(cs))
member x.Append(sb: StringBuilder) = x.Append(sb.ToString())
// member x.Append(o: obj) = x.Append(string o)

// see Replacements for AppendFormat
// member x.AppendFormat(fmt: string, o: obj) = x.Append(System.String.Format(fmt, o))
// member x.AppendFormat(provider: System.IFormatProvider, fmt: string, o: obj) = x.Append(System.String.Format(provider, fmt, o))

member x.AppendLine() = x.Append(System.Environment.NewLine)
member x.AppendLine(s: string) = x.Append(s).AppendLine()

member x.Clear() =
buf.Clear()
x

member x.Length = buf |> asArray |> Array.sumBy (fun s -> s.Length)
member x.Chars

with get (index: int) =
let mutable len = 0
let mutable i = -1

while i + 1 < buf.Count && len < index do
i <- i + 1
len <- len + buf[i].Length

if index < 0 || i < 0 || i >= buf.Count then
failwith "Index was outside the bounds of the array"
else
let pos = len - index - 1
buf[i][pos]

and set (index: int) (value: char) =
let mutable len = 0
let mutable i = -1

while i + 1 < buf.Count && len < index do
i <- i + 1
len <- len + buf[i].Length

if index < 0 || i < 0 || i >= buf.Count then
failwith "Index was outside the bounds of the array"
else
let pos = len - index - 1
buf[i] <- buf[i][0 .. (pos - 1)] + (string value) + buf[i][(pos + 1) ..]

member x.Length =
let mutable len = 0

for i = buf.Count - 1 downto 0 do
len <- len + buf[i].Length

len

member x.Replace(oldValue: char, newValue: char) =
let oldValue = string oldValue
let newValue = string newValue

for i = buf.Count - 1 downto 0 do
buf[i] <- buf[i].Replace(oldValue, newValue)

x

member x.Replace(oldValue: string, newValue: string) =
let str = x.ToString().Replace(oldValue, newValue)
x.Clear().Append(str)

override _.ToString() = System.String.Concat(buf |> asArray)

member x.ToString(index: int, count: int) = x.ToString().Substring(index, count)
Loading
Loading