Example usage for java.lang String codePointAt

List of usage examples for java.lang String codePointAt

Introduction

In this page you can find the example usage for java.lang String codePointAt.

Prototype

public int codePointAt(int index) 

Source Link

Document

Returns the character (Unicode code point) at the specified index.

Usage

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// w w  w  . j  ava  2s . 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:com.ngdata.hbaseindexer.model.impl.IndexerModelImpl.java

/**
 * Check the validity of an indexer name.
 * <p>//from   ww  w  . j  a  v a 2  s .  c o m
 * An indexer name can be any string of printable unicode characters that has a length greater than 0. Printable
 * characters in this context are considered to be anything that is not an ISO control character as defined by
 * {@link Character#isISOControl(int)}.
 *
 * @param indexerName The name to validate
 */
public static void validateIndexerName(String indexerName) {
    Preconditions.checkNotNull(indexerName);
    if (indexerName.length() == 0) {
        throw new IllegalArgumentException("Indexer name is empty");
    }
    for (int charIdx = 0; charIdx < indexerName.length(); charIdx++) {
        if (Character.isISOControl(indexerName.codePointAt(charIdx))) {
            throw new IllegalArgumentException("Indexer names may only consist of printable characters");
        }
    }
}

From source file:com.forerunnergames.tools.common.Strings.java

/**
 * Checks whether the string s is comprised of only whitespace. <br/>
 * <br/>//w  w w.  j a v  a 2 s .  c om
 * Note: The input string may contain supplementary characters. For rules on which characters are considered
 * whitespace, see @see Character#isWhitespace(char)
 *
 * @param s
 *          The string to check, must not be null.
 *
 * @return True if the string s is comprised of only whitespace or is empty.
 */
public static boolean isWhitespace(final String s) {
    Arguments.checkIsNotNull(s, "s");

    for (int i = 0; i < s.length(); ++i) {
        if (!Character.isWhitespace(s.codePointAt(i)))
            return false;
    }

    return true;
}

From source file:info.novatec.testit.livingdoc.ognl.OgnlResolution.java

private int firstChar(String s) {
    return s.codePointAt(0);
}

From source file:Main.java

/**
 * Similar to String.contains() with two main differences:
 * <p>/*from  w  w  w . j a  v  a  2 s . co  m*/
 * 1) Only searches token prefixes.  A token is defined as any combination of letters or
 * numbers.
 * <p>
 * 2) Returns the starting index where the substring is found.
 *
 * @param value The string to search.
 * @param substring The substring to look for.
 * @return The starting index where the substring is found. {@literal -1} if substring is not
 *         found in value.
 */
@VisibleForTesting
static int contains(String value, String substring) {
    if (value.length() < substring.length()) {
        return -1;
    }

    // i18n support
    // Generate the code points for the substring once.
    // There will be a maximum of substring.length code points.  But may be fewer.
    // Since the array length is not an accurate size, we need to keep a separate variable.
    final int[] substringCodePoints = new int[substring.length()];
    int substringLength = 0; // may not equal substring.length()!!
    for (int i = 0; i < substring.length();) {
        final int codePoint = Character.codePointAt(substring, i);
        substringCodePoints[substringLength] = codePoint;
        substringLength++;
        i += Character.charCount(codePoint);
    }

    for (int i = 0; i < value.length(); i = findNextTokenStart(value, i)) {
        int numMatch = 0;
        for (int j = i; j < value.length() && numMatch < substringLength; ++numMatch) {
            int valueCp = Character.toLowerCase(value.codePointAt(j));
            int substringCp = substringCodePoints[numMatch];
            if (valueCp != substringCp) {
                break;
            }
            j += Character.charCount(valueCp);
        }
        if (numMatch == substringLength) {
            return i;
        }
    }
    return -1;
}

From source file:org.nuclos.common2.StringUtils.java

