Skip to content
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 src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [Python] Fix type annotations for protocols, ABCs, Atom, and Set module (by @dbrattli)
* [Python] Fix type annotations for async functions, date operations, and None handling (by @dbrattli)
* [Python] Fix type annotations for tuple indexing, generic defaults, and reflection (by @dbrattli)
* [All] Fix `StringBuilder.Chars` getter and setter (by @MangelMaxime)

### Removed

Expand Down
1 change: 1 addition & 0 deletions src/Fable.Compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [Python] Fix type annotations for protocols, ABCs, Atom, and Set module (by @dbrattli)
* [Python] Fix type annotations for async functions, date operations, and None handling (by @dbrattli)
* [Python] Fix type annotations for tuple indexing, generic defaults, and reflection (by @dbrattli)
* [All] Fix `StringBuilder.Chars` getter and setter (by @MangelMaxime)

## 5.0.0-alpha.21 - 2025-12-26

Expand Down
26 changes: 13 additions & 13 deletions src/fable-library-dart/System.Text.fs
Original file line number Diff line number Diff line change
Expand Up @@ -101,35 +101,35 @@ type StringBuilder(value: string, capacity: int) =

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

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

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

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

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

if index < 0 || i < 0 || i >= buf.Count then
if index < 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) ..]
let pos = index - len
buf[i] <- buf[i][0 .. (pos - 1)] + (string<char> value) + buf[i][(pos + 1) ..]

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

for i = buf.Count - 1 downto 0 do
buf[i] <- buf[i].Replace(oldValue, newValue)
Expand Down
26 changes: 13 additions & 13 deletions src/fable-library-py/fable_library/System.Text.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ type StringBuilder(value: string, capacity: int) =
new() = StringBuilder("", 16)

member x.Append(s: string | null) =
buf.Add(string s)
buf.Add(string<string | null> s)
x

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

member x.Append(c: char) =
Expand Down Expand Up @@ -104,31 +104,31 @@ type StringBuilder(value: string, capacity: int) =

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

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

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

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

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

if index < 0 || i < 0 || i >= buf.Count then
if index < 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) ..]
let pos = index - len
buf[i] <- buf[i][0 .. (pos - 1)] + (string<char> value) + buf[i][(pos + 1) ..]

member x.Replace(oldValue: char, newValue: char) =
for i = buf.Count - 1 downto 0 do
Expand Down
50 changes: 25 additions & 25 deletions src/fable-library-rust/src/System.Text.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ type StringBuilder(value: string, capacity: int) =
buf.Add(s)
x

member x.Append(o: bool) = x.Append(string o)
member x.Append(c: char) = x.Append(string c)
member x.Append(o: bool) = x.Append(string<bool> o)
member x.Append(c: char) = x.Append(string<char> c)

member x.Append(c: char, repeatCount: int) =
let s = String.replicate repeatCount (string<char> c)
buf.Add(s)
x

member x.Append(o: int8) = x.Append(string o)
member x.Append(o: byte) = x.Append(string o)
member x.Append(o: int16) = x.Append(string o)
member x.Append(o: uint16) = x.Append(string o)
member x.Append(o: int32) = x.Append(string o)
member x.Append(o: uint32) = x.Append(string o)
member x.Append(o: int64) = x.Append(string o)
member x.Append(o: uint64) = x.Append(string o)
member x.Append(o: float32) = x.Append(string o)
member x.Append(o: float) = x.Append(string o)
member x.Append(o: int8) = x.Append(string<int8> o)
member x.Append(o: byte) = x.Append(string<byte> o)
member x.Append(o: int16) = x.Append(string<int16> o)
member x.Append(o: uint16) = x.Append(string<uint16> o)
member x.Append(o: int32) = x.Append(string<int32> o)
member x.Append(o: uint32) = x.Append(string<uint32> o)
member x.Append(o: int64) = x.Append(string<int64> o)
member x.Append(o: uint64) = x.Append(string<uint64> o)
member x.Append(o: float32) = x.Append(string<float32> o)
member x.Append(o: float) = x.Append(string<float> o)

member x.Append(s: string, index: int, count: int) = x.Append(s.Substring(index, count))

Expand All @@ -57,31 +57,31 @@ type StringBuilder(value: string, capacity: int) =

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

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

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

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

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

if index < 0 || i < 0 || i >= buf.Count then
if index < 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) ..]
let pos = index - len
buf[i] <- buf[i][0 .. (pos - 1)] + (string<char> value) + buf[i][(pos + 1) ..]

member x.Length =
let mutable len = 0
Expand All @@ -92,8 +92,8 @@ type StringBuilder(value: string, capacity: int) =
len

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

