author | paulson <lp15@cam.ac.uk> |
Thu, 22 Aug 2024 22:26:28 +0100 | |
changeset 80736 | c8bcb14fcfa8 |
parent 80478 | 902e6da44a68 |
child 81826 | 57b0a02e2774 |
permissions | -rw-r--r-- |
34136 | 1 |
/* Title: Pure/library.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Basic library. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
38258 | 9 |
|
63781 | 10 |
import scala.annotation.tailrec |
51981
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
11 |
import scala.collection.mutable |
59224 | 12 |
import scala.util.matching.Regex |
51981
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
13 |
|
37018 | 14 |
|
75393 | 15 |
object Library { |
63789 | 16 |
/* resource management */ |
17 |
||
75393 | 18 |
def using[A <: AutoCloseable, B](a: A)(f: A => B): B = { |
69393
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
68715
diff
changeset
|
19 |
try { f(a) } |
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
68715
diff
changeset
|
20 |
finally { if (a != null) a.close() } |
63789 | 21 |
} |
22 |
||
77515 | 23 |
def using_option[A <: AutoCloseable, B](opt: Option[A])(f: A => B): Option[B] = |
24 |
opt.map(a => using(a)(f)) |
|
25 |
||
78198 | 26 |
def using_optional[A <: AutoCloseable, B](opt: Option[A])(f: Option[A] => B): B = { |
27 |
try { f(opt) } |
|
28 |
finally { |
|
29 |
opt match { |
|
30 |
case Some(a) if a != null => a.close() |
|
31 |
case _ => |
|
32 |
} |
|
33 |
} |
|
34 |
} |
|
35 |
||
63789 | 36 |
|
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
37 |
/* integers */ |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
38 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
39 |
private val small_int = 10000 |
75393 | 40 |
private lazy val small_int_table = { |
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
41 |
val array = new Array[String](small_int) |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
42 |
for (i <- 0 until small_int) array(i) = i.toString |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
43 |
array |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
44 |
} |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
45 |
|
75393 | 46 |
def is_small_int(s: String): Boolean = { |
57909
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
47 |
val len = s.length |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
48 |
1 <= len && len <= 4 && |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
49 |
s.forall(c => '0' <= c && c <= '9') && |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
50 |
(len == 1 || s(0) != '0') |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
51 |
} |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
52 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
53 |
def signed_string_of_long(i: Long): String = |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
54 |
if (0 <= i && i < small_int) small_int_table(i.toInt) |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
55 |
else i.toString |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
56 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
57 |
def signed_string_of_int(i: Int): String = |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
58 |
if (0 <= i && i < small_int) small_int_table(i) |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
59 |
else i.toString |
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
60 |
|
0fb331032f02
more compact representation of special string values;
wenzelm
parents:
57831
diff
changeset
|
61 |
|
48996 | 62 |
/* separated chunks */ |
36688 | 63 |
|
75393 | 64 |
def separate[A](s: A, list: List[A]): List[A] = { |
51981
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
65 |
val result = new mutable.ListBuffer[A] |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
66 |
var first = true |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
67 |
for (x <- list) { |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
68 |
if (first) { |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
69 |
first = false |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
70 |
result += x |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
71 |
} |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
72 |
else { |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
73 |
result += s |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
74 |
result += x |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
75 |
} |
36688 | 76 |
} |
51981
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
77 |
result.toList |
a8ffd3692f57
more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents:
51616
diff
changeset
|
78 |
} |
36688 | 79 |
|
56600 | 80 |
def separated_chunks(sep: Char => Boolean, source: CharSequence): Iterator[CharSequence] = |
48996 | 81 |
new Iterator[CharSequence] { |
80478
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
82 |
private val length = source.length |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
83 |
private var start = -1 |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
84 |
private var stop = -1 |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
85 |
|
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
86 |
private def end(i: Int): Int = { |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
87 |
var j = i |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
88 |
while (j < length && !sep(source.charAt(j))) { j += 1 } |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
89 |
j |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
90 |
} |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
91 |
|
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
92 |
// init |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
93 |
if (!source.isEmpty) { start = 0; stop = end(0) } |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
94 |
|
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
95 |
def hasNext: Boolean = 0 <= start && stop <= length |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
96 |
|
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
97 |
def next(): CharSequence = |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
98 |
if (hasNext) { |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
99 |
val chunk = source.subSequence(start, stop) |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
100 |
if (stop < length) { start = stop + 1; stop = end(start) } |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
101 |
else { start = -1; stop = -1 } |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
102 |
chunk |
48996 | 103 |
} |
80478
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
104 |
else throw new NoSuchElementException |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
105 |
} |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
106 |
|
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
107 |
def separated_chunks(sep: Char, source: String): Iterator[String] = |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
108 |
new Iterator[String] { |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
109 |
private var start = -1 |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
110 |
private var stop = -1 |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
111 |
|
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
112 |
private def end(i: Int): Int = |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
113 |
source.indexOf(sep, start) match { |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
114 |
case -1 => source.length |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
115 |
case j => j |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
116 |
} |
48996 | 117 |
|
80478
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
118 |
// init |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
119 |
if (source.nonEmpty) { start = 0; stop = end(0) } |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
120 |
|
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
121 |
def hasNext: Boolean = |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
122 |
0 <= start && start <= stop && stop <= source.length |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
123 |
|
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
124 |
def next(): String = |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
125 |
if (hasNext) { |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
126 |
val chunk = source.substring(start, stop) |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
127 |
if (stop < source.length) { start = stop + 1; stop = end(start) } |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
128 |
else { start = -1; stop = -1 } |
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
129 |
chunk |
48996 | 130 |
} |
80478
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
131 |
else throw new NoSuchElementException |
43598 | 132 |
} |
133 |
||
48996 | 134 |
def space_explode(sep: Char, str: String): List[String] = |
80478
902e6da44a68
notable performance tuning for Library.separated_chunks variants;
wenzelm
parents:
80441
diff
changeset
|
135 |
separated_chunks(sep, str).toList |
48996 | 136 |
|
137 |
||
138 |
/* lines */ |
|
139 |
||
77007 | 140 |
def count_newlines(str: String): Int = str.count(_ == '\n') |
141 |
||
75859
7164f537370f
proper treatment of empty lines (amending 08f89f0e8a62);
wenzelm
parents:
75709
diff
changeset
|
142 |
def terminate_lines(lines: IterableOnce[String]): String = { |
7164f537370f
proper treatment of empty lines (amending 08f89f0e8a62);
wenzelm
parents:
75709
diff
changeset
|
143 |
val it = lines.iterator |
7164f537370f
proper treatment of empty lines (amending 08f89f0e8a62);
wenzelm
parents:
75709
diff
changeset
|
144 |
if (it.isEmpty) "" else it.mkString("", "\n", "\n") |
7164f537370f
proper treatment of empty lines (amending 08f89f0e8a62);
wenzelm
parents:
75709
diff
changeset
|
145 |
} |
51983
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
146 |
|
73362 | 147 |
def cat_lines(lines: IterableOnce[String]): String = |
148 |
lines.iterator.mkString("\n") |
|
48996 | 149 |
|
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
150 |
def split_lines(str: String): List[String] = space_explode('\n', str) |
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43652
diff
changeset
|
151 |
|
62590 | 152 |
def prefix_lines(prfx: String, str: String): String = |
73963 | 153 |
isabelle.setup.Library.prefix_lines(prfx, str) |
62590 | 154 |
|
73736 | 155 |
def indent_lines(n: Int, str: String): String = |
156 |
prefix_lines(Symbol.spaces(n), str) |
|
157 |
||
75393 | 158 |
def first_line(source: CharSequence): String = { |
56600 | 159 |
val lines = separated_chunks(_ == '\n', source) |
73344 | 160 |
if (lines.hasNext) lines.next().toString |
48996 | 161 |
else "" |
162 |
} |
|
163 |
||
73332 | 164 |
def trim_line(s: String): String = |
73963 | 165 |
isabelle.setup.Library.trim_line(s) |
73332 | 166 |
|
167 |
def trim_split_lines(s: String): List[String] = |
|
168 |
split_lines(trim_line(s)).map(trim_line) |
|
169 |
||
65932 | 170 |
def encode_lines(s: String): String = s.replace('\n', '\u000b') |
171 |
def decode_lines(s: String): String = s.replace('\u000b', '\n') |
|
172 |
||
50847 | 173 |
|
174 |
/* strings */ |
|
175 |
||
80440 | 176 |
def string_builder(hint: Int = 0)(body: StringBuilder => Unit): String = { |
177 |
val builder = new StringBuilder(if (hint <= 0) 16 else hint) |
|
178 |
body(builder) |
|
179 |
builder.toString |
|
64355 | 180 |
} |
181 |
||
50847 | 182 |
def try_unprefix(prfx: String, s: String): Option[String] = |
183 |
if (s.startsWith(prfx)) Some(s.substring(prfx.length)) else None |
|
184 |
||
55033 | 185 |
def try_unsuffix(sffx: String, s: String): Option[String] = |
186 |
if (s.endsWith(sffx)) Some(s.substring(0, s.length - sffx.length)) else None |
|
187 |
||
65606 | 188 |
def perhaps_unprefix(prfx: String, s: String): String = try_unprefix(prfx, s) getOrElse s |
189 |
def perhaps_unsuffix(sffx: String, s: String): String = try_unsuffix(sffx, s) getOrElse s |
|
190 |
||
65903 | 191 |
def isolate_substring(s: String): String = new String(s.toCharArray) |
64820
00488a8c042f
Line.Document consists of independently allocated strings;
wenzelm
parents:
64370
diff
changeset
|
192 |
|
71864 | 193 |
def strip_ansi_color(s: String): String = |
73355 | 194 |
s.replaceAll("\u001b\\[\\d+m", "") |
71864 | 195 |
|
43598 | 196 |
|
48996 | 197 |
/* quote */ |
46196 | 198 |
|
67820
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67436
diff
changeset
|
199 |
def single_quote(s: String): String = "'" + s + "'" |
e30d6368c7c8
clarified argument formats: explicit Unit, allow XML.Elem as well;
wenzelm
parents:
67436
diff
changeset
|
200 |
|
43598 | 201 |
def quote(s: String): String = "\"" + s + "\"" |
56843
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56730
diff
changeset
|
202 |
|
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56730
diff
changeset
|
203 |
def try_unquote(s: String): Option[String] = |
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56730
diff
changeset
|
204 |
if (s.startsWith("\"") && s.endsWith("\"")) Some(s.substring(1, s.length - 1)) |
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56730
diff
changeset
|
205 |
else None |
b2bfcd8cda80
support for path completion based on file-system content;
wenzelm
parents:
56730
diff
changeset
|
206 |
|
58592 | 207 |
def perhaps_unquote(s: String): String = try_unquote(s) getOrElse s |
208 |
||
43598 | 209 |
def commas(ss: Iterable[String]): String = ss.iterator.mkString(", ") |
48362 | 210 |
def commas_quote(ss: Iterable[String]): String = ss.iterator.map(quote).mkString(", ") |
43598 | 211 |
|
36688 | 212 |
|
51983
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
213 |
/* CharSequence */ |
34141 | 214 |
|
75393 | 215 |
class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence { |
73120
c3589f2dff31
more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents:
72214
diff
changeset
|
216 |
require(0 <= start && start <= end && end <= text.length, "bad reverse range") |
34141 | 217 |
|
218 |
def this(text: CharSequence) = this(text, 0, text.length) |
|
219 |
||
220 |
def length: Int = end - start |
|
221 |
def charAt(i: Int): Char = text.charAt(end - i - 1) |
|
222 |
||
223 |
def subSequence(i: Int, j: Int): CharSequence = |
|
224 |
if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i) |
|
225 |
else throw new IndexOutOfBoundsException |
|
226 |
||
80441 | 227 |
override def toString: String = |
228 |
string_builder(hint = length) { buf => for (i <- 0 until length) buf.append(charAt(i)) } |
|
34141 | 229 |
} |
230 |
||
75393 | 231 |
class Line_Termination(text: CharSequence) extends CharSequence { |
51983
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
232 |
def length: Int = text.length + 1 |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
233 |
def charAt(i: Int): Char = if (i == text.length) '\n' else text.charAt(i) |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
234 |
def subSequence(i: Int, j: Int): CharSequence = |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
235 |
if (j == text.length + 1) new Line_Termination(text.subSequence(i, j - 1)) |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
236 |
else text.subSequence(i, j) |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
237 |
override def toString: String = text.toString + "\n" |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
238 |
} |
32692ce4c61a
more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents:
51981
diff
changeset
|
239 |
|
34141 | 240 |
|
59224 | 241 |
/* regular expressions */ |
242 |
||
243 |
def make_regex(s: String): Option[Regex] = |
|
244 |
try { Some(new Regex(s)) } catch { case ERROR(_) => None } |
|
245 |
||
64871 | 246 |
def is_regex_meta(c: Char): Boolean = """()[]{}\^$|?*+.<>-=!""".contains(c) |
247 |
||
248 |
def escape_regex(s: String): String = |
|
71601 | 249 |
if (s.exists(is_regex_meta)) { |
64871 | 250 |
(for (c <- s.iterator) |
251 |
yield { if (is_regex_meta(c)) "\\" + c.toString else c.toString }).mkString |
|
252 |
} |
|
253 |
else s |
|
254 |
||
59224 | 255 |
|
61883
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
wenzelm
parents:
60215
diff
changeset
|
256 |
/* lists */ |
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
wenzelm
parents:
60215
diff
changeset
|
257 |
|
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
wenzelm
parents:
60215
diff
changeset
|
258 |
def take_prefix[A](pred: A => Boolean, xs: List[A]): (List[A], List[A]) = |
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
wenzelm
parents:
60215
diff
changeset
|
259 |
(xs.takeWhile(pred), xs.dropWhile(pred)) |
56686 | 260 |
|
75393 | 261 |
def take_suffix[A](pred: A => Boolean, xs: List[A]): (List[A], List[A]) = { |
67434 | 262 |
val rev_xs = xs.reverse |
263 |
(rev_xs.dropWhile(pred).reverse, rev_xs.takeWhile(pred).reverse) |
|
264 |
} |
|
265 |
||
266 |
def trim[A](pred: A => Boolean, xs: List[A]): List[A] = |
|
267 |
take_suffix(pred, take_prefix(pred, xs)._2)._1 |
|
268 |
||
60215 | 269 |
def member[A, B](xs: List[A])(x: B): Boolean = xs.contains(x) |
56688 | 270 |
def insert[A](x: A)(xs: List[A]): List[A] = if (xs.contains(x)) xs else x :: xs |
271 |
def remove[A, B](x: B)(xs: List[A]): List[A] = if (member(xs)(x)) xs.filterNot(_ == x) else xs |
|
272 |
def update[A](x: A)(xs: List[A]): List[A] = x :: remove(x)(xs) |
|
63734
133e3e84e6fb
some support for merge of Isabelle/jEdit shortcuts wrt. jEdit keymap;
wenzelm
parents:
62590
diff
changeset
|
273 |
|
63867 | 274 |
def merge[A](xs: List[A], ys: List[A]): List[A] = |
275 |
if (xs.eq(ys)) xs |
|
276 |
else if (xs.isEmpty) ys |
|
277 |
else ys.foldRight(xs)(Library.insert(_)(_)) |
|
278 |
||
75393 | 279 |
def distinct[A](xs: List[A], eq: (A, A) => Boolean = (x: A, y: A) => x == y): List[A] = { |
63734
133e3e84e6fb
some support for merge of Isabelle/jEdit shortcuts wrt. jEdit keymap;
wenzelm
parents:
62590
diff
changeset
|
280 |
val result = new mutable.ListBuffer[A] |
64207 | 281 |
xs.foreach(x => if (!result.exists(y => eq(x, y))) result += x) |
63734
133e3e84e6fb
some support for merge of Isabelle/jEdit shortcuts wrt. jEdit keymap;
wenzelm
parents:
62590
diff
changeset
|
282 |
result.toList |
133e3e84e6fb
some support for merge of Isabelle/jEdit shortcuts wrt. jEdit keymap;
wenzelm
parents:
62590
diff
changeset
|
283 |
} |
63781 | 284 |
|
75393 | 285 |
def duplicates[A](lst: List[A], eq: (A, A) => Boolean = (x: A, y: A) => x == y): List[A] = { |
63781 | 286 |
val result = new mutable.ListBuffer[A] |
287 |
@tailrec def dups(rest: List[A]): Unit = |
|
288 |
rest match { |
|
289 |
case Nil => |
|
290 |
case x :: xs => |
|
64207 | 291 |
if (!result.exists(y => eq(x, y)) && xs.exists(y => eq(x, y))) result += x |
63781 | 292 |
dups(xs) |
293 |
} |
|
294 |
dups(lst) |
|
295 |
result.toList |
|
296 |
} |
|
65761 | 297 |
|
68715 | 298 |
def replicate[A](n: Int, a: A): List[A] = |
299 |
if (n < 0) throw new IllegalArgumentException |
|
300 |
else if (n == 0) Nil |
|
301 |
else { |
|
302 |
val res = new mutable.ListBuffer[A] |
|
303 |
(1 to n).foreach(_ => res += a) |
|
304 |
res.toList |
|
305 |
} |
|
306 |
||
78943 | 307 |
def the_single[A](xs: List[A], message: => String = "Single argument expected"): A = |
73571 | 308 |
xs match { |
309 |
case List(x) => x |
|
78943 | 310 |
case _ => error(message) |
73571 | 311 |
} |
312 |
||
65761 | 313 |
|
314 |
/* proper values */ |
|
315 |
||
75295
38398766be6b
command-line arguments for "isabelle vscode", similar to "isabelle jedit";
wenzelm
parents:
74794
diff
changeset
|
316 |
def proper_bool(b: Boolean): Option[Boolean] = |
38398766be6b
command-line arguments for "isabelle vscode", similar to "isabelle jedit";
wenzelm
parents:
74794
diff
changeset
|
317 |
if (!b) None else Some(b) |
38398766be6b
command-line arguments for "isabelle vscode", similar to "isabelle jedit";
wenzelm
parents:
74794
diff
changeset
|
318 |
|
65761 | 319 |
def proper_string(s: String): Option[String] = |
320 |
if (s == null || s == "") None else Some(s) |
|
321 |
||
322 |
def proper_list[A](list: List[A]): Option[List[A]] = |
|
323 |
if (list == null || list.isEmpty) None else Some(list) |
|
72214 | 324 |
|
77367 | 325 |
def if_proper[A](x: Iterable[A], body: => String): String = |
326 |
if (x == null || x.isEmpty) "" else body |
|
327 |
||
77614 | 328 |
def if_proper(b: Boolean, body: => String): String = |
329 |
if (!b) "" else body |
|
330 |
||
72214 | 331 |
|
332 |
/* reflection */ |
|
333 |
||
75393 | 334 |
def is_subclass[A, B](a: Class[A], b: Class[B]): Boolean = { |
73339 | 335 |
import scala.language.existentials |
75393 | 336 |
@tailrec def subclass(c: Class[_]): Boolean = { |
337 |
c == b || { val d = c.getSuperclass; d != null && subclass(d) } |
|
72214 | 338 |
} |
339 |
subclass(a) |
|
340 |
} |
|
76788 | 341 |
|
342 |
def as_subclass[C](c: Class[C])(x: AnyRef): Option[C] = |
|
343 |
if (x == null || is_subclass(x.getClass, c)) Some(x.asInstanceOf[C]) else None |
|
34136 | 344 |
} |