1 /* Title: Pure/library.scala
11 import scala.collection.mutable
12 import scala.util.matching.Regex
19 class User_Error(message: String) extends RuntimeException(message)
21 override def equals(that: Any): Boolean =
23 case other: User_Error => message == other.getMessage
26 override def hashCode: Int = message.hashCode
28 override def toString: String = "ERROR(" + message + ")"
33 def apply(message: String): User_Error = new User_Error(message)
34 def unapply(exn: Throwable): Option[String] = Exn.user_message(exn)
37 def error(message: String): Nothing = throw ERROR(message)
39 def cat_message(msg1: String, msg2: String): String =
41 else if (msg2 == "") msg1
42 else msg1 + "\n" + msg2
44 def cat_error(msg1: String, msg2: String): Nothing =
45 error(cat_message(msg1, msg2))
50 private val small_int = 10000
51 private lazy val small_int_table =
53 val array = new Array[String](small_int)
54 for (i <- 0 until small_int) array(i) = i.toString
58 def is_small_int(s: String): Boolean =
61 1 <= len && len <= 4 &&
62 s.forall(c => '0' <= c && c <= '9') &&
63 (len == 1 || s(0) != '0')
66 def signed_string_of_long(i: Long): String =
67 if (0 <= i && i < small_int) small_int_table(i.toInt)
70 def signed_string_of_int(i: Int): String =
71 if (0 <= i && i < small_int) small_int_table(i)
75 /* separated chunks */
77 def separate[A](s: A, list: List[A]): List[A] =
79 val result = new mutable.ListBuffer[A]
94 def separated_chunks(sep: Char => Boolean, source: CharSequence): Iterator[CharSequence] =
95 new Iterator[CharSequence] {
96 private val end = source.length
97 private def next_chunk(i: Int): Option[(CharSequence, Int)] =
100 var j = i; do j += 1 while (j < end && !sep(source.charAt(j)))
101 Some((source.subSequence(i + 1, j), j))
105 private var state: Option[(CharSequence, Int)] = if (end == 0) None else next_chunk(-1)
107 def hasNext(): Boolean = state.isDefined
108 def next(): CharSequence =
110 case Some((s, i)) => state = next_chunk(i); s
111 case None => Iterator.empty.next()
115 def space_explode(sep: Char, str: String): List[String] =
116 separated_chunks(_ == sep, str).map(_.toString).toList
121 def terminate_lines(lines: Iterable[CharSequence]): Iterable[CharSequence] =
122 new Iterable[CharSequence] {
123 def iterator: Iterator[CharSequence] =
124 lines.iterator.map(line => new Line_Termination(line))
127 def cat_lines(lines: TraversableOnce[String]): String = lines.mkString("\n")
129 def split_lines(str: String): List[String] = space_explode('\n', str)
131 def first_line(source: CharSequence): String =
133 val lines = separated_chunks(_ == '\n', source)
134 if (lines.hasNext) lines.next.toString
141 def try_unprefix(prfx: String, s: String): Option[String] =
142 if (s.startsWith(prfx)) Some(s.substring(prfx.length)) else None
144 def try_unsuffix(sffx: String, s: String): Option[String] =
145 if (s.endsWith(sffx)) Some(s.substring(0, s.length - sffx.length)) else None
147 def trim_line(s: String): String =
148 if (s.endsWith("\r\n")) s.substring(0, s.length - 2)
149 else if (s.endsWith("\r") || s.endsWith("\n")) s.substring(0, s.length - 1)
155 def quote(s: String): String = "\"" + s + "\""
157 def try_unquote(s: String): Option[String] =
158 if (s.startsWith("\"") && s.endsWith("\"")) Some(s.substring(1, s.length - 1))
161 def perhaps_unquote(s: String): String = try_unquote(s) getOrElse s
163 def commas(ss: Iterable[String]): String = ss.iterator.mkString(", ")
164 def commas_quote(ss: Iterable[String]): String = ss.iterator.map(quote).mkString(", ")
169 class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence
171 require(0 <= start && start <= end && end <= text.length)
173 def this(text: CharSequence) = this(text, 0, text.length)
175 def length: Int = end - start
176 def charAt(i: Int): Char = text.charAt(end - i - 1)
178 def subSequence(i: Int, j: Int): CharSequence =
179 if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i)
180 else throw new IndexOutOfBoundsException
182 override def toString: String =
184 val buf = new StringBuilder(length)
185 for (i <- 0 until length)
186 buf.append(charAt(i))
191 class Line_Termination(text: CharSequence) extends CharSequence
193 def length: Int = text.length + 1
194 def charAt(i: Int): Char = if (i == text.length) '\n' else text.charAt(i)
195 def subSequence(i: Int, j: Int): CharSequence =
196 if (j == text.length + 1) new Line_Termination(text.subSequence(i, j - 1))
197 else text.subSequence(i, j)
198 override def toString: String = text.toString + "\n"
202 /* regular expressions */
204 def make_regex(s: String): Option[Regex] =
205 try { Some(new Regex(s)) } catch { case ERROR(_) => None }
208 /* canonical list operations */
210 def member[A, B](xs: List[A])(x: B): Boolean = xs.contains(x)
211 def insert[A](x: A)(xs: List[A]): List[A] = if (xs.contains(x)) xs else x :: xs
212 def remove[A, B](x: B)(xs: List[A]): List[A] = if (member(xs)(x)) xs.filterNot(_ == x) else xs
213 def update[A](x: A)(xs: List[A]): List[A] = x :: remove(x)(xs)
219 val ERROR = Library.ERROR
220 val error = Library.error _
221 val cat_error = Library.cat_error _
223 val space_explode = Library.space_explode _
224 val split_lines = Library.split_lines _
225 val cat_lines = Library.cat_lines _
226 val quote = Library.quote _
227 val commas = Library.commas _
228 val commas_quote = Library.commas_quote _