Skip to content
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

Fix #3408: Implement two javalib static String join() methods. #3420

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 26 additions & 7 deletions javalib/src/main/scala/java/lang/String.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import java.util._
import java.util.regex._
import java.nio._
import java.nio.charset._
import java.util.Objects
import java.util.{Objects, Arrays}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import?

Suggested change
import java.util.{Objects, Arrays}
import java.util.Objects

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tanishiking

Thank you for the review & suggestion. Good to have a second pair of eyes.

import java.util.ScalaOps._
import java.lang.constant.{Constable, ConstantDesc}
import scala.annotation.{switch, tailrec}
import _String.{string2_string, _string2string}
Expand Down Expand Up @@ -1553,6 +1554,30 @@ object _String {
def copyValueOf(data: Array[Char]): _String =
new _String(data, 0, data.length)

def format(fmt: _String, args: Array[AnyRef]): _String =
new Formatter().format(fmt, args).toString

def format(loc: Locale, fmt: _String, args: Array[AnyRef]): _String =
new Formatter(loc).format(fmt, args).toString()

def join(delimiter: CharSequence, elements: Array[CharSequence]): String = {
val sj = new StringJoiner(delimiter)

for (j <- 0 until elements.length)
sj.add(elements(j))

sj.toString()
}

def join(
delimiter: CharSequence,
elements: Iterable[CharSequence]
): String = {
elements.scalaOps
.foldLeft(new StringJoiner(delimiter))((j, e) => j.add(e))
.toString()
}

def valueOf(data: Array[Char]): _String = new _String(data)

def valueOf(data: Array[Char], start: Int, length: Int): _String =
Expand All @@ -1579,12 +1604,6 @@ object _String {
def valueOf(value: AnyRef): _String =
if (value != null) value.toString else "null"

def format(fmt: _String, args: Array[AnyRef]): _String =
new Formatter().format(fmt, args).toString

def format(loc: Locale, fmt: _String, args: Array[AnyRef]): _String =
new Formatter(loc).format(fmt, args).toString()

import scala.language.implicitConversions
@inline private[lang] implicit def _string2string(s: _String): String =
s.asInstanceOf[String]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -961,4 +961,42 @@ class StringTest {
)
}

/* selected Static methods
*/
@Test def joinVarargs(): Unit = {
val strings = Array("one", "two", "three")
val delimiter = "-%-"

val expected = s"${strings(0)}${delimiter}" +
s"${strings(1)}${delimiter}" +
s"${strings(2)}"
val joined = String.join(delimiter, strings(0), strings(1), strings(2))

assertEquals(
s"unexpected join",
expected,
joined
)
}

@Test def joinIterable(): Unit = {
val strings = new java.util.ArrayList[String](3)
strings.add("zeta")
strings.add("eta")
strings.add("theta")

val delimiter = "-*-"

val expected = s"${strings.get(0)}${delimiter}" +
s"${strings.get(1)}${delimiter}" +
s"${strings.get(2)}"
val joined = String.join(delimiter, strings)

assertEquals(
s"unexpected join",
expected,
joined
)
}

}