Example usage for java.lang String offsetByCodePoints

List of usage examples for java.lang String offsetByCodePoints

Introduction

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

Prototype

public int offsetByCodePoints(int index, int codePointOffset) 

Source Link

Document

Returns the index within this String that is offset from the given index by codePointOffset code points.

Usage

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

private static String toStringAsParamValue(String value, final int[] escapeIndicators) {
    if (TextUtils.isEmpty(value)) {
        value = "";
    }/*from w ww .  ja va 2s. 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:org.apache.hadoop.hive.common.type.HiveBaseChar.java

public static String enforceMaxLength(String val, int maxLength) {
    if (val == null) {
        return null;
    }/* www  . j a  v  a 2  s. com*/
    String value = val;

    if (maxLength > 0) {
        int valLength = val.codePointCount(0, val.length());
        if (valLength > maxLength) {
            // Truncate the excess chars to fit the character length.
            // Also make sure we take supplementary chars into account.
            value = val.substring(0, val.offsetByCodePoints(0, maxLength));
        }
    }
    return value;
}

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//  ww  w  . j a va  2s .  co 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:password.pwm.util.java.StringUtil.java

public static int[] toCodePointArray(final String str) {
    if (str != null) {
        final int len = str.length();
        final int[] acp = new int[str.codePointCount(0, len)];

        for (int i = 0, j = 0; i < len; i = str.offsetByCodePoints(i, 1)) {
            acp[j++] = str.codePointAt(i);
        }//from ww  w  .  j  a  v  a2  s.c o m

        return acp;
    }

    return new int[0];
}

From source file:password.pwm.util.StringUtil.java

public static int[] toCodePointArray(String str) {
    if (str != null) {
        int len = str.length();
        int[] acp = new int[str.codePointCount(0, len)];

        for (int i = 0, j = 0; i < len; i = str.offsetByCodePoints(i, 1)) {
            acp[j++] = str.codePointAt(i);
        }/*  w  w w  . j  a  va2  s  .com*/

        return acp;
    }

    return new int[0];
}