Example usage for java.lang Character codePointAt

List of usage examples for java.lang Character codePointAt

Introduction

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

Prototype

public static int codePointAt(char[] a, int index) 

Source Link

Document

Returns the code point at the given index of the char array.

Usage

From source file:Main.java

/**
 * Escapes a string builder so that it is valid XML.
 * //from  w  ww.j a  v a  2 s . c  om
 * @param sb
 *        The string builder to escape.
 */
public static void escapeXML(StringBuilder sb) {
    // double quote -- quot
    // ampersand -- amp
    // less than -- lt
    // greater than -- gt
    // apostrophe -- apos
    for (int i = 0; i < sb.length();) {
        int codePoint = Character.codePointAt(sb, i);
        int length = Character.charCount(codePoint);
        if (codePoint == '<') {
            sb.replace(i, i + length, LT);
            i += LT.length();
        } else if (codePoint == '>') {
            sb.replace(i, i + length, GT);
            i += GT.length();
        } else if (codePoint == '\"') {
            sb.replace(i, i + length, QUOT);
            i += QUOT.length();
        } else if (codePoint == '&') {
            sb.replace(i, i + length, AMP);
            i += AMP.length();
        } else if (codePoint == '\'') {
            sb.replace(i, i + length, APOS);
            i += APOS.length();
        } else {
            i += length;
        }
    }
}

From source file:android.support.text.emoji.EmojiProcessor.java

EmojiMetadata getEmojiMetadata(@NonNull final CharSequence charSequence) {
    final ProcessorSm sm = new ProcessorSm(mMetadataRepo.getRootNode());
    final int end = charSequence.length();
    int currentOffset = 0;

    while (currentOffset < end) {
        final int codePoint = Character.codePointAt(charSequence, currentOffset);
        final int action = sm.check(codePoint);
        if (action != ACTION_ADVANCE_END) {
            return null;
        }//from  www.j  a  va 2s .  c  o  m
        currentOffset += Character.charCount(codePoint);
    }

    if (sm.isInFlushableState()) {
        return sm.getCurrentMetadata();
    }

    return null;
}

From source file:app.data.parse.WebPageUtil.java

/**
 * ???//from   ww  w  .  j  a va2  s  .  c o m
 */
public static String ellipsis(String text, int limit, @NotNull String append) {
    if (text.length() <= limit) {
        return text;
    }

    final int space = limit - append.length();
    int i = 0, next = 0;
    while (i + next <= space) {
        i += next;
        int unicode = Character.codePointAt(text, i);
        next = Character.charCount(unicode);
    }
    return text.substring(0, i) + append;
}

From source file:org.apache.bval.jsr.util.PathNavigation.java

private static String parseQuotedString(CharSequence path, PathPosition pos) throws Exception {
    int len = path.length();
    int start = pos.getIndex();
    if (start < len) {
        char quote = path.charAt(start);
        pos.next();//from ww  w.  ja  va2s .co m
        StringWriter w = new StringWriter();
        while (pos.getIndex() < len) {
            int here = pos.getIndex();
            // look for matching quote
            if (path.charAt(here) == quote) {
                pos.next();
                return w.toString();
            }
            int codePoints = StringEscapeUtils.UNESCAPE_JAVA.translate(path, here, w);
            if (codePoints == 0) {
                w.write(Character.toChars(Character.codePointAt(path, here)));
                pos.next();
            } else {
                for (int i = 0; i < codePoints; i++) {
                    pos.plus(Character.charCount(Character.codePointAt(path, pos.getIndex())));
                }
            }
        }
        // if reached, reset due to no ending quote found
        pos.setIndex(start);
    }
    return null;
}

From source file:nl.strohalm.cyclos.utils.StringHelper.java

/**
 * Replaces supplementary characters with a ? character
 * @param text//from   www . j  a  va2 s  .  c  o  m
 * @return
 */