for i = buf.Count - 1 downto 0 do
buf[i] <- buf[i].Replace(oldValue, newValue)
Expand Down
26 changes: 13 additions & 13 deletions src/fable-library-ts/System.Text.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ type StringBuilder(value: string, capacity: int) =
new() = StringBuilder("", 16)

member x.Append(s: string | null) =
buf.Add(string s)
buf.Add(string<string | null> s)
x

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

member x.Append(c: char) =
Expand Down Expand Up @@ -101,31 +101,31 @@ type StringBuilder(value: string, capacity: int) =

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

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

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

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

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

if index < 0 || i < 0 || i >= buf.Count then
if index < 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) ..]
let pos = index - len
buf[i] <- buf[i][0 .. (pos - 1)] + (string<char> value) + buf[i][(pos + 1) ..]

member x.Replace(oldValue: char, newValue: char) =
for i = buf.Count - 1 downto 0 do
Expand Down
16 changes: 14 additions & 2 deletions tests/Dart/src/StringTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,20 @@ let tests() =
testCase "StringBuilder.Chars works" <| fun () ->
let sb = System.Text.StringBuilder()
.Append("abc")
.Append("def")
sb.Chars(0) |> equal 'a'
sb.Chars(1) |> equal 'b'
sb.Chars(2) |> equal 'c'
sb.Chars(3) |> equal 'd'
sb.Chars(4) |> equal 'e'
sb.Chars(5) |> equal 'f'

testCase "StringBuilder.Chars throws when index is out of bounds" <| fun () ->
let sb = System.Text.StringBuilder()
.Append("abc")
throwsAnyError <| fun () ->
let sb = System.Text.StringBuilder()
.Append("abc")
sb.Chars(-1) |> ignore
throwsAnyError <| fun () ->
sb.Chars(3) |> ignore

testCase "StringBuilder.Replace works" <| fun () ->
Expand All @@ -173,6 +180,11 @@ let tests() =
sb[1] <- 'x'
sb.ToString() |> equal "axc"

throwsAnyError <| fun () ->
sb[-1] <- 'y'
throwsAnyError <| fun () ->
sb[3] <- 'z'

// Formatting

// testCase "kprintf works" <| fun () ->
Expand Down
15 changes: 13 additions & 2 deletions tests/Js/Main/StringTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,20 @@ let tests = testList "Strings" [
testCase "StringBuilder.Chars works" <| fun () ->
let sb = System.Text.StringBuilder()
.Append("abc")
.Append("def")
sb.Chars(0) |> equal 'a'
sb.Chars(1) |> equal 'b'
sb.Chars(2) |> equal 'c'
sb.Chars(3) |> equal 'd'
sb.Chars(4) |> equal 'e'
sb.Chars(5) |> equal 'f'

testCase "StringBuilder.Chars throws when index is out of bounds" <| fun () ->
let sb = System.Text.StringBuilder()
.Append("abc")
throwsAnyError <| fun () ->
let sb = System.Text.StringBuilder()
.Append("abc")
sb.Chars(-1) |> ignore
throwsAnyError <| fun () ->
sb.Chars(3) |> ignore

testCase "StringBuilder.Replace works" <| fun () ->
Expand All @@ -150,6 +157,10 @@ let tests = testList "Strings" [
.Append("abc")
sb[1] <- 'x'
sb.ToString() |> equal "axc"
throwsAnyError <| fun () ->
sb[-1] <- 'y'
throwsAnyError <| fun () ->
sb[3] <- 'z'

// Formatting

Expand Down
15 changes: 13 additions & 2 deletions tests/Php/TestString.fs
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,21 @@ let ``StringBuilder.AppendFormat with provider works`` () =
let ``StringBuilder.Chars works`` () =
let sb = System.Text.StringBuilder()
.Append("abc")
.Append("def")
sb.Chars(0) |> equal 'a'
sb.Chars(1) |> equal 'b'
sb.Chars(2) |> equal 'c'
sb.Chars(3) |> equal 'd'
sb.Chars(4) |> equal 'e'
sb.Chars(5) |> equal 'f'

[<Fact>]
let ``StringBuilder.Chars throws when index is out of bounds`` () =
let sb = System.Text.StringBuilder()
.Append("abc")
throwsAnyError <| fun () ->
let sb = System.Text.StringBuilder()
.Append("abc")
sb.Chars(-1) |> ignore
throwsAnyError <| fun () ->
sb.Chars(3) |> ignore

[<Fact>]
Expand All @@ -249,6 +256,10 @@ let ``StringBuilder index setter works`` () =
.Append("abc")
sb[1] <- 'x'
sb.ToString() |> equal "axc"
throwsAnyError <| fun () ->
sb[-1] <- 'y'
throwsAnyError <| fun () ->
sb[3] <- 'z'

[<Fact>]
let ``test Conversion char to int works`` () =
Expand Down
Loading
Loading