-
Notifications
You must be signed in to change notification settings - Fork 914
/
RubyStringBuilder.java
282 lines (223 loc) · 10.1 KB
/
RubyStringBuilder.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
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
package org.jruby.util;
import org.jcodings.Encoding;
import org.jcodings.specific.USASCIIEncoding;
import org.jcodings.unicode.UnicodeEncoding;
import org.jruby.*;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.io.EncodingUtils;
import static org.jruby.util.StringSupport.MBCLEN_CHARFOUND_LEN;
import static org.jruby.util.StringSupport.MBCLEN_CHARFOUND_P;
import static org.jruby.util.StringSupport.codePoint;
/**
* Helper methods to make Ruby Strings without the ceremony of manually building the string up.
*/
public class RubyStringBuilder {
public static RubyString types(ThreadContext context, RubyModule type) {
return inspectIdentifierByteList(context.runtime, type.toRubyString(context).getByteList());
}
public static RubyString types(Ruby runtime, RubyModule type) {
ThreadContext context = runtime.getCurrentContext();
return inspectIdentifierByteList(runtime, type.toRubyString(context).getByteList());
}
public static RubyString types(Ruby runtime, RubyModule type1, RubyModule type2) {
ThreadContext context = runtime.getCurrentContext();
RubyString fullTypeName = type1.toRubyString(context).catString("::").cat(type2.toRubyString(context));
return inspectIdentifierByteList(runtime, fullTypeName.getByteList());
}
public static RubyString ids(Ruby runtime, String id) {
ByteList identifier = runtime.newSymbol(id).getBytes();
return inspectIdentifierByteList(runtime, identifier);
}
/**
* Convert object to proper Ruby String representation of an id. This will not work for any string.
* It must represent an identifier the systems knows about.
*
* @param runtime
* @param object
*/
public static RubyString ids(Ruby runtime, IRubyObject object) {
ByteList identifier;
if (object instanceof RubyString) {
identifier = ((RubyString) object).getByteList();
} else if (object instanceof RubySymbol) {
identifier = ((RubySymbol) object).getBytes();
} else {
identifier = object.convertToString().getByteList();
}
return inspectIdentifierByteList(runtime, identifier);
}
public static RubyString inspectIdentifierByteList(final Ruby runtime, ByteList byteList) {
Encoding enc = byteList.getEncoding();
byte bytes[] = byteList.getUnsafeBytes();
int p = byteList.getBegin();
int end = p + byteList.getRealSize();
RubyString result = RubyString.newStringLight(runtime, new ByteList(end - p + 2));
result.getByteList().setBegin(1); // just in-case we need to prepend a '"'
Encoding resultEnc = runtime.getDefaultInternalEncoding();
boolean isUnicode = enc.isUnicode();
boolean asciiCompat = enc.isAsciiCompatible();
boolean needsQuotes = false;
if (resultEnc == null) resultEnc = runtime.getDefaultExternalEncoding();
if (!resultEnc.isAsciiCompatible()) resultEnc = USASCIIEncoding.INSTANCE;
result.associateEncoding(resultEnc);
int prev = p;
Encoding actEnc = EncodingUtils.getActualEncoding(enc, byteList);
if (actEnc != enc) {
enc = actEnc;
if (isUnicode) isUnicode = enc instanceof UnicodeEncoding;
}
while (p < end) {
int n = StringSupport.preciseLength(enc, bytes, p, end);
if (!MBCLEN_CHARFOUND_P(n)) {
if (p > prev) result.cat(bytes, prev, p - prev);
n = enc.minLength();
if (end < p + n) n = end - p;
while (n-- > 0) {
result.modifyExpand(result.size() + 4);
Sprintf.sprintf(runtime, result.getByteList() ,"\\x%02X", bytes[p] & 0377);
prev = ++p;
}
continue;
}
n = MBCLEN_CHARFOUND_LEN(n);
final int c = enc.mbcToCode(bytes, p, end); int cc = 0;
p += n;
if ((asciiCompat || isUnicode) &&
(c == '"' || c == '\\' ||
(c == '#' &&
p < end &&
MBCLEN_CHARFOUND_P(StringSupport.preciseLength(enc, bytes, p, end)) &&
((cc = codePoint(runtime, enc, bytes, p, end)) == '$' ||
cc == '@' || cc == '{')
)
)) {
if (p - n > prev) result.cat(bytes, prev, p - n - prev);
result.cat('\\');
if (asciiCompat || enc == resultEnc) {
prev = p - n;
continue;
}
}
switch (c) {
case '\n': cc = 'n'; break;
case '\r': cc = 'r'; break;
case '\t': cc = 't'; break;
case '\f': cc = 'f'; break;
case '\013': cc = 'v'; break;
case '\010': cc = 'b'; break;
case '\007': cc = 'a'; break;
case 033: cc = 'e'; break;
default: cc = 0; break;
}
if (cc != 0) {
if (p - n > prev) result.cat(bytes, prev, p - n - prev);
result.cat('\\');
result.cat(cc);
prev = p;
continue;
}
// FIXME: Can't use Encoding.isAscii because it does not treat int as unsigned 32-bit
if ((enc == resultEnc && enc.isPrint(c)) || (asciiCompat && (c < 128 && c > 0) && enc.isPrint(c))) {
continue;
} else {
if (p - n > prev) result.cat(bytes, prev, p - n - prev);
String format = StringSupport.escapedCharFormat(c, isUnicode);
if (!format.equals("%c")) needsQuotes = true;
Sprintf.sprintf(runtime, result.getByteList() ,format, (c & 0xFFFFFFFFL));
prev = p;
continue;
}
}
if (p > prev) result.cat(bytes, prev, p - prev);
if (needsQuotes) {
result.prepend('"');
result.cat('"');
}
return result;
}
public static String str(Ruby runtime, IRubyObject value, String message) {
RubyString buf = (RubyString) value.asString().dup();
cat(runtime, buf, message);
return buf.toString();
}
public static String str(Ruby runtime, String message, IRubyObject value) {
RubyString buf = runtime.newString(message);
buf.cat19(value.asString());
return buf.toString();
}
public static String str(Ruby runtime, String messageBegin, IRubyObject value, String messageEnd) {
RubyString buf = runtime.newString(messageBegin);
buf.cat19(value.asString());
cat(runtime, buf, messageEnd);
return buf.toString();
}
public static String str(Ruby runtime, IRubyObject value, String message, IRubyObject value2) {
RubyString buf = (RubyString) value.asString().dup();
cat(runtime, buf, message);
buf.cat19(value2.asString());
return buf.toString();
}
public static String str(Ruby runtime, IRubyObject value, String message, IRubyObject value2, String message2) {
RubyString buf = (RubyString) value.asString().dup();
cat(runtime, buf, message);
buf.cat19(value2.asString());
cat(runtime, buf, message2);
return buf.toString();
}
public static String str(Ruby runtime, String messageBegin, IRubyObject value, String messageMiddle, IRubyObject value2) {
RubyString buf = runtime.newString(messageBegin);
buf.cat19(value.asString());
cat(runtime, buf, messageMiddle);
buf.cat19(value2.asString());
return buf.toString();
}
public static String str(Ruby runtime, String messageBegin, IRubyObject value, String messageMiddle, IRubyObject value2, String messageEnd) {
RubyString buf = runtime.newString(messageBegin);
buf.cat19(value.asString());
cat(runtime, buf, messageMiddle);
buf.cat19(value2.asString());
cat(runtime, buf, messageEnd);
return buf.toString();
}
public static String str(Ruby runtime, String messageBegin, IRubyObject value, String messageMiddle, IRubyObject value2,
String messageMiddle2, IRubyObject value3, String messageEnd) {
RubyString buf = runtime.newString(messageBegin);
buf.cat19(value.asString());
cat(runtime, buf, messageMiddle);
buf.cat19(value2.asString());
cat(runtime, buf, messageMiddle2);
buf.cat19(value3.asString());
cat(runtime, buf, messageEnd);
return buf.toString();
}
public static String str(Ruby runtime, String messageBegin, IRubyObject value, String messageMiddle, RubyString value2,
String messageMiddle2, IRubyObject value3, String messageMiddle3, RubyString value4, String messageEnd) {
RubyString buf = runtime.newString(messageBegin);
buf.cat19(value.asString());
cat(runtime, buf, messageMiddle);
buf.cat19(value2);
cat(runtime, buf, messageMiddle2);
buf.cat19(value3.asString());
cat(runtime, buf, messageMiddle3);
buf.cat19(value4);
cat(runtime, buf, messageEnd);
return buf.toString();
}
public static RubyString cat(final Ruby runtime, final RubyString str, final String value) {
EncodingUtils.encStrBufCat(runtime, str, value);
return str;
}
public static RubyString cat(final Ruby runtime, RubyString buf, int b) {
EncodingUtils.encStrBufCat(runtime, buf, new byte[] { (byte) b });
return buf;
}
public static RubyString cat(final Ruby runtime, RubyString buf, byte[] bytes) {
EncodingUtils.encStrBufCat(runtime, buf, bytes);
return buf;
}
public static RubyString cat(final Ruby runtime, RubyString buf, ByteList bytes) {
EncodingUtils.encStrBufCat(runtime, buf, bytes);
return buf;
}
}