List of usage examples for java.lang StringBuilder appendCodePoint
@Override public StringBuilder appendCodePoint(int codePoint)
From source file:org.omegat.util.FileUtil.java
static Pattern compileFileMask(String mask) { StringBuilder m = new StringBuilder(); // "Relative" masks can match at any directory level if (!mask.startsWith("/")) { mask = "**/" + mask; }/*from w ww.j a va 2s. c om*/ // Masks ending with a slash match everything in subtree if (mask.endsWith("/")) { mask += "**"; } for (int cp, i = 0; i < mask.length(); i += Character.charCount(cp)) { cp = mask.codePointAt(i); if (cp >= 'A' && cp <= 'Z') { m.appendCodePoint(cp); } else if (cp >= 'a' && cp <= 'z') { m.appendCodePoint(cp); } else if (cp >= '0' && cp <= '9') { m.appendCodePoint(cp); } else if (cp == '/') { if (mask.regionMatches(i, "/**/", 0, 4)) { // The sequence /**/ matches *zero* or more levels m.append("(?:/|/.*/)"); i += 3; } else if (mask.regionMatches(i, "/**", 0, 3)) { // The sequence /** matches *zero* or more levels m.append("(?:|/.*)"); i += 2; } else { m.appendCodePoint(cp); } } else if (cp == '?') { // ? matches anything but a directory separator m.append("[^/]"); } else if (cp == '*') { if (mask.regionMatches(i, "**/", 0, 3)) { // The sequence **/ matches *zero* or more levels m.append("(?:|.*/)"); i += 2; } else if (mask.regionMatches(i, "**", 0, 2)) { // ** m.append(".*"); i++; } else { // * m.append("[^/]*"); } } else { m.append('\\').appendCodePoint(cp); } } return Pattern.compile(m.toString()); }
From source file:jp.furplag.util.commons.StringUtils.java
/** * returns a new string that is a substring of this string. The substring begins at the specified <code>beginIndex</code> and extends to the string at <code>byteLen</code> bytes length. * * <pre>// ww w . ja v a 2 s .c o m * StringUtils.substringUBL(null, *, *) = "" * StringUtils.substringUBL("", * , *) = "" * StringUtils.substringUBL("abc", 0, 2) = "ab" * StringUtils.substringUBL("abc", 2, 0) = "" * StringUtils.substringUBL("abc", 2, 4) = "c" * StringUtils.substringUBL("abc", 4, 6) = "" * StringUtils.substringUBL("abc", 2, 2) = "c" * StringUtils.substringUBL("abc", -2, -1) = "b" * StringUtils.substringUBL("abc", -4, 2) = "ab" * </pre> * * @param str the string to get the substring from, may be null. * @param beginIndex the position to start from, negative means count back from the end of the String by this many characters. * @param byteLen the byte length to end at (exclusive), return empty if negative. * @return substring from start position to end position, return empty if null. * @exception IllegalArgumentException if a character that is more than <code>byteLen</code> bytes in the string is present */ public static String substringUBL(final String str, final int beginIndex, final int byteLen) { if (byteLen < 1) return EMPTY; String temporary = defaultString(str); int[] codePoints = getCodePoints(temporary); int begin = (beginIndex < 0 ? codePoints.length : 0) + beginIndex; if (begin < 0) begin = 0; if (begin > codePoints.length) return EMPTY; StringBuilder sb = new StringBuilder(); int subLen = 0; for (int codePoint : Arrays.copyOfRange(codePoints, begin, codePoints.length)) { StringBuilder internalSb = new StringBuilder().appendCodePoint(codePoint); int internalSbLen = byteLength(internalSb.toString()); if (internalSbLen > byteLen) throw new IllegalArgumentException("byteLen too small even for \"" + internalSb + "\"."); if (subLen + internalSbLen > byteLen) break; sb.appendCodePoint(codePoint); subLen += internalSbLen; } return sb.toString(); }
From source file:org.mariotaku.twidere.util.CodePointArray.java
@NonNull public String substring(int start, int end) { final StringBuilder sb = new StringBuilder(); for (int i = start; i < end; i++) { sb.appendCodePoint(codePoints[i]); }/*from ww w. ja v a 2 s. c o m*/ return sb.toString(); }
From source file:org.pentaho.reporting.libraries.xmlns.writer.XmlWriterSupport.java
private static void writeTextNormalized(final Writer writer, final String s, final CharsetEncoder encoder, final boolean transformNewLine) throws IOException { if (s == null) { return;// w w w . ja v a 2s . com } final StringBuilder strB = new StringBuilder(s.length()); for (int offset = 0; offset < s.length();) { final int cp = s.codePointAt(offset); switch (cp) { case 9: // \t strB.appendCodePoint(cp); break; case 10: // \n if (transformNewLine) { strB.append(" "); break; } strB.appendCodePoint(cp); break; case 13: // \r if (transformNewLine) { strB.append(" "); break; } strB.appendCodePoint(cp); break; case 60: // < strB.append("<"); break; case 62: // > strB.append(">"); break; case 34: // " strB.append("""); break; case 38: // & strB.append("&"); break; case 39: // ' strB.append("'"); break; default: if (cp >= 0x20) { final String cpStr = new String(new int[] { cp }, 0, 1); if ((encoder != null) && !encoder.canEncode(cpStr)) { strB.append("&#x" + Integer.toHexString(cp)); } else { strB.appendCodePoint(cp); } } } offset += Character.charCount(cp); } writer.write(strB.toString()); }
From source file:android.pim.vcard.VCardUtils.java
private static String toStringAsParamValue(String value, final int[] escapeIndicators) { if (TextUtils.isEmpty(value)) { value = ""; }//from w w w . j a va2 s . c o m final int asciiFirst = 0x20; final int asciiLast = 0x7E; // included final StringBuilder builder = new StringBuilder(); final int length = value.length(); boolean needQuote = false; for (int i = 0; i < length; i = value.offsetByCodePoints(i, 1)) { final int codePoint = value.codePointAt(i); if (codePoint < asciiFirst || codePoint == '"') { // CTL characters and DQUOTE are never accepted. Remove them. continue; } builder.appendCodePoint(codePoint); for (int indicator : escapeIndicators) { if (codePoint == indicator) { needQuote = true; break; } } } final String result = builder.toString(); return ((result.isEmpty() || VCardUtils.containsOnlyWhiteSpaces(result)) ? "" : (needQuote ? ('"' + result + '"') : result)); }
From source file:ductive.console.jline.JLineInteractiveTerminal.java
public JLineInteractiveTerminal(JLineConsoleReader r, ShellSettings settings) { Validate.notNull(settings);//www. j av a 2 s . c o m this.jline = r; updateSettings(settings); jline.addCompleter(new Completer() { @Override public int complete(String buffer, int cursor, List<CharSequence> candidates) { return currentSettings.completer() != null ? currentSettings.completer().complete(buffer, cursor, candidates) : cursor; } }); jline.setPrompt(new Provider<Ansi>() { @Override public Ansi get() { return currentSettings.prompt().get(); } }); // hack to support Ctrl-C on the prompt in a bash-like manner StringBuilder sb = new StringBuilder(); sb.appendCodePoint(3); // Ctrl-C jline.getKeys().bind(sb, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { jline.println("^C"); flush(); String partialLine = jline.getCursorBuffer().buffer.toString(); jline.getCursorBuffer().clear(); throw new UserInterruptException(partialLine); } catch (IOException x) { throw Throwables.propagate(x); } } }); }
From source file:com.predic8.membrane.core.interceptor.IndexInterceptor.java
static String fullfillRegexp(String regex) { StringBuilder sb = new StringBuilder(); int p = 0, groupLevel = 0; WHILE: while (p < regex.length()) { int c = regex.codePointAt(p++); switch (c) { case '\\': if (p == regex.length()) return null; // illegal c = regex.codePointAt(p++);/*from w ww. ja v a2 s. c o m*/ if (Character.isDigit(c)) return null; // backreferences are not supported if (c == 'Q') { while (true) { if (p == regex.length()) return null; // 'end of regex' within quote c = regex.codePointAt(p++); if (c == '\\') { if (p == regex.length()) return null; // 'end of regex' within quote c = regex.codePointAt(p++); if (c == 'E') break; sb.append('\\'); } sb.appendCodePoint(c); } break; } if (c == 'E') { return null; // 'end of quote' without begin } sb.appendCodePoint(c); break; case '[': case '?': case '*': case '+': case '{': return null; // meaningful characters we do not unterstand case '(': groupLevel++; break; case ')': if (groupLevel == 0) return null; // unbalanced ')' else groupLevel--; break; case '|': if (groupLevel == 0) { break WHILE; } W2: while (true) { if (++p == regex.length()) return null; // unbalanced ')' switch (regex.charAt(p)) { case ')': break W2; case '[': case '?': case '*': case '+': case '{': return null; // meaningful characters we do not unterstand case '\\': return null; // TODO: \) \Q..\E } } groupLevel--; p++; break; case '^': if (p == 1) break; return null; case '$': if (p == regex.length() || regex.codePointAt(p) == '|') break; return null; case '.': int q; if (p != regex.length() && isQuantifier(q = regex.codePointAt(p))) { if (++p != regex.length() && isModifier(regex.codePointAt(p))) p++; if (q == '+') sb.append('a'); } else { sb.append('a'); } break; default: sb.appendCodePoint(c); break; } } if (groupLevel > 0) return null; return sb.toString(); }
From source file:jav.correctionBackend.parser.WagnerFischer.java
@Override public String toString() { StringBuilder builder = new StringBuilder(); for (int i = 0, j = 0; i < trace.size(); ++i) { if (trace.get(i).equals(EditOperation.INSERTION)) { builder.append('_'); } else {/*from w w w . j a va2 s . c o m*/ builder.appendCodePoint(ocr.get(j).getChar()); if (Tokenization.isNonSpacingMark(ocr.get(j).getChar())) { builder.append('_'); } ++j; } } builder.append('\n'); builder.append(trace.toString()); builder.append('\n'); for (int i = 0, j = 0; i < trace.size(); ++i) { if (trace.get(i).equals(EditOperation.DELETION)) { builder.append('_'); } else { builder.appendCodePoint(gt.get(j).getChar()); if (Tokenization.isNonSpacingMark(gt.get(j).getChar())) { builder.append('_'); } ++j; } } return builder.toString(); }
From source file:org.omegat.util.StaticUtils.java
/** * Parse a command line string into arguments, interpreting * double and single quotes as Bash does. * @param cmd Command string/*from w ww. j ava2 s. c o m*/ * @return Array of arguments */ public static String[] parseCLICommand(String cmd) { cmd = cmd.trim(); if (cmd.isEmpty()) { return new String[] { "" }; } StringBuilder arg = new StringBuilder(); List<String> result = new ArrayList<String>(); final char noQuote = '\0'; char currentQuote = noQuote; for (int cp, i = 0; i < cmd.length(); i += Character.charCount(cp)) { cp = cmd.codePointAt(i); if (cp == currentQuote) { currentQuote = noQuote; } else if (cp == '"' && currentQuote == noQuote) { currentQuote = '"'; } else if (cp == '\'' && currentQuote == noQuote) { currentQuote = '\''; } else if (cp == '\\' && i + 1 < cmd.length()) { int ncp = cmd.codePointAt(cmd.offsetByCodePoints(i, 1)); if ((currentQuote == noQuote && Character.isWhitespace(ncp)) || (currentQuote == '"' && ncp == '"')) { arg.appendCodePoint(ncp); i += Character.charCount(ncp); } else { arg.appendCodePoint(cp); } } else { if (Character.isWhitespace(cp) && currentQuote == noQuote) { if (arg.length() > 0) { result.add(arg.toString()); arg = new StringBuilder(); } else { // Discard } } else { arg.appendCodePoint(cp); } } } // Catch last arg if (arg.length() > 0) { result.add(arg.toString()); } return result.toArray(new String[result.size()]); }
From source file:jav.correctionBackend.parser.WagnerFischer.java
public String matrixToString() { StringBuilder builder = new StringBuilder(); builder.append(" "); for (int i = 0; i < matrix[0].length; ++i) { if (i > 0) { if (Tokenization.isNonSpacingMark(gt.get(i - 1).getChar())) { builder.append('_'); }//from w w w .ja v a2s . co m builder.appendCodePoint(gt.get(i - 1).getChar()).append(" "); } else { builder.append(" "); } } builder.append('\n'); for (int i = 0; i < matrix.length; ++i) { if (i > 0) { if (Tokenization.isNonSpacingMark(ocr.get(i - 1).getChar())) { builder.append('_'); } builder.appendCodePoint(ocr.get(i - 1).getChar()).append(" "); } else { builder.append(" "); } for (int j = 0; j < matrix[i].length; ++j) { if (matrix[i][j] < 10) { builder.append(matrix[i][j]).append(" "); } else { builder.append(matrix[i][j]).append(" "); } } builder.append('\n'); } return builder.toString(); }