Skip to content

Add replace and deleteCharAt methods to StringBuilder and StringBuffer #1205

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

Merged
merged 1 commit into from
Oct 30, 2014
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
22 changes: 22 additions & 0 deletions javalanglib/src/main/scala/java/lang/StringBuffer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions javalanglib/src/main/scala/java/lang/StringBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down