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