-
Notifications
You must be signed in to change notification settings - Fork 1
/
glsl-shortener.java
166 lines (129 loc) · 4.9 KB
/
glsl-shortener.java
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
public static final char[] WHITE_SPACE = " \t\r\n".toCharArray();
public static final char[] GLSL_SPECIAL_CHAR = ";,.[](){}-+/*=><^&|!: \t\r\n".toCharArray();
public static String minimalGLSL(String s){
StringBuilder parsed = new StringBuilder();
s = s.trim();
boolean directiveAddLine = true;
main:for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
//check for comments
if (i < s.length() - 1 && c == '/' && s.charAt(i + 1) == '/'){
i += 2;
boolean end = false;
for (int ii = i; ii < s.length(); ii++){
char cc = s.charAt(ii);
boolean white = false;
if (!end){
end = cc == '\r' || cc == '\n';
}
for (int iii = 0; iii < WHITE_SPACE.length; iii++){
if (WHITE_SPACE[iii] == cc){
white = true;
}
}
if (white){
i++;
}else if (end){
break;
}else{
i++;
}
}
i--;
continue main;
}
//check for any directives
if (c == '#'){
//find next line break and insert line breaks where needed
int ii;
for (ii = i + 1; ii < s.length(); ii++){
char cc = s.charAt(ii);
if (cc == '\n' || cc == '\r'){
break;
}
}
if (directiveAddLine){
parsed.append("\\r\\n");
}else{
directiveAddLine = true;
}
parsed.append(s.substring(i, ii));
parsed.append("\\r\\n");
//find next valid character
for (; ii < s.length(); ii++){
char cc = s.charAt(ii);
boolean cwhite = false;
for (int iii = 0; iii < WHITE_SPACE.length; iii++){
if (WHITE_SPACE[iii] == cc){
cwhite = true;
break;
}
}
if (!cwhite){
if (s.charAt(ii) == '#'){
directiveAddLine = false;
}
ii--;
break;
}
}
i = ii;
}else{
boolean special = false;
for (int ii = 0; ii < GLSL_SPECIAL_CHAR.length; ii++){
if (GLSL_SPECIAL_CHAR[ii] == c){
special = true;
break;
}
}
if (special){
boolean cwhite = false;
for (int ii = 0; ii < WHITE_SPACE.length; ii++){
if (WHITE_SPACE[ii] == c){
cwhite = true;
break;
}
}
if (parsed.length() > 0){
int ii;
for (ii = parsed.length() - 1; ii >= 0; ii--){
boolean white = false;
for (int iii = 0; iii < WHITE_SPACE.length; iii++){
if (WHITE_SPACE[iii] == parsed.charAt(ii)){
white = true;
break;
}
}
if (!white){
ii++;
break;
}
}
parsed.setLength(ii);
}
for (int ii = i + 1; ii < s.length(); ii++){
boolean white = false;
for (int iii = 0; iii < WHITE_SPACE.length; iii++){
if (WHITE_SPACE[iii] == s.charAt(ii)){
white = true;
break;
}
}
if (white){
i++;
}else{
break;
}
}
if (cwhite){
parsed.append(' ');
}else{
parsed.append(c);
}
}else{
parsed.append(c);
}
}
}
return parsed.toString();
}