private static void makeSQLIdentifierFrom(StringBuilder result, String s, int maxLen) {
    if (maxLen < 1)
        throw new IllegalArgumentException();
    final int len = s.length();
    final int max = result.length() + maxLen;
    for (int i = 0; i < len; ++i) {
        final boolean accept;
        int c = s.codePointAt(i);
        if (Character.isSupplementaryCodePoint(c)) {
            ++i;//from w  w  w .  ja v a  2s . c  om
        }
        if (c >= 'A' && c <= 'Z') {
            accept = true;
        } else if (c >= 'a' && c <= 'z') {
            accept = true;
        } else if (c >= '0' && c <= '9') {
            accept = true;
        } else {
            switch (c) {
            case '_':
                accept = true;
                break;
            case ' ':
                c = '_';
                accept = true;
                break;
            // german umlaut support
            case '\u00e4':
                c = 'a';
                accept = true;
                break;
            case '\u00f6':
                c = 'o';
                accept = true;
                break;
            case '\u00fc':
                c = 'u';
                accept = true;
                break;
            case '\u00df':
                c = 's';
                accept = true;
                break;
            case '\u00c4':
                c = 'A';
                accept = true;
                break;
            case '\u00d6':
                c = 'O';
                accept = true;
                break;
            case '\u00dc':
                c = 'U';
                accept = true;
                break;
            default:
                accept = false;
            }
        }
        if (accept) {
            result.append((char) c);
        }
    }
    if (result.length() > max)
        result.setLength(max);
}

From source file:cz.lidinsky.editor.Menu.java

protected void setLabel(final JMenuItem menuItem, final String label) {
    if (label != null) {
        int mnemonicIndex = label.indexOf('_');
        if (mnemonicIndex >= 0) {
            String text = StringUtils.remove(label, '_');
            int key = text.codePointAt(mnemonicIndex);
            menuItem.setText(text);/*from  w  w  w .j  a va2  s.  c o  m*/
            menuItem.setMnemonic(key);
        } else {
            menuItem.setText(label);
        }
    }
}

From source file:info.novatec.testit.livingdoc.ognl.OgnlResolution.java

private boolean startAsAnIdentifier(String token) {
    return Character.isJavaIdentifierStart(token.codePointAt(0));
}

From source file:ac.elements.parser.ExtendedFunctions.java

/**
 * /* w  w  w  . ja  v a  2  s .c  om*/
 * This method ensures that the output String has only valid XML unicode
 * characters as specified by the
 * 
 * XML 1.0 standard. For reference, please see the
 * 
 * standard. This method will return an empty String if the input is null or
 * empty.
 * 
 * 
 * @author Donoiu Cristian, GPL
 * 
 * @param The
 *            String whose non-valid characters we want to remove.
 * 
 * @return The in String, stripped of non-valid characters.
 * @author 
 *         http://cse-mjmcl.cse.bris.ac.uk/blog/2007/02/14/1171465494443.html
 */
public static String stripNonValidXML(String s) {
    // Used to hold the output.
    StringBuilder out = new StringBuilder();

    // Used to reference the current character.
    int codePoint;

    // This is actualy one unicode character,
    // represented by two code units!!!.
    // String ss = "\ud801\udc00";
    // System.out.println(ss.codePointCount(0, ss.length()));// See: 1

    int i = 0;

    while (i < s.length()) {

        // System.out.println("i=" + i);

        // This is the unicode code of the character.
        codePoint = s.codePointAt(i);

        // Consider testing larger ranges first to improve speed.
        if ((codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD)
                || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
                || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
                || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF))) {

            out.append(Character.toChars(codePoint));

        }

        // Increment with the number of code units(java chars) needed to
        // represent a Unicode char.
        i += Character.charCount(codePoint);

    }

    return out.toString();

}

From source file:android.pim.vcard.VCardUtils.java

private static String toStringAsParamValue(String value, final int[] escapeIndicators) {
    if (TextUtils.isEmpty(value)) {
        value = "";
    }/*w ww .j av  a 2s  .  c  om*/
    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));
}