author | wenzelm |
Fri, 20 Aug 2010 11:00:15 +0200 | |
changeset 38565 | 32b924a832c4 |
parent 38564 | a6e2715fac5f |
child 38568 | f117ba49a59c |
permissions | -rw-r--r-- |
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 |
|
38565
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
18 |
/* range -- with total quasi-ordering */ |
38477 | 19 |
|
38426 | 20 |
sealed case class Range(val start: Offset, val stop: Offset) |
38427 | 21 |
{ |
38565
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
22 |
// denotation: {start} Un {i. start < i & i < stop} |
38477 | 23 |
require(start <= stop) |
24 |
||
38563 | 25 |
override def toString = "[" + start.toString + ":" + stop.toString + "]" |
26 |
||
38427 | 27 |
def map(f: Offset => Offset): Range = Range(f(start), f(stop)) |
28 |
def +(i: Offset): Range = map(_ + i) |
|
38565
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
29 |
def contains(i: Offset): Boolean = start == i || start < i && i < stop |
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
30 |
def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop |
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
31 |
def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start) |
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
32 |
def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start |
38485 | 33 |
|
34 |
def start_range: Range = Range(start, start) |
|
35 |
def stop_range: Range = Range(stop, stop) |
|
36 |
||
38564 | 37 |
def restrict(that: Range): Range = |
38485 | 38 |
Range(this.start max that.start, this.stop min that.stop) |
38427 | 39 |
} |
38426 | 40 |
|
41 |
||
42 |
/* editing */ |
|
34286 | 43 |
|
38425 | 44 |
object Edit |
45 |
{ |
|
38426 | 46 |
def insert(start: Offset, text: String): Edit = new Edit(true, start, text) |
47 |
def remove(start: Offset, text: String): Edit = new Edit(false, start, text) |
|
38425 | 48 |
} |
34286 | 49 |
|
38426 | 50 |
class Edit(val is_insert: Boolean, val start: Offset, val text: String) |
38425 | 51 |
{ |
52 |
override def toString = |
|
53 |
(if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")" |
|
34286 | 54 |
|
55 |
||
38425 | 56 |
/* transform offsets */ |
34286 | 57 |
|
38426 | 58 |
private def transform(do_insert: Boolean, i: Offset): Offset = |
59 |
if (i < start) i |
|
60 |
else if (is_insert == do_insert) i + text.length |
|
61 |
else (i - text.length) max start |
|
34286 | 62 |
|
38426 | 63 |
def convert(i: Offset): Offset = transform(true, i) |
64 |
def revert(i: Offset): Offset = transform(false, i) |
|
38425 | 65 |
|
34286 | 66 |
|
38425 | 67 |
/* edit strings */ |
68 |
||
38426 | 69 |
private def insert(i: Offset, string: String): String = |
70 |
string.substring(0, i) + text + string.substring(i) |
|
34276 | 71 |
|
38426 | 72 |
private def remove(i: Offset, count: Int, string: String): String = |
73 |
string.substring(0, i) + string.substring(i + count) |
|
38425 | 74 |
|
75 |
def can_edit(string: String, shift: Int): Boolean = |
|
76 |
shift <= start && start < shift + string.length |
|
77 |
||
78 |
def edit(string: String, shift: Int): (Option[Edit], String) = |
|
79 |
if (!can_edit(string, shift)) (Some(this), string) |
|
80 |
else if (is_insert) (None, insert(start - shift, string)) |
|
81 |
else { |
|
38426 | 82 |
val i = start - shift |
83 |
val count = text.length min (string.length - i) |
|
38425 | 84 |
val rest = |
85 |
if (count == text.length) None |
|
86 |
else Some(Edit.remove(start, text.substring(count))) |
|
38426 | 87 |
(rest, remove(i, count, string)) |
38425 | 88 |
} |
89 |
} |
|
34276 | 90 |
} |