-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathinternal.gs
More file actions
331 lines (280 loc) · 7.44 KB
/
internal.gs
File metadata and controls
331 lines (280 loc) · 7.44 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// return array length
int len(any[] a){}
// return array cap
int cap(any[] a){}
// copies elements from a source array into a destination array.
int copy(byte[] dst, byte[] src){}
// return hashcode
int hash(any s){}
println(any a){}
//formats according to a format specifier and writes to standard output.
printf(string format, any ...a){}
//formats according to a format specifier and returns the resulting string.
string sprintf(string format, any ...a){}
// formats using the default formats for its operands and writes to standard output.
print(any ...a){}
assertEqual(any a1, any a2){}
// covert string to byte array.
byte[] toByteArray(string s){}
// covert byte array to string.
string toString(byte[] b){}
class StringBuilder{
byte[] buf = [0]{};
// append contents to buf, it returns the length of s
int writeString(string s){
byte[] temp = toByteArray(s);
append(buf, temp);
return len(temp);
}
// append b to buf, it returns the length of b.
int WriteBytes(byte[] b){
append(buf, b);
return len(b);
}
// copies the buffer to a new.
grow(int n){
if (n > 0) {
// when there is not enough space left.
if (cap(buf) - len(buf) < n) {
byte[] newBuf = [len(buf), 2*cap(buf)+n]{};
copy(newBuf, buf);
buf = newBuf;
}
}
}
string String(){
return toString(buf);
}
}
class Strings{
// concatenates the elements of its first argument to create a single string. The separator
// string sep is placed between elements in the resulting string.
string join(string[] elems, string sep){
if (len(elems) == 0) {
return "";
}
if (len(elems) == 1) {
return elems[0];
}
byte[] bs = toByteArray(sep);
int n = len(bs) * (len(elems) -1);
for (int i=0; i < len(elems); i++) {
string s = elems[i];
byte[] bs = toByteArray(s);
n = n + len(bs);
}
StringBuilder sb = StringBuilder();
sb.grow(n);
string first = elems[0];
sb.writeString(first);
string[] remain = elems[1:len(elems)];
for(int i=0; i < len(remain); i++){
sb.writeString(sep);
string r = remain[i];
sb.writeString(r);
}
return sb.String();
}
// tests whether the string s begins with prefix.
bool hasPrefix(string s, string prefix){
byte[] bs = toByteArray(s);
byte[] bp = toByteArray(prefix);
return len(bs) >= len(bp) && toString(bs[0:len(bp)]) == prefix;
}
}
// appends "v" to the end of a array "a"
append(any[] a, any v){}
// return JSON string
string JSON(any a){}
// JSON query with path
any JSONGet(string json, string path){}
// HTTP lib
// Response json
FprintfJSON(int code, string path, string json){}
// Resonse html
FprintfHTML(int code, string path, string html){}
// path (relative paths may omit leading slash)
string QueryPath(string path){}
// returns the first value for the named component of the query.
string FormValue(string path, string key){}
// returns the first value for the named component of the POST
string PostFormValue(string path, string key){}
// return request raw body
string RequestBody(string path){}
class HttpContext{
string path;
JSON(int code, any v){
string json = JSON(v);
FprintfJSON(code, path, json);
}
HTML(int code, any v) {
string html = v;
FprintfHTML(code, path, html);
}
string queryPath() {
string p = QueryPath(path);
return p;
}
string formValue(string key){
string v = FormValue(path, key);
return v;
}
string postFormValue(string key){
string v = PostFormValue(path, key);
return v;
}
string requestBody(){
string body = RequestBody(path);
return body;
}
}
// Bind route
httpHandle(string method, string path, func (HttpContext) handle){
// println("path="+path);
HttpContext ctx = HttpContext();
handle(ctx);
}
// Run http server.
httpRun(string addr){}
// System os api
string[] GetOSArgs(){}
string Command(string name, string ...arg){}
WriteFile(string fileName, string value, int perm){}
Remove(string fileName){}
string Getwd(){}
// system os api
class System{
// Get os args.
string[] getOSArgs(){
return GetOSArgs();
}
string command(string name, string ...arg){
return Command(name, arg);
}
// attention: perm is the decimal
writeFile(string fileName, string value, int perm){
WriteFile(fileName, value, perm);
}
// removes the named file
remove(string fileName){
Remove(fileName);
}
// returns a rooted path name corresponding to the current directory.
string getwd(){
return Getwd();
}
}
// Date
string GetCurrentTime(string tz, string layout){}
int Unix(string tz){}
class DateTime{
string getCurrentTime(string tz, string layout){
return GetCurrentTime(tz, layout);
}
int unix(string tz){
return Unix(tz);
}
}
// Compiler api
string dumpAST(string code){}
string dumpSymbol(string code){}
class Entry{
any key,value;
Entry next;
Entry(any k, any v, Entry n){
key=k;
value=v;
next=n;
}
}
class Map{
Entry[] table = [16]{};
int size=0;
put(any key, any value){
// todo crossoverJie 扩容
if(key==""){
return;
}
int hashcode = hash(key);
int i = hashcode % len(table) ;
Entry e = table[i];
if (e != nil){
Entry tempEntry = e;
for (tempEntry != nil) {
int currentHash = hash(tempEntry.key);
if (currentHash == hashcode && key == tempEntry.key){
tempEntry.value= value;
return;
}
tempEntry = tempEntry.next;
}
table[i] = Entry(key, value, e);
size++;
} else {
// 参考 jdk1.7 链表头插法
table[i] = Entry(key,value,nil);
size++;
}
}
any get(any key){
if(key == ""){
return;
}
int hashcode = hash(key);
int i = hashcode % len(table) ;
Entry e = table[i];
for (e.next != nil){
int currentHash = hash(e.key);
if (key == e.key && currentHash == hashcode){
return e.value;
}
e = e.next;
}
if (hash(e.key) == hashcode && key == e.key){
return e.value;
}else {
return nil;
}
return nil;
}
int getSize(){
return size;
}
}
// LinkedList
class LinkedNode{
any value;
LinkedNode next;
LinkedNode(any v){
value = v;
next = nil;
}
}
class LinkedList{
LinkedNode first, last;
int size=0;
add(any v){
if (v == ""){
return;
}
LinkedNode l = LinkedNode(v);
if (first == nil){
first = l;
last = l;
} else{
last.next = l;
last = l;
}
size ++;
}
dump(){
LinkedNode start = first;
for (start != nil){
println(start.value);
start = start.next;
}
}
int getSize(){
return size;
}
}