public static String replaceSupplementaryCharacters(final String text) {
    if (text == null) {
        return null;
    }
    final int len = text.length();
    boolean isSupplementary = false;
    final StringBuilder result = new StringBuilder();
    for (int i = 0; i < len; i++) {
        final int cp = Character.codePointAt(text, i);
        isSupplementary = Character.isSupplementaryCodePoint(cp);
        if (isSupplementary) {
            result.append("?");
            i++;
        } else {
            result.append(text.charAt(i));
        }
    }
    return result.toString();
}

From source file:de.questmaster.fatremote.fragments.RemoteFragment.java

private TextWatcher determineKeyboardTextChangedListener() {
    TextWatcher tw = null;//from   w w w.ja v  a 2  s.  c  o  m

    //check for bad keyboards
    boolean hwKeyboardShown = getActivity().getResources()
            .getConfiguration().hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO;
    String swKeyboardName = Settings.Secure.getString(getActivity().getContentResolver(),
            Settings.Secure.DEFAULT_INPUT_METHOD);

    if (!hwKeyboardShown && swKeyboardName.contains("nuance.xt9")) { // ASUS Transformer Prime SoftKeyboard
        tw = new TextWatcher() {
            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                int diff = count - before;

                if (s.length() > 0) {
                    // Send Character
                    keyCode = 0xf9;
                    if (diff > 0) { // +1
                        keyModifier = (short) Character.codePointAt(s, before);
                    } else if (diff < 0) { // -1
                        keyModifier = (short) 0x7f; // backspace
                    } else {
                        keyModifier = (short) 0x0a; // enter
                    }

                    // send keyCode
                    invokeSend();
                }
            }
        };
    } else { // Android Default Behaviour                
        tw = new TextWatcher() {
            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0 || before != 0) {
                    // Send Character
                    keyCode = 0xf9;
                    if (before == 0) {
                        keyModifier = (short) Character.codePointAt(s, start);
                    } else if (start == 0) {
                        keyModifier = 0x0a; // enter
                    } else {
                        keyModifier = (short) 0x7f; // backspace
                    }

                    // send keyCode
                    invokeSend();
                }
            }
        };
    }

    return tw;
}

From source file:gate.creole.splitter.RegexSentenceSplitter.java

