Skip to content

Commit

Permalink
[JavaScript] Add supports of StringBuilder.Chars
Browse files Browse the repository at this point in the history
Fix #3759
  • Loading branch information
MangelMaxime committed Feb 18, 2024
1 parent ea490e9 commit e1b1954
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

#### JavaScript

* [GH-3759](https://github.com/fable-compiler/Fable/issues/3759) Add `StringBuilder.Chars` (by @MangelMaxime)

## 4.12.2 - 2024-02-13

### Changed
Expand Down
6 changes: 6 additions & 0 deletions src/fable-library-ts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

#### JavaScript

* [GH-3759](https://github.com/fable-compiler/Fable/issues/3759) Add `StringBuilder.Chars` (by @MangelMaxime)

## 1.0.0 - 2024-02-13

* Release stable version
Expand Down
26 changes: 22 additions & 4 deletions src/fable-library-ts/System.Text.fs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ type StringBuilder(value: string, capacity: int) =
buf.Add(System.Environment.NewLine)
x

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

member x.Chars
with get (index: int) =
let bufferText = x.ToString()

if index < 0 || index >= bufferText.Length then
failwith "Index was outside the bounds of the array"
else
bufferText.[index]
and set (index: int) (value: char) =
let bufferText = x.ToString()

if index < 0 || index >= bufferText.Length then
failwith "Index was outside the bounds of the array"
else
let bufferText = bufferText.ToCharArray()
bufferText.[index] <- value
x.Clear().Append(bufferText) |> ignore

member x.Replace(oldValue: char, newValue: char) =
for i = buf.Count - 1 downto 0 do
buf[i] <- buf[i].Replace(oldValue, newValue)
Expand All @@ -89,7 +111,3 @@ type StringBuilder(value: string, capacity: int) =
member x.ToString(firstIndex: int, length: int) =
let str = x.ToString()
str.Substring(firstIndex, length)

member x.Clear() =
buf.Clear()
x
32 changes: 32 additions & 0 deletions tests/Js/Main/StringTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,38 @@ let tests =
.Append(34)
equal "aaabcd/true5.234" (builder.ToString().Replace(",", ".").ToLower())

testCase "StringBuilder.Chars works" <| fun () ->
let builder = Text.StringBuilder()
.Append("abc")
equal 'b' (builder.Chars(1))

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

builder.Chars(3) |> ignore

testCase "StringBuilder.Replace works" <| fun () ->
let builder = Text.StringBuilder()
.Append("abc")
.Append("abc")
.Replace('a', 'x')
.Replace("bc", "yz")
equal "xyzxyz" (builder.ToString())

testCase "StringBuilder index accessor works" <| fun () ->
let builder = Text.StringBuilder()
.Append("abc")
equal 'b' (builder[1])

testCase "StringBuilder index setter works" <| fun () ->
let builder = Text.StringBuilder()
.Append("abc")
builder[1] <- 'x'
equal "axc" (builder.ToString())

testCase "kprintf works" <| fun () ->
let f (s:string) = s + "XX"
Printf.kprintf f "hello" |> equal "helloXX"
Expand Down

0 comments on commit e1b1954

Please sign in to comment.