forked from paulfitz/daff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnit.hx
126 lines (114 loc) · 3.1 KB
/
Unit.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
#if !TOPLEVEL
package coopy;
#end
/**
*
* The row/column number for related content in the local table,
* the remote table, and the parent table (if there is one).
*
*/
class Unit {
/**
*
* The row/column number in the local table.
*
*/
public var l : Int;
/**
*
* The row/column number in the remote table.
*
*/
public var r : Int;
/**
*
* The row/column number in the parent table.
*
*/
public var p : Int;
/**
*
* Constructor.
* @param l the row/column number in the local table (-1 means absent)
* @param r the row/column number in the remote table (-1 means absent)
* @param p the row/column number in the parent table (-1 means absent, -2 means there is no parent)
*
*/
public function new(l: Int = -2, r: Int = -2, p: Int = -2) : Void {
this.l = l;
this.r = r;
this.p = p;
}
/**
*
* @return the row/column number in the parent table if present, otherwise in the local table
*
*/
public function lp() : Int {
return (p==-2) ? l : p;
}
private static function describe(i: Int) : String {
return (i>=0) ? ("" + i) : "-";
}
/**
*
* @return a text serialization of the row/column numbers, as `LL:RR` when the parent is absent, and `PP|LL:RR` when the parent is present
*
*/
public function toString() : String {
if (p>=-1) return describe(p) + "|" + describe(l) + ":" + describe(r);
return describe(l) + ":" + describe(r);
}
/**
*
* Read from a serialized version of the row/column numbers
* @param txt the string to read
* @return true on success
*
*/
public function fromString(txt: String) : Bool {
txt += "]";
var at : Int = 0;
for (i in 0...txt.length) {
var ch : Int = txt.charCodeAt(i);
if (ch>='0'.code && ch<='9'.code) {
at *= 10;
at += ch - '0'.code;
} else if (ch == '-'.code) {
at = -1;
} else if (ch == '|'.code) {
p = at;
at = 0;
} else if (ch == ':'.code) {
l = at;
at = 0;
} else if (ch == ']'.code) {
r = at;
return true;
}
}
return false;
}
private function base26(num: Int) : String {
// thanks @jordigh
var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (num<0) return "-";
var out = "";
do {
out = out + alpha.charAt(num % 26);
num = Math.floor(num / 26) - 1;
} while (num>=0);
return out;
}
/**
*
* @return as for toString(), but representing row/column numbers
* as A,B,C,D,...,AA,AB,AC,AD,....
*
*/
public function toBase26String() : String {
if (p>=-1) return base26(p) + "|" + base26(l) + ":" + base26(r);
return base26(l) + ":" + base26(r);
}
}