38425
|
1 |
/* Title: Pure/PIDE/text.scala
|
34276
|
2 |
Author: Fabian Immler, TU Munich
|
|
3 |
Author: Makarius
|
|
4 |
|
38425
|
5 |
Basic operations on plain text.
|
34276
|
6 |
*/
|
|
7 |
|
|
8 |
package isabelle
|
|
9 |
|
|
10 |
|
38425
|
11 |
object Text
|
34276
|
12 |
{
|
38477
|
13 |
/* offset */
|
38426
|
14 |
|
|
15 |
type Offset = Int
|
|
16 |
|
38477
|
17 |
|
|
18 |
/* range: interval with total quasi-ordering */
|
|
19 |
|
38426
|
20 |
sealed case class Range(val start: Offset, val stop: Offset)
|
38427
|
21 |
{
|
38477
|
22 |
require(start <= stop)
|
|
23 |
|
38563
|
24 |
override def toString = "[" + start.toString + ":" + stop.toString + "]"
|
|
25 |
|
38427
|
26 |
def map(f: Offset => Offset): Range = Range(f(start), f(stop))
|
|
27 |
def +(i: Offset): Range = map(_ + i)
|
38477
|
28 |
def contains(i: Offset): Boolean = start <= i && i < stop
|
38485
|
29 |
def contains(that: Range): Boolean =
|
|
30 |
this == that || this.contains(that.start) && that.stop <= this.stop
|
|
31 |
def overlaps(that: Range): Boolean =
|
|
32 |
this == that || this.contains(that.start) || that.contains(this.start)
|
38477
|
33 |
def compare(that: Range): Int =
|
|
34 |
if (overlaps(that)) 0
|
|
35 |
else if (this.start < that.start) -1
|
|
36 |
else 1
|
38485
|
37 |
|
|
38 |
def start_range: Range = Range(start, start)
|
|
39 |
def stop_range: Range = Range(stop, stop)
|
|
40 |
|
38564
|
41 |
def restrict(that: Range): Range =
|
38485
|
42 |
Range(this.start max that.start, this.stop min that.stop)
|
38427
|
43 |
}
|
38426
|
44 |
|
|
45 |
|
|
46 |
/* editing */
|
34286
|
47 |
|
38425
|
48 |
object Edit
|
|
49 |
{
|
38426
|
50 |
def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
|
|
51 |
def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
|
38425
|
52 |
}
|
34286
|
53 |
|
38426
|
54 |
class Edit(val is_insert: Boolean, val start: Offset, val text: String)
|
38425
|
55 |
{
|
|
56 |
override def toString =
|
|
57 |
(if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
|
34286
|
58 |
|
|
59 |
|
38425
|
60 |
/* transform offsets */
|
34286
|
61 |
|
38426
|
62 |
private def transform(do_insert: Boolean, i: Offset): Offset =
|
|
63 |
if (i < start) i
|
|
64 |
else if (is_insert == do_insert) i + text.length
|
|
65 |
else (i - text.length) max start
|
34286
|
66 |
|
38426
|
67 |
def convert(i: Offset): Offset = transform(true, i)
|
|
68 |
def revert(i: Offset): Offset = transform(false, i)
|
38425
|
69 |
|
34286
|
70 |
|
38425
|
71 |
/* edit strings */
|
|
72 |
|
38426
|
73 |
private def insert(i: Offset, string: String): String =
|
|
74 |
string.substring(0, i) + text + string.substring(i)
|
34276
|
75 |
|
38426
|
76 |
private def remove(i: Offset, count: Int, string: String): String =
|
|
77 |
string.substring(0, i) + string.substring(i + count)
|
38425
|
78 |
|
|
79 |
def can_edit(string: String, shift: Int): Boolean =
|
|
80 |
shift <= start && start < shift + string.length
|
|
81 |
|
|
82 |
def edit(string: String, shift: Int): (Option[Edit], String) =
|
|
83 |
if (!can_edit(string, shift)) (Some(this), string)
|
|
84 |
else if (is_insert) (None, insert(start - shift, string))
|
|
85 |
else {
|
38426
|
86 |
val i = start - shift
|
|
87 |
val count = text.length min (string.length - i)
|
38425
|
88 |
val rest =
|
|
89 |
if (count == text.length) None
|
|
90 |
else Some(Edit.remove(start, text.substring(count)))
|
38426
|
91 |
(rest, remove(i, count, string))
|
38425
|
92 |
}
|
|
93 |
}
|
34276
|
94 |
}
|