1 /* Title: Pure/library.scala
9 import java.lang.System
15 /* reverse CharSequence */
17 class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence
19 require(0 <= start && start <= end && end <= text.length)
21 def this(text: CharSequence) = this(text, 0, text.length)
23 def length: Int = end - start
24 def charAt(i: Int): Char = text.charAt(end - i - 1)
26 def subSequence(i: Int, j: Int): CharSequence =
27 if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i)
28 else throw new IndexOutOfBoundsException
30 override def toString: String =
32 val buf = new StringBuilder(length)
33 for (i <- 0 until length)
40 /* permissive UTF-8 decoding */
42 // see also http://en.wikipedia.org/wiki/UTF-8#Description
43 def decode_permissive_utf8(text: CharSequence): String =
45 val buf = new java.lang.StringBuilder(text.length)
51 if (rest == 0 && Character.isValidCodePoint(code))
52 buf.appendCodePoint(code)
53 else buf.append('\uFFFD')
58 def init(x: Int, n: Int)
66 if (rest <= 0) init(x, -1)
73 for (i <- 0 until text.length) {
74 val c = text.charAt(i)
75 if (c < 128) { flush(); buf.append(c) }
76 else if ((c & 0xC0) == 0x80) push(c & 0x3F)
77 else if ((c & 0xE0) == 0xC0) init(c & 0x1F, 1)
78 else if ((c & 0xF0) == 0xE0) init(c & 0x0F, 2)
79 else if ((c & 0xF8) == 0xF0) init(c & 0x07, 3)
88 def with_tmp_file[A](prefix: String)(body: File => A): A =
90 val file = File.createTempFile(prefix, null)
93 finally { file.delete }
100 def timeit[A](e: => A) =
102 val start = System.currentTimeMillis()
103 val result = Exn.capture(e)
104 val stop = System.currentTimeMillis()
105 System.err.println((stop - start) + "ms elapsed time")