Skip to content

Commit d59398e

Browse files
committed
Use string interpolation instead of concatenation or format().
It looks like the code predates the introduction of the string interpolation... (I'd like to replace more uses of the StringBuilder with string interpolation, but binary compatibility is in the way.)
1 parent c1d49c8 commit d59398e

33 files changed

+112
-141
lines changed

jvm/src/test/scala-2.x/scala/xml/CompilerErrors.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class CompilerTesting {
193193
// note: `code` should have a | margin
194194
// the import's needed because toolbox compiler does not accumulate imports like the real one (TODO: verify hypothesis)
195195
def xmlErrorMessages(msg: String, code: String): List[String] =
196-
errorMessages(msg)("import scala.xml.{TopScope => $scope}\n"+ code.stripMargin)
196+
errorMessages(msg)("import scala.xml.{TopScope => $scope}\n"+ code.stripMargin) // TODO what is this $scope?
197197

198198
def expectXmlError(msg: String, code: String): Unit = {
199199
val errors: List[String] = xmlErrorMessages(msg, code)
@@ -203,6 +203,6 @@ class CompilerTesting {
203203
def expectXmlErrors(msgCount: Int, msg: String, code: String): Unit = {
204204
val errors: List[String] = xmlErrorMessages(msg, code)
205205
val errorCount: Int = errors.count(_.contains(msg))
206-
assert(errorCount == msgCount, s"$errorCount occurrences of \'$msg\' found -- expected $msgCount in:\n${errors.mkString("\n")}")
206+
assert(errorCount == msgCount, s"$errorCount occurrences of '$msg' found -- expected $msgCount in:\n${errors.mkString("\n")}")
207207
}
208208
}

shared/src/main/scala/scala/xml/Atom.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import scala.collection.Seq
2424
*/
2525
class Atom[+A](val data: A) extends SpecialNode with Serializable {
2626
if (data == null)
27-
throw new IllegalArgumentException("cannot construct " + getClass.getSimpleName + " with null")
27+
throw new IllegalArgumentException(s"cannot construct ${getClass.getSimpleName} with null")
2828

2929
override protected def basisForHashCode: Seq[Any] = Seq(data)
3030

shared/src/main/scala/scala/xml/Attribute.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ trait Attribute extends MetaData {
9898
if (value == null)
9999
return
100100
if (isPrefixed)
101-
sb.append(pre).append(':')
101+
sb.append(s"$pre:")
102102

103-
sb.append(key).append('=')
103+
sb.append(s"$key=")
104104
val sb2: StringBuilder = new StringBuilder()
105105
Utility.sequenceToXML(value, TopScope, sb2, stripComments = true)
106106
Utility.appendQuoted(sb2.toString, sb)

shared/src/main/scala/scala/xml/Comment.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ case class Comment(commentText: String) extends SpecialNode {
3030
final override def doTransform: Boolean = false
3131

3232
if (commentText.contains("--")) {
33-
throw new IllegalArgumentException("text contains \"--\"")
33+
throw new IllegalArgumentException(s"""text contains "--"""")
3434
}
3535
if (commentText.nonEmpty && commentText.charAt(commentText.length - 1) == '-') {
3636
throw new IllegalArgumentException("The final character of a XML comment may not be '-'. See https://www.w3.org/TR/xml11//#IDA5CES")
@@ -40,5 +40,5 @@ case class Comment(commentText: String) extends SpecialNode {
4040
* Appends &quot;<!-- text -->&quot; to this string buffer.
4141
*/
4242
override def buildString(sb: StringBuilder): StringBuilder =
43-
sb.append("<!--").append(commentText).append("-->")
43+
sb.append(s"<!--$commentText-->")
4444
}

shared/src/main/scala/scala/xml/EntityRef.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ case class EntityRef(entityName: String) extends SpecialNode {
4141
* @return the modified string buffer `sb`.
4242
*/
4343
override def buildString(sb: StringBuilder): StringBuilder =
44-
sb.append("&").append(entityName).append(";")
44+
sb.append(s"&$entityName;")
4545
}

shared/src/main/scala/scala/xml/Group.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ final case class Group(nodes: Seq[Node]) extends Node {
4040
* Since Group is very much a hack it throws an exception if you
4141
* try to do anything with it.
4242
*/
43-
private def fail(msg: String): Nothing = throw new UnsupportedOperationException("class Group does not support method '%s'".format(msg))
43+
private def fail(msg: String): Nothing = throw new UnsupportedOperationException(s"class Group does not support method '$msg'")
4444

4545
override def label: Nothing = fail("label")
4646
override def attributes: Nothing = fail("attributes")

shared/src/main/scala/scala/xml/MetaData.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ abstract class MetaData
174174
* prefixed, and "key" otherwise.
175175
*/
176176
def prefixedKey: String = this match {
177-
case x: Attribute if x.isPrefixed => x.pre + ":" + key
177+
case x: Attribute if x.isPrefixed => s"${x.pre}:$key"
178178
case _ => key
179179
}
180180

shared/src/main/scala/scala/xml/NamespaceBinding.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ case class NamespaceBinding(prefix: String, uri: String, parent: NamespaceBindin
7979
private def doBuildString(sb: StringBuilder, stop: NamespaceBinding): Unit = {
8080
if (List(null, stop, TopScope).contains(this)) return
8181

82-
val s: String = " xmlns%s=\"%s\"".format(
83-
if (prefix != null) ":" + prefix else "",
84-
if (uri != null) uri else ""
85-
)
86-
parent.doBuildString(sb.append(s), stop) // copy(ignore)
82+
val prefixStr: String = if (prefix != null) s":$prefix" else ""
83+
val uriStr: String = if (uri != null) uri else ""
84+
parent.doBuildString(sb.append(s""" xmlns$prefixStr="$uriStr""""), stop) // copy(ignore)
8785
}
8886
}

shared/src/main/scala/scala/xml/Node.scala

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,8 @@ abstract class Node extends NodeSeq {
184184
/**
185185
* Appends qualified name of this node to `StringBuilder`.
186186
*/
187-
def nameToString(sb: StringBuilder): StringBuilder = {
188-
if (null != prefix) {
189-
sb.append(prefix)
190-
sb.append(':')
191-
}
192-
sb.append(label)
193-
}
187+
def nameToString(sb: StringBuilder): StringBuilder =
188+
sb.append(s"${if (prefix == null) "" else s"$prefix:"}$label")
194189

195190
/**
196191
* Returns a type symbol (e.g. DTD, XSD), default `'''null'''`.

shared/src/main/scala/scala/xml/NodeSeq.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ abstract class NodeSeq extends AbstractSeq[Node] with immutable.Seq[Node] with S
157157
* Convenience method which returns string text of the named attribute. Use:
158158
* - `that \@ "foo"` to get the string text of attribute `"foo"`;
159159
*/
160-
def \@(attributeName: String): String = (this \ ("@" + attributeName)).text
160+
def \@(attributeName: String): String = (this \ s"@$attributeName").text
161161

162162
override def toString: String = theSeq.mkString
163163

0 commit comments

Comments
 (0)