forked from ballerina-platform/nballerina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.bal
204 lines (185 loc) · 6.36 KB
/
string.bal
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Implementation specific to basic type string.
public type StringSubtype readonly & record {|
CharStringSubtype char;
NonCharStringSubtype nonChar;
|};
// allowed = false with empty `values` means `String:char`
public type CharStringSubtype readonly & record {|
boolean allowed;
string:Char[] values;
|};
// allowed = false with empty `values` mean all non char strings
public type NonCharStringSubtype readonly & record {|
boolean allowed;
string[] values;
|};
public function stringConst(string value) returns ComplexSemType {
CharStringSubtype char;
NonCharStringSubtype nonChar;
if value.length() == 1 {
char = { allowed: true, values: [<string:Char>value] };
nonChar = { allowed: true, values: [] };
}
else {
char = { allowed: true, values: [] };
nonChar = { allowed: true, values: [value] };
}
return basicSubtype(BT_STRING, { char, nonChar });
}
function stringChar() returns SemType {
StringSubtype st = {
char: { allowed: false, values: [] },
nonChar: { allowed: true, values: [] }
};
return basicSubtype(BT_STRING, st);
}
function stringSubtypeSingleValue(SubtypeData d) returns string? {
if d is boolean {
return ();
}
var { char, nonChar } = <StringSubtype>d;
int charCount = char.allowed ? char.values.length() : 2;
int nonCharCount = nonChar.allowed ? nonChar.values.length() : 2;
if charCount + nonCharCount == 1 {
return charCount != 0 ? char.values[0] : nonChar.values[0];
}
return ();
}
function stringSubtypeContains(SubtypeData d, string s) returns boolean {
if d is boolean {
return d;
}
StringSubtype st = <StringSubtype>d;
var { char, nonChar } = st;
if s.length() == 1 {
return char.values.indexOf(<string:Char>s) != () ? char.allowed : !char.allowed;
}
return nonChar.values.indexOf(s) != () ? nonChar.allowed : !nonChar.allowed;
}
// Describes the relationship between a StringSubtype and a list of strings
// How the StringSubtype covers the list and vice versa.
type StringSubtypeListCoverage record {|
// true if the StringSubtype is a subtype of the type containing the strings in the lists
boolean isSubtype;
// contains the index in order of each member of the list that is in the StringSubtype
int[] indices;
|};
// Returns a description of the relationship between a StringSubtype and a list of strings
// `values` must be ordered.
function stringSubtypeListCoverage(StringSubtype subtype, string[] values) returns StringSubtypeListCoverage {
int[] indices = [];
var { char, nonChar } = subtype;
int stringConsts = 0;
if char.allowed {
stringListIntersect(values, char.values, indices);
stringConsts = char.values.length();
}
else if char.values.length() == 0 {
foreach var i in 0 ..< values.length() {
if values[i].length() == 1 {
indices.push(i);
}
}
}
if nonChar.allowed {
stringListIntersect(values, nonChar.values, indices);
stringConsts += nonChar.values.length();
}
else if nonChar.values.length() == 0 {
foreach var i in 0 ..< values.length() {
if values[i].length() != 1 {
indices.push(i);
}
}
}
return { isSubtype: stringConsts == indices.length(), indices };
}
function stringListIntersect(string[] values, string[] target, int[] indices) {
int i1 = 0;
int i2 = 0;
int len1 = values.length();
int len2 = target.length();
while true {
if i1 >= len1 || i2 >= len2 {
break;
}
else {
match compareEnumerable(values[i1], target[i2]) {
EQ => {
indices.push(i1);
i1 += 1;
i2 += 1;
}
LT => {
i1 += 1;
}
GT => {
i2 += 1;
}
}
}
}
}
function stringSubtypeUnion(SubtypeData d1, SubtypeData d2) returns SubtypeData {
StringSubtype sd1 = <StringSubtype>d1;
StringSubtype sd2 = <StringSubtype>d2;
string:Char[] chars = [];
string[] nonChars = [];
boolean charsAllowed = enumerableSubtypeUnion(sd1.char, sd2.char, chars);
boolean nonCharsAllowed = enumerableSubtypeUnion(sd1.nonChar, sd2.nonChar, nonChars);
return createStringSubtype(
{ allowed: charsAllowed, values: chars.cloneReadOnly() },
{ allowed: nonCharsAllowed, values: nonChars.cloneReadOnly() }
);
}
function stringSubtypeIntersect(SubtypeData d1, SubtypeData d2) returns SubtypeData {
if d1 is boolean {
return d1 == true ? d2 : false;
}
if d2 is boolean {
return d2 == true ? d1 : false;
}
StringSubtype sd1 = <StringSubtype>d1;
StringSubtype sd2 = <StringSubtype>d2;
string:Char[] chars = [];
string[] nonChars = [];
boolean charsAllowed = enumerableSubtypeIntersect(sd1.char, sd2.char, chars);
boolean nonCharsAllowed = enumerableSubtypeIntersect(sd1.nonChar, sd2.nonChar, nonChars);
return createStringSubtype(
{ allowed: charsAllowed, values: chars.cloneReadOnly() },
{ allowed: nonCharsAllowed, values: nonChars.cloneReadOnly() }
);
}
function stringSubtypeComplement(SubtypeData d) returns SubtypeData {
var {char, nonChar} = <StringSubtype>d;
if char.values.length() == 0 && nonChar.values.length() == 0 {
if char.allowed && nonChar.allowed {
return true;
}
else if !char.allowed && !nonChar.allowed {
return false;
}
}
return createStringSubtype(
{ allowed: !char.allowed, values: char.values },
{ allowed: !nonChar.allowed, values: nonChar.values }
);
}
function createStringSubtype(CharStringSubtype char, NonCharStringSubtype nonChar) returns SubtypeData {
if char.values.length() == 0 && nonChar.values.length() == 0 {
if !char.allowed && !nonChar.allowed {
return true;
}
else if char.allowed && nonChar.allowed {
return false;
}
}
return { char, nonChar };
}
final BasicTypeOps stringOps = {
union: stringSubtypeUnion,
intersect: stringSubtypeIntersect,
complement: stringSubtypeComplement,
// Empty string sets don't use subtype representation.
isEmpty: notIsEmpty
};