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:Main.java

public static void main(String[] args) {

    String str = "java2s.com";
    System.out.println("string = " + str);

    // returns the index within this String
    int retval = str.offsetByCodePoints(2, 4);
    // prints the index
    System.out.println("index = " + retval);
}

From source file:Main.java

public static boolean containsOnlyWhiteSpaces(final Collection<String> values) {
    if (values == null) {
        return true;
    }/*from   www . j  ava2  s. com*/
    for (final String str : values) {
        if (TextUtils.isEmpty(str)) {
            continue;
        }
        final int length = str.length();
        for (int i = 0; i < length; i = str.offsetByCodePoints(i, 1)) {
            if (!Character.isWhitespace(str.codePointAt(i))) {
                return false;
            }
        }
    }
    return true;
}

From source file:Main.java

public static boolean containsOnlyNonCrLfPrintableAscii(final Collection<String> values) {
    if (values == null) {
        return true;
    }/*from  www  .  jav a  2 s.  c om*/
    final int asciiFirst = 0x20;
    final int asciiLast = 0x7E; // included
    for (final String value : values) {
        if (TextUtils.isEmpty(value)) {
            continue;
        }
        final int length = value.length();
        for (int i = 0; i < length; i = value.offsetByCodePoints(i, 1)) {
            final int c = value.codePointAt(i);
            if (!(asciiFirst <= c && c <= asciiLast)) {
                return false;
            }
        }
    }
    return true;
}

From source file:Main.java

public static boolean containsOnlyAlphaDigitHyphen(final Collection<String> values) {
    if (values == null) {
        return true;
    }/*from  www. ja va 2 s .co  m*/
    final int upperAlphabetFirst = 0x41; // A
    final int upperAlphabetAfterLast = 0x5b; // [
    final int lowerAlphabetFirst = 0x61; // a
    final int lowerAlphabetAfterLast = 0x7b; // {
    final int digitFirst = 0x30; // 0
    final int digitAfterLast = 0x3A; // :
    final int hyphen = '-';
    for (final String str : values) {
        if (TextUtils.isEmpty(str)) {
            continue;
        }
        final int length = str.length();
        for (int i = 0; i < length; i = str.offsetByCodePoints(i, 1)) {
            int codepoint = str.codePointAt(i);
            if (!((lowerAlphabetFirst <= codepoint && codepoint < lowerAlphabetAfterLast)
                    || (upperAlphabetFirst <= codepoint && codepoint < upperAlphabetAfterLast)
                    || (digitFirst <= codepoint && codepoint < digitAfterLast) || (codepoint == hyphen))) {
                return false;
            }
        }
    }
    return true;
}

From source file:Main.java

