summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2011-01-20 23:19:14 +0000
committerPaul Phillips <paulp@improving.org>2011-01-20 23:19:14 +0000
commite20693030316e4854886f7a945e6a6a3117ca071 (patch)
tree5babed112aa56aff89628f0b4f66365eef21009d /src/library
parentcd52c9797d1ac04cd87da794c80f4735503593ea (diff)
downloadscala-e20693030316e4854886f7a945e6a6a3117ca071.tar.gz
scala-e20693030316e4854886f7a945e6a6a3117ca071.tar.bz2
scala-e20693030316e4854886f7a945e6a6a3117ca071.zip
Minor collections cleanups, no review.
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/collection/immutable/StringLike.scala9
-rw-r--r--src/library/scala/collection/package.scala36
-rw-r--r--src/library/scala/util/matching/Regex.scala22
3 files changed, 33 insertions, 34 deletions
diff --git a/src/library/scala/collection/immutable/StringLike.scala b/src/library/scala/collection/immutable/StringLike.scala
index 1be0596d51..acb5dd586a 100644
--- a/src/library/scala/collection/immutable/StringLike.scala
+++ b/src/library/scala/collection/immutable/StringLike.scala
@@ -219,13 +219,8 @@ self =>
else
throw new NumberFormatException("For input string: \"null\"")
- /* !!! causes crash?
- def toArray: Array[Char] = {
- val result = new Array[Char](length)
- toString.getChars(0, length, result, 0)
- result
- }
- */
+ override def toArray[B >: Char : ClassManifest]: Array[B] =
+ toString.toCharArray.asInstanceOf[Array[B]]
private def unwrapArg(arg: Any): AnyRef = arg match {
case x: ScalaNumber => x.underlying
diff --git a/src/library/scala/collection/package.scala b/src/library/scala/collection/package.scala
index 31cea84ab8..f951f22057 100644
--- a/src/library/scala/collection/package.scala
+++ b/src/library/scala/collection/package.scala
@@ -68,26 +68,40 @@ package scala
*
*/
package object collection {
- import scala.collection.generic.CanBuildFrom // can't refer to CanBuild here
+ import scala.collection.generic.CanBuildFrom
- /** Provides a CanBuildFrom instance that builds a specific target collection (`To') irrespective of the original collection (`From').
+ /** Provides a CanBuildFrom instance that builds a specific target collection (`To')
+ * irrespective of the original collection (`From').
*/
- def breakOut[From, T, To](implicit b : CanBuildFrom[Nothing, T, To]) =
- new CanBuildFrom[From, T, To] { // TODO: could we just return b instead?
- def apply(from: From) = b.apply() ; def apply() = b.apply()
+ def breakOut[From, T, To](implicit b: CanBuildFrom[Nothing, T, To]): CanBuildFrom[From, T, To] =
+ // can't just return b because the argument to apply could be cast to From in b
+ new CanBuildFrom[From, T, To] {
+ def apply(from: From) = b.apply()
+ def apply() = b.apply()
}
+}
+package collection {
+ /** Collection internal utility functions.
+ */
private[collection] object DebugUtils {
- /* debug utils */
+ def unsupported(msg: String) = throw new UnsupportedOperationException(msg)
+ def noSuchElement(msg: String) = throw new NoSuchElementException(msg)
+ def indexOutOfBounds(index: Int) = throw new IndexOutOfBoundsException(index.toString)
+ def illegalArgument(msg: String) = throw new IllegalArgumentException(msg)
+
def buildString(closure: (Any => Unit) => Unit): String = {
var output = ""
- def appendln(s: Any) = output += s + "\n"
- closure(appendln)
+ closure(output += _ + "\n")
+
output
}
- def arrayString[T](array: Array[T], from: Int, until: Int) = array.slice(from, until).map(x => if (x != null) x.toString else "n/a").mkString(" | ")
-
+ def arrayString[T](array: Array[T], from: Int, until: Int): String = {
+ array.slice(from, until) map {
+ case null => "n/a"
+ case x => "" + x
+ } mkString " | "
+ }
}
-
}
diff --git a/src/library/scala/util/matching/Regex.scala b/src/library/scala/util/matching/Regex.scala
index 68e971b92f..6a783e4594 100644
--- a/src/library/scala/util/matching/Regex.scala
+++ b/src/library/scala/util/matching/Regex.scala
@@ -10,9 +10,7 @@
package scala.util.matching
-import java.util.regex.{Pattern, Matcher}
-import collection.immutable.List
-import collection.{Iterator, Seq}
+import java.util.regex.{ Pattern, Matcher }
/** This class provides methods for creating and using regular expressions.
* It is based on the regular expressions of the JDK since 1.4.
@@ -32,7 +30,7 @@ import collection.{Iterator, Seq}
* @param groupNames A mapping from names to indices in capture groups
*/
@SerialVersionUID(-2094783597747625537L)
-class Regex(regex: String, groupNames: String*) extends Serializable {
+class Regex(regex: String, groupNames: String*) extends Serializable {
import Regex._
@@ -114,20 +112,15 @@ class Regex(regex: String, groupNames: String*) extends Serializable {
*/
def replaceAllIn(target: java.lang.CharSequence, replacer: Match => String): String = {
val it = new Regex.MatchIterator(target, this, groupNames).replacementData
- while (it.hasNext) {
- val matchdata = it.next
- it.replace(replacer(matchdata))
- }
+ it foreach (md => it replace replacer(md))
it.replaced
}
def replaceSomeIn(target: java.lang.CharSequence, replacer: Match => Option[String]): String = {
val it = new Regex.MatchIterator(target, this, groupNames).replacementData
- while (it.hasNext) {
- val matchdata = it.next
- val replaceopt = replacer(matchdata)
- if (replaceopt != None) it.replace(replaceopt.get)
- }
+ for (matchdata <- it ; replacement <- replacer(matchdata))
+ it replace replacement
+
it.replaced
}
@@ -363,6 +356,3 @@ object Regex {
def replace(rs: String) = matcher.appendReplacement(sb, rs)
}
}
-
-
-