use of java.text.StringCharacterIterator in project lucene-solr by apache.
the class SplittingBreakIterator method getText.
@Override
public CharacterIterator getText() {
StringCharacterIterator charIter = new StringCharacterIterator(text);
// API doesn't say what the state should be but it should probably be at the current index.
charIter.setIndex(current());
return charIter;
}
use of java.text.StringCharacterIterator in project compiler by boalang.
the class JsonFormat method escapeText.
/**
* Implements JSON string escaping as specified <a href="http://www.ietf.org/rfc/rfc4627.txt">here</a>.
* <ul>
* <li>The following characters are escaped by prefixing them with a '\' : \b,\f,\n,\r,\t,\,"</li>
* <li>Other control characters in the range 0x0000-0x001F are escaped using the \\uXXXX notation</li>
* <li>UTF-16 surrogate pairs are encoded using the \\uXXXX\\uXXXX notation</li>
* <li>any other character is printed as-is</li>
* </ul>
*/
static String escapeText(String input) {
StringBuilder builder = new StringBuilder(input.length());
CharacterIterator iter = new StringCharacterIterator(input);
for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
switch(c) {
case '\b':
builder.append("\\b");
break;
case '\f':
builder.append("\\f");
break;
case '\n':
builder.append("\\n");
break;
case '\r':
builder.append("\\r");
break;
case '\t':
builder.append("\\t");
break;
case '\\':
builder.append("\\\\");
break;
case '"':
builder.append("\\\"");
break;
default:
// Check for other control characters
if (c >= 0x0000 && c <= 0x001F) {
appendEscapedUnicode(builder, c);
} else if (Character.isHighSurrogate(c)) {
// Encode the surrogate pair using 2 six-character sequence (\\uXXXX\\uXXXX)
appendEscapedUnicode(builder, c);
c = iter.next();
if (c == CharacterIterator.DONE)
throw new IllegalArgumentException("invalid unicode string: unexpected high surrogate pair value without corresponding low value.");
appendEscapedUnicode(builder, c);
} else {
// Anything else can be printed as-is
builder.append(c);
}
break;
}
}
return builder.toString();
}
use of java.text.StringCharacterIterator in project sling by apache.
the class CustomLogFormat method parse.
// ---------- Parsing the format pattern -----------------------------------
private Parameter[] parse(String pattern) {
List<Parameter> parameterList = new ArrayList<Parameter>();
StringBuilder buf = new StringBuilder();
CharacterIterator sr = new StringCharacterIterator(pattern);
for (int c = sr.first(); c != CharacterIterator.DONE; c = sr.next()) {
if (c == '%') {
int c1 = sr.next();
if (c1 != '%') {
if (buf.length() > 0) {
Parameter text = new PlainTextParameter(buf.toString());
parameterList.add(text);
buf.setLength(0);
}
Parameter param = this.parseFormatString(sr, c1);
if (param != null) {
parameterList.add(param);
}
continue;
}
}
buf.append((char) c);
}
// append any remaining plain text
if (buf.length() > 0) {
Parameter text = new PlainTextParameter(buf.toString());
parameterList.add(text);
buf.setLength(0);
}
return parameterList.toArray(new Parameter[parameterList.size()]);
}
use of java.text.StringCharacterIterator in project leopard by tanhaichao.
the class XmlUtils method escape.
public static String escape(String input) {
if (input == null) {
return null;
}
StringBuilder result = new StringBuilder();
StringCharacterIterator iterator = new StringCharacterIterator(input);
char character = iterator.current();
while (character != CharacterIterator.DONE) {
if (character == '<') {
result.append("<");
} else if (character == '>') {
result.append(">");
} else if (character == '\"') {
result.append(""");
} else if (character == '\'') {
result.append("'");
} else if (character == '&') {
result.append("&");
} else {
result.append(character);
}
character = iterator.next();
}
return result.toString();
}
use of java.text.StringCharacterIterator in project Lucee by lucee.
the class UUCoder method decode.
/**
* decodes back a String to a byte array
* @param b
* @return decoded byte array
*/
public static byte[] decode(String str) throws CoderException {
byte[] out = new byte[str.length()];
int len = 0;
int offset = 0;
// int current = 0;
byte b = 0;
boolean stop = false;
StringCharacterIterator it = new StringCharacterIterator(str);
do {
b = _dec(it.current());
it.next();
if (b > 45)
throw new CoderException("can't decode string [" + str + "]");
if (b < 45)
stop = true;
len += b;
for (; b > 0; b -= 3) {
decodeChars(it, out, offset);
offset += 3;
}
it.next();
} while (!stop);
byte[] rtn = new byte[len];
for (int i = 0; i < len; i++) rtn[i] = out[i];
return rtn;
}
Aggregations