-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext processing.a68
112 lines (87 loc) · 1.92 KB
/
text processing.a68
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
# -*- coding: utf-8 -*- #
OP TOINT = (STRING str) INT: (
INT n := 0, sign := 1;
FOR i TO UPB str WHILE sign /= 0 DO
IF is digit(str[i]) THEN n := n * 10 + ABS str[i] - ABS "0"
ELIF i = 1 AND str[i] = "-" THEN sign := -1
ELIF i /= 1 OR str[i] /= "+" THEN sign := 0
FI
OD;
n * sign
);
PROC sh scape = (STRING str, CHAR ch) STRING: (
STRING text := str;
INT text len := UPB text, i := 1;
WHILE i <= text len DO
IF text[i] = ch THEN
text := text[:i - 1] + "\" + text[i:];
text len +:= 1;
i +:= 1
FI;
i +:= 1
OD;
text
);
PROC strchr = (STRING str, CHAR ch) INT: (
INT pos := UPB str;
FOR i TO UPB str DO
IF str[i] = ch THEN
pos := i;
done
FI
OD;
done: pos
);
PROC join = ([] STRING list, STRING delim) STRING: (
STRING res;
FOR i TO UPB list DO
res +:= list[i] + (i < UPB list | delim | "")
OD;
res
);
PROC wrap text = (STRING text, INT width) [] STRING: (
FLEX [0] STRING parts;
STRING part;
INT text i := 1;
WHILE text i < UPB text DO
INT
next space = strchr(text[text i:], " "),
next dash = strchr(text[text i:], "-");
INT next := (next space < next dash | next space | next dash);
IF UPB part + next - 1 > width THEN
IF next > width THEN
INT len = width - UPB part - 1;
parts +:= part + text[text i:][:len] + "-";
next := len;
part := ""
ELSE
parts +:= part;
IF part[UPB part] = " " THEN
parts[UPB parts] := part[:UPB part - 1]
FI;
part := text[text i:][:next]
FI
ELSE
part +:= text[text i:][:next]
FI;
text i +:= next
OD;
parts +:= part
);
PROC center text = (STRING text, INT width) STRING: (
STRING new := text;
INT missing = width - UPB text;
IF missing > 0 THEN
REAL part = missing / 2;
INT
left := ENTIER part,
right := left + (part - left > 0 | 1 | 0);
IF right > left THEN
left +:= 1;
right -:= 1
FI;
TO left DO new := " " + new OD;
TO right DO new := new + " " OD
FI;
new
)