~ubuntu-branches/debian/sid/scala/sid

« back to all changes in this revision

Viewing changes to src/reflect/scala/reflect/internal/util/StringOps.scala

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg, Mehdi Dogguy, Lucas Satabin, Frank S. Thomas, Emmanuel Bourg
  • Date: 2015-06-05 23:52:59 UTC
  • mfrom: (1.2.11)
  • Revision ID: package-import@ubuntu.com-20150605235259-wk00vgk83dh8o19g
Tags: 2.10.5-1
* Team upload.

[ Mehdi Dogguy ]
* New upstream release (Closes: #744278).

[ Lucas Satabin ]
* Update patches
* Update the clean target
* Update paths of elements to install
* Update watch file

[ Frank S. Thomas ]
* Remove myself from Uploaders.

[ Emmanuel Bourg ]
* The package has been adopted by the Java Team (Closes: #754935)
* Patched the build to avoid downloading libraries from the Internet
* Replaced the minified JavaScript files with unobfuscated ones
* No longer build scala-partest.jar until diffutils is packaged or replaced
* debian/watch: Fixed the versions matched (x.y.z instead of x.y.z..z)
* debian/rules:
  - Added the missing get-orig-source target (Closes: #724704)
  - Improved the clean target
* debian/control:
  - Build depend on scala (>= 2.10) and bnd
  - Use canonical URLs for the Vcs-* fields
  - Standards-Version updated to 3.9.6 (no changes)
* Switch to debhelper level 9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*                     __                                               *\
 
2
**     ________ ___   / /  ___     Scala API                            **
 
3
**    / __/ __// _ | / /  / _ |    (c) 2002-2013, LAMP/EPFL             **
 
4
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
 
5
** /____/\___/_/ |_/____/_/ | |                                         **
 
6
**                          |/                                          **
 
7
\*                                                                      */
 
8
 
 
9
 
 
10
package scala.reflect.internal.util
 
11
 
 
12
/** This object provides utility methods to extract elements
 
13
 *  from Strings.
 
14
 *
 
15
 *  @author Martin Odersky
 
16
 *  @version 1.0
 
17
 */
 
18
trait StringOps {
 
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
 
26
  }
 
27
  def trimTrailingSpace(s: String) = {
 
28
    if (s.length == 0 || !s.charAt(s.length - 1).isWhitespace) s
 
29
    else {
 
30
      var idx = s.length - 1
 
31
      while (idx >= 0 && s.charAt(idx).isWhitespace)
 
32
        idx -= 1
 
33
 
 
34
      s.substring(0, idx + 1)
 
35
    }
 
36
  }
 
37
 
 
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)
 
42
      else {
 
43
        val end = str.indexOf(sep, start)
 
44
        if (end < 0) List(str.substring(start))
 
45
        else str.substring(start, end) :: ws(end + 1)
 
46
      }
 
47
    ws(0)
 
48
  }
 
49
 
 
50
  def words(str: String): List[String] = decompose(str, ' ')
 
51
 
 
52
  def stripPrefixOpt(str: String, prefix: String): Option[String] =
 
53
    if (str startsWith prefix) Some(str drop prefix.length)
 
54
    else None
 
55
 
 
56
  def stripSuffixOpt(str: String, suffix: String): Option[String] =
 
57
    if (str endsWith suffix) Some(str dropRight suffix.length)
 
58
    else None
 
59
 
 
60
  def splitWhere(str: String, f: Char => Boolean, doDropIndex: Boolean = false): Option[(String, String)] =
 
61
    splitAt(str, str indexWhere f, doDropIndex)
 
62
 
 
63
  def splitAt(str: String, idx: Int, doDropIndex: Boolean = false): Option[(String, String)] =
 
64
    if (idx == -1) None
 
65
    else Some((str take idx, str drop (if (doDropIndex) idx + 1 else idx)))
 
66
 
 
67
  /** Returns a string meaning "n elements".
 
68
   *
 
69
   *  @param n        ...
 
70
   *  @param elements ...
 
71
   *  @return         ...
 
72
   */
 
73
  def countElementsAsString(n: Int, elements: String): String =
 
74
    n match {
 
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"
 
81
    }
 
82
 
 
83
  /** Turns a count into a friendly English description if n<=4.
 
84
   *
 
85
   *  @param n        ...
 
86
   *  @return         ...
 
87
   */
 
88
  def countAsString(n: Int): String =
 
89
    n match {
 
90
      case 0 => "none"
 
91
      case 1 => "one"
 
92
      case 2 => "two"
 
93
      case 3 => "three"
 
94
      case 4 => "four"
 
95
      case _ => "" + n
 
96
    }
 
97
}
 
98
 
 
99
object StringOps extends StringOps { }