}
@Override
public StringValue encode(Env env, CharSequence str)
{
StringValue sb = env.createBinaryBuilder();
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch <= 0x7F) {
sb.appendByte(ch);
continue;
}
int code = ch;
if (0xD800 <= ch && ch <= 0xDBFF) {
char ch2;
if (i + 1 < len
&& 0xDC00 <= (ch2 = str.charAt(i + 1)) && ch2 <= 0xDFFF) {
i++;
code = 0x10000 + ((ch - 0xD800) << 10) + (ch2 - 0xDC00);
}
else {
if (_isIgnore) {
}
else if (_replacement != null)
sb.append(_replacement);
else
return sb;
continue;
}
}
if (0x80 <= code && code <= 0x7FF) {
sb.appendByte(0xC0 | (code >> 6));
sb.appendByte(0x80 | (code & 0x3F));
}
else if (0x800 <= code && code <= 0xFFFF) {
sb.appendByte(0xE0 | (code >> 12));
sb.appendByte(0x80 | ((code >> 6) & 0x3F));
sb.appendByte(0x80 | (code & 0x3F));
}
else {
sb.appendByte(0xF0 | (code >> 18));
sb.appendByte(0x80 | ((code >> 12) & 0x3F));
sb.appendByte(0x80 | ((code >> 6) & 0x3F));
sb.appendByte(0x80 | (code & 0x3F));
}
}
return sb;
}