static ByteList quote19(ByteList bs, boolean asciiOnly) {
int p = bs.getBegin();
int end = p + bs.getRealSize();
byte[]bytes = bs.getUnsafeBytes();
Encoding enc = bs.getEncoding();
metaFound: do {
while (p < end) {
final int c;
final int cl;
if (enc.isAsciiCompatible()) {
cl = 1;
c = bytes[p] & 0xff;
} else {
cl = StringSupport.preciseLength(enc, bytes, p, end);
c = enc.mbcToCode(bytes, p, end);
}
if (!Encoding.isAscii(c)) {
p += StringSupport.length(enc, bytes, p, end);
continue;
}
switch (c) {
case '[': case ']': case '{': case '}':
case '(': case ')': case '|': case '-':
case '*': case '.': case '\\':
case '?': case '+': case '^': case '$':
case ' ': case '#':
case '\t': case '\f': case '\n': case '\r':
break metaFound;
}
p += cl;
}
if (asciiOnly) {
ByteList tmp = bs.shallowDup();
tmp.setEncoding(USASCIIEncoding.INSTANCE);
return tmp;
}
return bs;
} while (false);
ByteList result = new ByteList(end * 2);
result.setEncoding(asciiOnly ? USASCIIEncoding.INSTANCE : bs.getEncoding());
byte[]obytes = result.getUnsafeBytes();
int op = p - bs.getBegin();
System.arraycopy(bytes, bs.getBegin(), obytes, 0, op);
while (p < end) {
final int c;
final int cl;
if (enc.isAsciiCompatible()) {
cl = 1;
c = bytes[p] & 0xff;
} else {
cl = StringSupport.preciseLength(enc, bytes, p, end);
c = enc.mbcToCode(bytes, p, end);
}
if (!Encoding.isAscii(c)) {
int n = StringSupport.length(enc, bytes, p, end);
while (n-- > 0) obytes[op++] = bytes[p++];
continue;
}
p += cl;
switch (c) {
case '[': case ']': case '{': case '}':
case '(': case ')': case '|': case '-':
case '*': case '.': case '\\':
case '?': case '+': case '^': case '$':
case '#':
op += enc.codeToMbc('\\', obytes, op);
break;
case ' ':
op += enc.codeToMbc('\\', obytes, op);
op += enc.codeToMbc(' ', obytes, op);
continue;
case '\t':
op += enc.codeToMbc('\\', obytes, op);
op += enc.codeToMbc('t', obytes, op);
continue;
case '\n':
op += enc.codeToMbc('\\', obytes, op);
op += enc.codeToMbc('n', obytes, op);
continue;
case '\r':
op += enc.codeToMbc('\\', obytes, op);
op += enc.codeToMbc('r', obytes, op);
continue;
case '\f':
op += enc.codeToMbc('\\', obytes, op);
op += enc.codeToMbc('f', obytes, op);
continue;
}
op += enc.codeToMbc(c, obytes, op);
}
result.setRealSize(op);
return result;
}