Skip to content

Commit d24afe0

Browse files
committed
Merge pull request #1205 from l15k4/replace-deleteCharAt-StringBuilder
Add replace and deleteCharAt methods to StringBuilder and StringBuffer
2 parents 79944cd + 20973bc commit d24afe0

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

javalanglib/src/main/scala/java/lang/StringBuffer.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,28 @@ class StringBuffer(private var content: String) extends CharSequence
6767
this
6868
}
6969

70+
def deleteCharAt(index: Int): StringBuffer = {
71+
if (index < 0 || index >= content.length)
72+
throw new StringIndexOutOfBoundsException("String index out of range: " + index)
73+
content = content.substring(0, index) + content.substring(index+1)
74+
this
75+
}
76+
77+
/**
78+
* @param start The beginning index, inclusive.
79+
* @param end The ending index, exclusive.
80+
* @param str String that will replace previous contents.
81+
* @return This StringBuilder.
82+
*/
83+
def replace(start: Int, end: Int, str: String): StringBuffer = {
84+
val length = content.length
85+
if (start < 0 || start > end || start >= length)
86+
throw new StringIndexOutOfBoundsException(s"Illegal to replace substring at [$start - $end] in string of length $length")
87+
val realEnd = if (end > length) length else end // java api convention
88+
content = content.substring(0, start) + str + content.substring(realEnd)
89+
this
90+
}
91+
7092
def setCharAt(index: Int, ch: scala.Char): Unit = {
7193
if (index < 0 || index >= content.length)
7294
throw new IndexOutOfBoundsException("String index out of range: " + index)

javalanglib/src/main/scala/java/lang/StringBuilder.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,28 @@ class StringBuilder(private var content: String) extends CharSequence
8686
this
8787
}
8888

89+
def deleteCharAt(index: Int): StringBuilder = {
90+
if (index < 0 || index >= content.length)
91+
throw new StringIndexOutOfBoundsException("String index out of range: " + index)
92+
content = content.substring(0, index) + content.substring(index+1)
93+
this
94+
}
95+
96+
/**
97+
* @param start The beginning index, inclusive.
98+
* @param end The ending index, exclusive.
99+
* @param str String that will replace previous contents.
100+
* @return This StringBuilder.
101+
*/
102+
def replace(start: Int, end: Int, str: String): StringBuilder = {
103+
val length = content.length
104+
if (start < 0 || start > end || start >= length)
105+
throw new StringIndexOutOfBoundsException(s"Illegal to replace substring at [$start - $end] in string of length $length")
106+
val realEnd = if (end > length) length else end // java api convention
107+
content = content.substring(0, start) + str + content.substring(realEnd)
108+
this
109+
}
110+
89111
def setCharAt(index: Int, ch: scala.Char): Unit = {
90112
if (index < 0 || index >= content.length)
91113
throw new IndexOutOfBoundsException("String index out of range: " + index)

test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StringBufferTest.scala

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,26 @@ object StringBufferTest extends JasmineTest {
7171
shouldThrow[StringIndexOutOfBoundsException](initBuf("abcd").insert(-1, "whatever"))
7272
}
7373

74+
it("should respond to `deleteCharAt`") {
75+
expect(initBuf("0123").deleteCharAt(1).toString).toEqual("023")
76+
expect(initBuf("0123").deleteCharAt(0).toString).toEqual("123")
77+
expect(initBuf("0123").deleteCharAt(3).toString).toEqual("012")
78+
shouldThrow[StringIndexOutOfBoundsException](initBuf("0123").deleteCharAt(-1))
79+
shouldThrow[StringIndexOutOfBoundsException](initBuf("0123").deleteCharAt(4))
80+
}
81+
82+
it("should respond to `replace`") {
83+
expect(initBuf("0123").replace(1,3,"bc").toString).toEqual("0bc3")
84+
expect(initBuf("0123").replace(0,4,"abcd").toString).toEqual("abcd")
85+
expect(initBuf("0123").replace(0,10,"abcd").toString).toEqual("abcd")
86+
expect(initBuf("0123").replace(3,10,"defg").toString).toEqual("012defg")
87+
expect(initBuf("0123").replace(0,1,"xxxx").toString).toEqual("xxxx123")
88+
expect(initBuf("0123").replace(1,1,"xxxx").toString).toEqual("0xxxx123")
89+
90+
shouldThrow[StringIndexOutOfBoundsException](initBuf("0123").replace(-1,3,"x"))
91+
shouldThrow[StringIndexOutOfBoundsException](initBuf("0123").replace(4,5,"x"))
92+
}
93+
7494
it("should respond to `setCharAt`") {
7595
val buf = newBuf
7696
buf.append("foobar")
@@ -151,6 +171,26 @@ object StringBufferTest extends JasmineTest {
151171
expect(s"${js.undefined}").toEqual("undefined")
152172
}
153173

174+
it("should respond to `deleteCharAt`") {
175+
expect(initBuilder("0123").deleteCharAt(1).toString).toEqual("023")
176+
expect(initBuilder("0123").deleteCharAt(0).toString).toEqual("123")
177+
expect(initBuilder("0123").deleteCharAt(3).toString).toEqual("012")
178+
shouldThrow[StringIndexOutOfBoundsException](initBuilder("0123").deleteCharAt(-1))
179+
shouldThrow[StringIndexOutOfBoundsException](initBuilder("0123").deleteCharAt(4))
180+
}
181+
182+
it("should respond to `replace`") {
183+
expect(initBuilder("0123").replace(1,3,"bc").toString).toEqual("0bc3")
184+
expect(initBuilder("0123").replace(0,4,"abcd").toString).toEqual("abcd")
185+
expect(initBuilder("0123").replace(0,10,"abcd").toString).toEqual("abcd")
186+
expect(initBuilder("0123").replace(3,10,"defg").toString).toEqual("012defg")
187+
expect(initBuilder("0123").replace(0,1,"xxxx").toString).toEqual("xxxx123")
188+
expect(initBuilder("0123").replace(1,1,"xxxx").toString).toEqual("0xxxx123")
189+
190+
shouldThrow[StringIndexOutOfBoundsException](initBuilder("0123").replace(-1,3,"x"))
191+
shouldThrow[StringIndexOutOfBoundsException](initBuilder("0123").replace(4,5,"x"))
192+
}
193+
154194
it("should respond to `setCharAt`") {
155195
val b = newBuilder
156196
b.append("foobar")

0 commit comments

Comments
 (0)