34136
|
1 |
/* Title: Pure/library.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Basic library.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
import java.lang.System
|
34216
|
10 |
import java.awt.Component
|
|
11 |
import javax.swing.JOptionPane
|
34136
|
12 |
|
|
13 |
|
|
14 |
object Library
|
|
15 |
{
|
34141
|
16 |
/* reverse CharSequence */
|
|
17 |
|
|
18 |
class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence
|
|
19 |
{
|
|
20 |
require(0 <= start && start <= end && end <= text.length)
|
|
21 |
|
|
22 |
def this(text: CharSequence) = this(text, 0, text.length)
|
|
23 |
|
|
24 |
def length: Int = end - start
|
|
25 |
def charAt(i: Int): Char = text.charAt(end - i - 1)
|
|
26 |
|
|
27 |
def subSequence(i: Int, j: Int): CharSequence =
|
|
28 |
if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i)
|
|
29 |
else throw new IndexOutOfBoundsException
|
|
30 |
|
|
31 |
override def toString: String =
|
|
32 |
{
|
|
33 |
val buf = new StringBuilder(length)
|
|
34 |
for (i <- 0 until length)
|
|
35 |
buf.append(charAt(i))
|
|
36 |
buf.toString
|
|
37 |
}
|
|
38 |
}
|
|
39 |
|
|
40 |
|
36685
|
41 |
/* iterate over chunks (cf. space_explode/split_lines in ML) */
|
|
42 |
|
|
43 |
def chunks(source: CharSequence, sep: Char = '\n') = new Iterator[CharSequence]
|
|
44 |
{
|
|
45 |
private val end = source.length
|
|
46 |
private def next_chunk(i: Int): Option[(CharSequence, Int)] =
|
|
47 |
{
|
|
48 |
if (i < end) {
|
|
49 |
var j = i; do j += 1 while (j < end && source.charAt(j) != sep)
|
|
50 |
Some((source.subSequence(i + 1, j), j))
|
|
51 |
}
|
|
52 |
else None
|
|
53 |
}
|
|
54 |
private var state: Option[(CharSequence, Int)] = if (end == 0) None else next_chunk(-1)
|
|
55 |
|
|
56 |
def hasNext(): Boolean = state.isDefined
|
|
57 |
def next(): CharSequence =
|
|
58 |
state match {
|
|
59 |
case Some((s, i)) => { state = next_chunk(i); s }
|
|
60 |
case None => throw new NoSuchElementException("next on empty iterator")
|
|
61 |
}
|
|
62 |
}
|
|
63 |
|
|
64 |
|
34216
|
65 |
/* simple dialogs */
|
|
66 |
|
|
67 |
private def simple_dialog(kind: Int, default_title: String)
|
|
68 |
(parent: Component, title: String, message: Any*)
|
|
69 |
{
|
|
70 |
JOptionPane.showMessageDialog(parent,
|
|
71 |
message.toArray.asInstanceOf[Array[AnyRef]],
|
|
72 |
if (title == null) default_title else title, kind)
|
|
73 |
}
|
|
74 |
|
|
75 |
def dialog = simple_dialog(JOptionPane.PLAIN_MESSAGE, null) _
|
|
76 |
def warning_dialog = simple_dialog(JOptionPane.WARNING_MESSAGE, "Warning") _
|
|
77 |
def error_dialog = simple_dialog(JOptionPane.ERROR_MESSAGE, "Error") _
|
|
78 |
|
|
79 |
|
34136
|
80 |
/* timing */
|
|
81 |
|
34314
|
82 |
def timeit[A](message: String)(e: => A) =
|
34136
|
83 |
{
|
|
84 |
val start = System.currentTimeMillis()
|
|
85 |
val result = Exn.capture(e)
|
|
86 |
val stop = System.currentTimeMillis()
|
34314
|
87 |
System.err.println(
|
34317
|
88 |
(if (message.isEmpty) "" else message + ": ") + (stop - start) + "ms elapsed time")
|
34136
|
89 |
Exn.release(result)
|
|
90 |
}
|
|
91 |
}
|