public static boolean containsOnlyPrintableAscii(String str) {
    if (TextUtils.isEmpty(str)) {
        return true;
    }//  ww  w.  j  a  va  2 s  .  c  o  m

    final int length = str.length();
    final int asciiFirst = 0x20;
    final int asciiLast = 0x126;
    for (int i = 0; i < length; i = str.offsetByCodePoints(i, 1)) {
        int c = str.codePointAt(i);
        if (c < asciiFirst || asciiLast < c) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * This is useful when checking the string should be encoded into quoted-printable
 * or not, which is required by vCard 2.1.
 * See the definition of "7bit" in vCard 2.1 spec for more information.
 *//*from ww  w.  j  ava2s.  com*/
public static boolean containsOnlyNonCrLfPrintableAscii(String str) {
    if (TextUtils.isEmpty(str)) {
        return true;
    }

    final int length = str.length();
    final int asciiFirst = 0x20;
    final int asciiLast = 0x126;
    for (int i = 0; i < length; i = str.offsetByCodePoints(i, 1)) {
        int c = str.codePointAt(i);
        if (c < asciiFirst || asciiLast < c || c == '\n' || c == '\r') {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * <p>// ww w  .  ja v a 2 s  .co m
 * Returns true when the given String is categorized as "word" specified in vCard spec 2.1.
 * </p>
 * <p>
 * vCard 2.1 specifies:<br />
 * word = &lt;any printable 7bit us-ascii except []=:., &gt;
 * </p>
 */
public static boolean isV21Word(final String value) {
    if (TextUtils.isEmpty(value)) {
        return true;
    }
    final int asciiFirst = 0x20;
    final int asciiLast = 0x7E; // included
    final int length = value.length();
    for (int i = 0; i < length; i = value.offsetByCodePoints(i, 1)) {
        final int c = value.codePointAt(i);
        if (!(asciiFirst <= c && c <= asciiLast) || sUnAcceptableAsciiInV21WordSet.contains((char) c)) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

private static String encodeB(String prefix, String text, int usedCharacters, Charset charset, byte[] bytes) {
    int encodedLength = bEncodedLength(bytes);

    int totalLength = prefix.length() + encodedLength + ENC_WORD_SUFFIX.length();
    if (totalLength <= ENCODED_WORD_MAX_LENGTH - usedCharacters) {
        return prefix + encodeB(bytes) + ENC_WORD_SUFFIX;
    } else {/* www.j  a  va  2 s  .  c  om*/
        int splitOffset = text.offsetByCodePoints(text.length() / 2, -1);

        String part1 = text.substring(0, splitOffset);
        byte[] bytes1 = encode(part1, charset);
        String word1 = encodeB(prefix, part1, usedCharacters, charset, bytes1);

        String part2 = text.substring(splitOffset);
        byte[] bytes2 = encode(part2, charset);
        String word2 = encodeB(prefix, part2, 0, charset, bytes2);

        return word1 + " " + word2;
    }
}

From source file:Main.java

/**
 * This is useful since vCard 3.0 often requires the ("X-") properties and groups
 * should contain only alphabets, digits, and hyphen.
 * //www. j a  va 2  s.c om
 * Note: It is already known some devices (wrongly) outputs properties with characters
 *       which should not be in the field. One example is "X-GOOGLE TALK". We accept
 *       such kind of input but must never output it unless the target is very specific
 *       to the device which is able to parse the malformed input. 
 */
public static boolean containsOnlyAlphaDigitHyphen(String str) {
    if (TextUtils.isEmpty(str)) {
        return true;
    }

    final int lowerAlphabetFirst = 0x41; // included ('A')
    final int lowerAlphabetLast = 0x5b; // not included ('[')
    final int upperAlphabetFirst = 0x61; // included ('a')
    final int upperAlphabetLast = 0x7b; // included ('{')
    final int digitFirst = 0x30; // included ('0')
    final int digitLast = 0x39; // included ('9')
    final int hyphen = '-';
    final int length = str.length();
    for (int i = 0; i < length; i = str.offsetByCodePoints(i, 1)) {
        int codepoint = str.codePointAt(i);
        if (!((lowerAlphabetFirst <= codepoint && codepoint < lowerAlphabetLast)
                || (upperAlphabetFirst <= codepoint && codepoint < upperAlphabetLast)
                || (digitFirst <= codepoint && codepoint < digitLast) || (codepoint == hyphen))) {
            return false;
        }
    }
    return true;
}

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

public static String toHalfWidthString(final String orgString) {
    if (TextUtils.isEmpty(orgString)) {
        return null;
    }/*  w w w  . j  a  va  2s . c  om*/
    final StringBuilder builder = new StringBuilder();
    final int length = orgString.length();
    for (int i = 0; i < length; i = orgString.offsetByCodePoints(i, 1)) {
        // All Japanese character is able to be expressed by char.
        // Do not need to use String#codepPointAt().
        final char ch = orgString.charAt(i);
        final String halfWidthText = JapaneseUtils.tryGetHalfWidthText(ch);
        if (halfWidthText != null) {
            builder.append(halfWidthText);
        } else {
            builder.append(ch);
        }
    }
    return builder.toString();
}