@Override
public void execute() throws ExecutionException {
    interrupted = false;/*from   w ww.j  a v  a2s.  c  o  m*/
    int lastProgress = 0;
    fireProgressChanged(lastProgress);
    //get pointers to the annotation sets
    AnnotationSet outputAS = (outputASName == null || outputASName.trim().length() == 0)
            ? document.getAnnotations()
            : document.getAnnotations(outputASName);

    String docText = document.getContent().toString();

    /* If the document's content is empty or contains only whitespace,
     * we drop out right here, since there's nothing to sentence-split.     */
    if (docText.trim().length() < 1) {
        return;
    }

    Matcher internalSplitMatcher = internalSplitsPattern.matcher(docText);
    Matcher externalSplitMatcher = externalSplitsPattern.matcher(docText);

    Matcher nonSplitMatcher = nonSplitsPattern.matcher(docText);
    //store all non split locations in a list of pairs
    List<int[]> nonSplits = new LinkedList<int[]>();
    while (nonSplitMatcher.find()) {
        nonSplits.add(new int[] { nonSplitMatcher.start(), nonSplitMatcher.end() });
    }
    //this lists holds the next matches at each step
    List<MatchResult> nextSplitMatches = new ArrayList<MatchResult>();
    //initialise matching process
    MatchResult internalMatchResult = null;
    if (internalSplitMatcher.find()) {
        internalMatchResult = internalSplitMatcher.toMatchResult();
        nextSplitMatches.add(internalMatchResult);
    }
    MatchResult externalMatchResult = null;
    if (externalSplitMatcher.find()) {
        externalMatchResult = externalSplitMatcher.toMatchResult();
        nextSplitMatches.add(externalMatchResult);
    }
    MatchResultComparator comparator = new MatchResultComparator();
    int lastSentenceEnd = 0;

    while (!nextSplitMatches.isEmpty()) {
        //see which one matches first
        Collections.sort(nextSplitMatches, comparator);
        MatchResult nextMatch = nextSplitMatches.remove(0);
        if (nextMatch == internalMatchResult) {
            //we have a new internal split; see if it's vetoed or not
            if (!veto(nextMatch, nonSplits)) {
                //split is not vetoed
                try {
                    //add the split annotation
                    FeatureMap features = Factory.newFeatureMap();
                    features.put("kind", "internal");
                    outputAS.add(new Long(nextMatch.start()), new Long(nextMatch.end()), "Split", features);
                    //generate the sentence annotation
                    int endOffset = nextMatch.end();
                    //find the first non whitespace character starting from where the
                    //last sentence ended
                    while (lastSentenceEnd < endOffset
                            && Character.isWhitespace(Character.codePointAt(docText, lastSentenceEnd))) {
                        lastSentenceEnd++;
                    }
                    //if there is any useful text between the two offsets, generate
                    //a new sentence
                    if (lastSentenceEnd < nextMatch.start()) {
                        outputAS.add(new Long(lastSentenceEnd), new Long(endOffset),
                                ANNIEConstants.SENTENCE_ANNOTATION_TYPE, Factory.newFeatureMap());
                    }
                    //store the new sentence end
                    lastSentenceEnd = endOffset;
                } catch (InvalidOffsetException e) {
                    // this should never happen
                    throw new ExecutionException(e);
                }
            }
            //prepare for next step
            if (internalSplitMatcher.find()) {
                internalMatchResult = internalSplitMatcher.toMatchResult();
                nextSplitMatches.add(internalMatchResult);
            } else {
                internalMatchResult = null;
            }
        } else if (nextMatch == externalMatchResult) {
            //we have a new external split; see if it's vetoed or not
            if (!veto(nextMatch, nonSplits)) {
                //split is not vetoed
                try {
                    //generate the split
                    FeatureMap features = Factory.newFeatureMap();
                    features.put("kind", "external");
                    outputAS.add(new Long(nextMatch.start()), new Long(nextMatch.end()), "Split", features);
                    //generate the sentence annotation
                    //find the last non whitespace character, going backward from
                    //where the external skip starts
                    int endOffset = nextMatch.start();
                    while (endOffset > lastSentenceEnd
                            && Character.isSpaceChar(Character.codePointAt(docText, endOffset - 1))) {
                        endOffset--;
                    }
                    //find the first non whitespace character starting from where the
                    //last sentence ended
                    while (lastSentenceEnd < endOffset
                            && Character.isSpaceChar(Character.codePointAt(docText, lastSentenceEnd))) {
                        lastSentenceEnd++;
                    }
                    //if there is any useful text between the two offsets, generate
                    //a new sentence
                    if (lastSentenceEnd < endOffset) {
                        outputAS.add(new Long(lastSentenceEnd), new Long(endOffset),
                                ANNIEConstants.SENTENCE_ANNOTATION_TYPE, Factory.newFeatureMap());
                    }
                    //store the new sentence end
                    lastSentenceEnd = nextMatch.end();
                } catch (InvalidOffsetException e) {
                    // this should never happen
                    throw new ExecutionException(e);
                }
            }
            //prepare for next step
            if (externalSplitMatcher.find()) {
                externalMatchResult = externalSplitMatcher.toMatchResult();
                nextSplitMatches.add(externalMatchResult);
            } else {
                externalMatchResult = null;
            }
        } else {
            //malfunction
            throw new ExecutionException("Invalid state - cannot identify match!");
        }
        //report progress
        int newProgress = 100 * lastSentenceEnd / docText.length();
        if (newProgress - lastProgress > 20) {
            lastProgress = newProgress;
            fireProgressChanged(lastProgress);
        }
    } //while(!nextMatches.isEmpty()){
    fireProcessFinished();
}

From source file:android.support.text.emoji.EmojiProcessor.java

