diff --git a/javalanglib/src/main/scala/java/lang/StringBuffer.scala b/javalanglib/src/main/scala/java/lang/StringBuffer.scala index 317e373aef..31ee89a40c 100644 --- a/javalanglib/src/main/scala/java/lang/StringBuffer.scala +++ b/javalanglib/src/main/scala/java/lang/StringBuffer.scala @@ -67,6 +67,28 @@ class StringBuffer(private var content: String) extends CharSequence this } + def deleteCharAt(index: Int): StringBuffer = { + if (index < 0 || index >= content.length) + throw new StringIndexOutOfBoundsException("String index out of range: " + index) + content = content.substring(0, index) + content.substring(index+1) + this + } + + /** + * @param start The beginning index, inclusive. + * @param end The ending index, exclusive. + * @param str String that will replace previous contents. + * @return This StringBuilder. + */ + def replace(start: Int, end: Int, str: String): StringBuffer = { + val length = content.length + if (start < 0 || start > end || start >= length) + throw new StringIndexOutOfBoundsException(s"Illegal to replace substring at [$start - $end] in string of length $length") + val realEnd = if (end > length) length else end // java api convention + content = content.substring(0, start) + str + content.substring(realEnd) + this + } + def setCharAt(index: Int, ch: scala.Char): Unit = { if (index < 0 || index >= content.length) throw new IndexOutOfBoundsException("String index out of range: " + index) diff --git a/javalanglib/src/main/scala/java/lang/StringBuilder.scala b/javalanglib/src/main/scala/java/lang/StringBuilder.scala index e8d9cdbbe9..e8bd2b77df 100644 --- a/javalanglib/src/main/scala/java/lang/StringBuilder.scala +++ b/javalanglib/src/main/scala/java/lang/StringBuilder.scala @@ -86,6 +86,28 @@ class StringBuilder(private var content: String) extends CharSequence this } + def deleteCharAt(index: Int): StringBuilder = { + if (index < 0 || index >= content.length) + throw new StringIndexOutOfBoundsException("String index out of range: " + index) + content = content.substring(0, index) + content.substring(index+1) + this + } + + /** + * @param start The beginning index, inclusive. + * @param end The ending index, exclusive. + * @param str String that will replace previous contents. + * @return This StringBuilder. + */ + def replace(start: Int, end: Int, str: String): StringBuilder = { + val length = content.length + if (start < 0 || start > end || start >= length) + throw new StringIndexOutOfBoundsException(s"Illegal to replace substring at [$start - $end] in string of length $length") + val realEnd = if (end > length) length else end // java api convention + content = content.substring(0, start) + str + content.substring(realEnd) + this + } + def setCharAt(index: Int, ch: scala.Char): Unit = { if (index < 0 || index >= content.length) throw new IndexOutOfBoundsException("String index out of range: " + index) diff --git a/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StringBufferTest.scala b/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StringBufferTest.scala index 85409a541d..a034ce67d5 100644 --- a/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StringBufferTest.scala +++ b/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/StringBufferTest.scala @@ -71,6 +71,26 @@ object StringBufferTest extends JasmineTest { shouldThrow[StringIndexOutOfBoundsException](initBuf("abcd").insert(-1, "whatever")) } + it("should respond to `deleteCharAt`") { + expect(initBuf("0123").deleteCharAt(1).toString).toEqual("023") + expect(initBuf("0123").deleteCharAt(0).toString).toEqual("123") + expect(initBuf("0123").deleteCharAt(3).toString).toEqual("012") + shouldThrow[StringIndexOutOfBoundsException](initBuf("0123").deleteCharAt(-1)) + shouldThrow[StringIndexOutOfBoundsException](initBuf("0123").deleteCharAt(4)) + } + + it("should respond to `replace`") { + expect(initBuf("0123").replace(1,3,"bc").toString).toEqual("0bc3") + expect(initBuf("0123").replace(0,4,"abcd").toString).toEqual("abcd") + expect(initBuf("0123").replace(0,10,"abcd").toString).toEqual("abcd") + expect(initBuf("0123").replace(3,10,"defg").toString).toEqual("012defg") + expect(initBuf("0123").replace(0,1,"xxxx").toString).toEqual("xxxx123") + expect(initBuf("0123").replace(1,1,"xxxx").toString).toEqual("0xxxx123") + + shouldThrow[StringIndexOutOfBoundsException](initBuf("0123").replace(-1,3,"x")) + shouldThrow[StringIndexOutOfBoundsException](initBuf("0123").replace(4,5,"x")) + } + it("should respond to `setCharAt`") { val buf = newBuf buf.append("foobar") @@ -151,6 +171,26 @@ object StringBufferTest extends JasmineTest { expect(s"${js.undefined}").toEqual("undefined") } + it("should respond to `deleteCharAt`") { + expect(initBuilder("0123").deleteCharAt(1).toString).toEqual("023") + expect(initBuilder("0123").deleteCharAt(0).toString).toEqual("123") + expect(initBuilder("0123").deleteCharAt(3).toString).toEqual("012") + shouldThrow[StringIndexOutOfBoundsException](initBuilder("0123").deleteCharAt(-1)) + shouldThrow[StringIndexOutOfBoundsException](initBuilder("0123").deleteCharAt(4)) + } + + it("should respond to `replace`") { + expect(initBuilder("0123").replace(1,3,"bc").toString).toEqual("0bc3") + expect(initBuilder("0123").replace(0,4,"abcd").toString).toEqual("abcd") + expect(initBuilder("0123").replace(0,10,"abcd").toString).toEqual("abcd") + expect(initBuilder("0123").replace(3,10,"defg").toString).toEqual("012defg") + expect(initBuilder("0123").replace(0,1,"xxxx").toString).toEqual("xxxx123") + expect(initBuilder("0123").replace(1,1,"xxxx").toString).toEqual("0xxxx123") + + shouldThrow[StringIndexOutOfBoundsException](initBuilder("0123").replace(-1,3,"x")) + shouldThrow[StringIndexOutOfBoundsException](initBuilder("0123").replace(4,5,"x")) + } + it("should respond to `setCharAt`") { val b = newBuilder b.append("foobar")