2
** ________ ___ / / ___ Scala API **
3
** / __/ __// _ | / / / _ | (c) 2002-2013, LAMP/EPFL **
4
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5
** /____/\___/_/ |_/____/_/ | | **
10
package scala.reflect.internal.util
12
/** This object provides utility methods to extract elements
15
* @author Martin Odersky
19
def onull(s: String) = if (s == null) "" else s
20
def oempty(xs: String*) = xs filterNot (x => x == null || x == "")
21
def ojoin(xs: String*): String = oempty(xs: _*) mkString " "
22
def ojoin(xs: Seq[String], sep: String): String = oempty(xs: _*) mkString sep
23
def ojoinOr(xs: Seq[String], sep: String, orElse: String) = {
24
val ys = oempty(xs: _*)
25
if (ys.isEmpty) orElse else ys mkString sep
27
def trimTrailingSpace(s: String) = {
28
if (s.length == 0 || !s.charAt(s.length - 1).isWhitespace) s
30
var idx = s.length - 1
31
while (idx >= 0 && s.charAt(idx).isWhitespace)
34
s.substring(0, idx + 1)
38
def decompose(str: String, sep: Char): List[String] = {
39
def ws(start: Int): List[String] =
40
if (start == str.length) List()
41
else if (str.charAt(start) == sep) ws(start + 1)
43
val end = str.indexOf(sep, start)
44
if (end < 0) List(str.substring(start))
45
else str.substring(start, end) :: ws(end + 1)
50
def words(str: String): List[String] = decompose(str, ' ')
52
def stripPrefixOpt(str: String, prefix: String): Option[String] =
53
if (str startsWith prefix) Some(str drop prefix.length)
56
def stripSuffixOpt(str: String, suffix: String): Option[String] =
57
if (str endsWith suffix) Some(str dropRight suffix.length)
60
def splitWhere(str: String, f: Char => Boolean, doDropIndex: Boolean = false): Option[(String, String)] =
61
splitAt(str, str indexWhere f, doDropIndex)
63
def splitAt(str: String, idx: Int, doDropIndex: Boolean = false): Option[(String, String)] =
65
else Some((str take idx, str drop (if (doDropIndex) idx + 1 else idx)))
67
/** Returns a string meaning "n elements".
73
def countElementsAsString(n: Int, elements: String): String =
75
case 0 => "no " + elements + "s"
76
case 1 => "one " + elements
77
case 2 => "two " + elements + "s"
78
case 3 => "three " + elements + "s"
79
case 4 => "four " + elements + "s"
80
case _ => "" + n + " " + elements + "s"
83
/** Turns a count into a friendly English description if n<=4.
88
def countAsString(n: Int): String =
99
object StringOps extends StringOps { }