/**
 * Checks a given CharSequence for emojis, and adds EmojiSpans if any emojis are found.
 * <p>// w ww  .  j a  v  a 2  s. c om
 * <ul>
 * <li>If no emojis are found, {@code charSequence} given as the input is returned without
 * any changes. i.e. charSequence is a String, and no emojis are found, the same String is
 * returned.</li>
 * <li>If the given input is not a Spannable (such as String), and at least one emoji is found
 * a new {@link android.text.Spannable} instance is returned. </li>
 * <li>If the given input is a Spannable, the same instance is returned. </li>
 * </ul>
 *
 * @param charSequence CharSequence to add the EmojiSpans, cannot be {@code null}
 * @param start start index in the charSequence to look for emojis, should be greater than or
 *              equal to {@code 0}, also less than {@code charSequence.length()}
 * @param end end index in the charSequence to look for emojis, should be greater than or
 *            equal to {@code start} parameter, also less than {@code charSequence.length()}
 * @param maxEmojiCount maximum number of emojis in the {@code charSequence}, should be greater
 *                      than or equal to {@code 0}
 * @param replaceAll whether to replace all emoji with {@link EmojiSpan}s
 */
CharSequence process(@NonNull final CharSequence charSequence, @IntRange(from = 0) int start,
        @IntRange(from = 0) int end, @IntRange(from = 0) int maxEmojiCount, final boolean replaceAll) {
    final boolean isSpannableBuilder = charSequence instanceof SpannableBuilder;
    if (isSpannableBuilder) {
        ((SpannableBuilder) charSequence).beginBatchEdit();
    }

    try {
        Spannable spannable = null;
        // if it is a spannable already, use the same instance to add/remove EmojiSpans.
        // otherwise wait until the the first EmojiSpan found in order to change the result
        // into a Spannable.
        if (isSpannableBuilder || charSequence instanceof Spannable) {
            spannable = (Spannable) charSequence;
        }

        if (spannable != null) {
            final EmojiSpan[] spans = spannable.getSpans(start, end, EmojiSpan.class);
            if (spans != null && spans.length > 0) {
                // remove existing spans, and realign the start, end according to spans
                // if start or end is in the middle of an emoji they should be aligned
                final int length = spans.length;
                for (int index = 0; index < length; index++) {
                    final EmojiSpan span = spans[index];
                    final int spanStart = spannable.getSpanStart(span);
                    final int spanEnd = spannable.getSpanEnd(span);
                    // Remove span only when its spanStart is NOT equal to current end.
                    // During add operation an emoji at index 0 is added with 0-1 as start and
                    // end indices. Therefore if there are emoji spans at [0-1] and [1-2]
                    // and end is 1, the span between 0-1 should be deleted, not 1-2.
                    if (spanStart != end) {
                        spannable.removeSpan(span);
                    }
                    start = Math.min(spanStart, start);
                    end = Math.max(spanEnd, end);
                }
            }
        }

        if (start == end || start >= charSequence.length()) {
            return charSequence;
        }

        // calculate max number of emojis that can be added. since getSpans call is a relatively
        // expensive operation, do it only when maxEmojiCount is not unlimited.
        if (maxEmojiCount != EmojiCompat.EMOJI_COUNT_UNLIMITED && spannable != null) {
            maxEmojiCount -= spannable.getSpans(0, spannable.length(), EmojiSpan.class).length;
        }
        // add new ones
        int addedCount = 0;
        final ProcessorSm sm = new ProcessorSm(mMetadataRepo.getRootNode());

        int currentOffset = start;
        int codePoint = Character.codePointAt(charSequence, currentOffset);

        while (currentOffset < end && addedCount < maxEmojiCount) {
            final int action = sm.check(codePoint);

            switch (action) {
            case ACTION_ADVANCE_BOTH:
                start += Character.charCount(Character.codePointAt(charSequence, start));
                currentOffset = start;
                if (currentOffset < end) {
                    codePoint = Character.codePointAt(charSequence, currentOffset);
                }
                break;
            case ACTION_ADVANCE_END:
                currentOffset += Character.charCount(codePoint);
                if (currentOffset < end) {
                    codePoint = Character.codePointAt(charSequence, currentOffset);
                }
                break;
            case ACTION_FLUSH:
                if (replaceAll || !hasGlyph(charSequence, start, currentOffset, sm.getFlushMetadata())) {
                    if (spannable == null) {
                        spannable = new SpannableString(charSequence);
                    }
                    addEmoji(spannable, sm.getFlushMetadata(), start, currentOffset);
                    addedCount++;
                }
                start = currentOffset;
                break;
            }
        }

        // After the last codepoint is consumed the state machine might be in a state where it
        // identified an emoji before. i.e. abc[women-emoji] when the last codepoint is consumed
        // state machine is waiting to see if there is an emoji sequence (i.e. ZWJ).
        // Need to check if it is in such a state.
        if (sm.isInFlushableState() && addedCount < maxEmojiCount) {
            if (replaceAll || !hasGlyph(charSequence, start, currentOffset, sm.getCurrentMetadata())) {
                if (spannable == null) {
                    spannable = new SpannableString(charSequence);
                }
                addEmoji(spannable, sm.getCurrentMetadata(), start, currentOffset);
                addedCount++;
            }
        }
        return spannable == null ? charSequence : spannable;
    } finally {
        if (isSpannableBuilder) {
            ((SpannableBuilder) charSequence).endBatchEdit();
        }
    }
}

From source file:com.dnw.json.J.java

/**
 * Escapes each character to make the string can be denoted as a JSON string.
 * //from   www.ja  v  a  2 s .com
 * @author manbaum
 * @since Oct 11, 2014
 * @param text a string to have all characters to be escaped.
 * @return the escaped string.
 */
private final static String escape(final CharSequence text) {
    final StringBuffer sb = new StringBuffer();
    for (int i = 0; i < text.length(); i++) {
        char ch = text.charAt(i);
        if (ch == 0) {
            sb.append("\\0");
        } else if (ch == '\n') {
            sb.append("\\n");
        } else if (ch == '\r') {
            sb.append("\\r");
        } else if (ch < 32) {
            sb.append("\\x");
            if (ch < 16) {
                sb.append('0');
            }
            sb.append(Integer.toHexString(ch));
        } else if (ch == '\\' || ch == '\'' || ch == '\"') {
            sb.append("\\");
            sb.append(ch);
        } else if (ch <= 126) {
            sb.append(ch);
        } else {
            int n = Character.codePointAt(text, i);
            sb.append("\\u");
            if (n < 16) {
                sb.append("000");
            } else if (n < 256) {
                sb.append("00");
            } else if (n < 4096) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(n));
        }
    }
    return sb.toString();
}

From source file:org.openo.nfvo.jujuvnfmadapter.common.VNFJsonUtil.java

/**
 * @param context the HttpContext// www .ja  v  a2  s.c  o m
 * @param <T> JSONObject or JSONArray
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T getJsonFromContext(HttpServletRequest context) {
    try {
        InputStream input = context.getInputStream();
        String vnfJsonStr = IOUtils.toString(input);
        JSONTokener vnfVnfJsonTokener = new JSONTokener(vnfJsonStr);

        // "{"
        if (vnfVnfJsonTokener.nextClean() == Character.codePointAt("{", 0)) {
            return (T) JSONObject.fromObject(vnfJsonStr);
        }

        vnfVnfJsonTokener.back();

        // "["
        if (vnfVnfJsonTokener.nextClean() == Character.codePointAt("[", 0)) {
            return (T) JSONArray.fromObject(vnfJsonStr);
        }
    } catch (IOException e) {
        LOG.error("function=getJsonFromContext,msg= IOException occurs. exception=" + e);
    } catch (JSONException e) {
        LOG.error("function=getJsonFromContext,msg= JSONException occurs, exception=" + e);
    }
    return null;
}