Example usage for java.lang CharSequence charAt

List of usage examples for java.lang CharSequence charAt

Introduction

In this page you can find the example usage for java.lang CharSequence charAt.

Prototype

char charAt(int index);

Source Link

Document

Returns the char value at the specified index.

Usage

From source file:com.healthmarketscience.jackcess.Column.java

/**
 * Returns {@code true} if the given text can be compressed using simple
 * ASCII encoding, {@code false} otherwise.
 *///  w w w  .ja  v a  2s  .  co  m
private static boolean isAsciiCompressible(CharSequence text) {
    // only attempt to compress > 2 chars (compressing less than 3 chars would
    // not result in a space savings due to the 2 byte compression header)
    if (text.length() <= TEXT_COMPRESSION_HEADER.length) {
        return false;
    }
    // now, see if it is all printable ASCII
    for (int i = 0; i < text.length(); ++i) {
        char c = text.charAt(i);
        if (!Compress.isAsciiCrLfOrTab(c)) {
            return false;
        }
    }
    return true;
}

From source file:com.google.dart.tools.ui.internal.text.dart.DartAutoIndentStrategy_NEW.java

/**
 * Computes the difference of two indentations and returns the difference in length of current and
 * correct. If the return value is positive, <code>addition</code> is initialized with a substring
 * of that length of <code>correct</code>.
 * //from  w  ww  .  ja  va  2 s .c  om
 * @param correct the correct indentation
 * @param current the current indentation (might contain non-whitespace)
 * @param difference a string buffer - if the return value is positive, it will be cleared and set
 *          to the substring of <code>current</code> of that length
 * @param tabLength the length of a tab
 * @return the difference in length of <code>correct</code> and <code>current</code>
 */
private int subtractIndent(CharSequence correct, CharSequence current, StringBuffer difference, int tabLength) {
    int c1 = computeVisualLength(correct, tabLength);
    int c2 = computeVisualLength(current, tabLength);
    int diff = c1 - c2;
    if (diff <= 0) {
        return diff;
    }

    difference.setLength(0);
    int len = 0, i = 0;
    while (len < diff) {
        char c = correct.charAt(i++);
        difference.append(c);
        len += computeVisualLength(c, tabLength);
    }

    return diff;
}

From source file:cn.sinobest.jzpt.framework.utils.string.StringUtils.java

/**
 * HTML//from  w w  w. j  ava 2  s . com
 */
public static CharSequence escapeHtml(CharSequence s, boolean unicode) {
    if (unicode)
        return StringEscapeUtils.escapeHtml3(s.toString());
    StringBuilder sb = new StringBuilder(s.length() + 16);
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        int n = htmlEscEntities.indexOf(c);
        if (n > -1) {
            sb.append(htmlEscapeSequence[n]);
        } else {
            sb.append(c);
        }
    }
    return sb.length() == s.length() ? s : sb.toString();// string
}

From source file:com.appeaser.sublimepickerlibrary.timepicker.SublimeTimePicker.java

/**
 * Get the keycode value for AM and PM in the current language.
 *//* w w  w.j  a va2s  .c  om*/
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        final KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        final CharSequence amText = mAmText.toLowerCase(mCurrentLocale);
        final CharSequence pmText = mPmText.toLowerCase(mCurrentLocale);
        final int N = Math.min(amText.length(), pmText.length());
        for (int i = 0; i < N; i++) {
            final char amChar = amText.charAt(i);
            final char pmChar = pmText.charAt(i);
            if (amChar != pmChar) {
                // There should be 4 events: a down and up for both AM and PM.
                final KeyEvent[] events = kcm.getEvents(new char[] { amChar, pmChar });
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }

    if (amOrPm == AM) {
        return mAmKeyCode;
    } else if (amOrPm == PM) {
        return mPmKeyCode;
    }

    return -1;
}

From source file:com.github.irshulx.Components.InputExtensions.java

public CustomEditText getNewEditTextInst(final String hint, CharSequence text) {
    final CustomEditText editText = new CustomEditText(
            new ContextThemeWrapper(this.editorCore.getContext(), R.style.WysiwygEditText));
    addEditableStyling(editText);//ww w.  j  a va  2 s.  c  o m
    editText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    if (hint != null) {
        editText.setHint(hint);
    }
    if (text != null) {
        setText(editText, text);
    }

    /**
     * create tag for the editor
     */

    EditorControl editorTag = editorCore.createTag(EditorType.INPUT);
    editorTag.textSettings = new TextSettings(this.DEFAULT_TEXT_COLOR);
    editText.setTag(editorTag);
    editText.setBackgroundDrawable(
            ContextCompat.getDrawable(this.editorCore.getContext(), R.drawable.invisible_edit_text));
    editText.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            return editorCore.onKey(v, keyCode, event, editText);
        }
    });
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                editText.clearFocus();
            } else {
                editorCore.setActiveView(v);
            }
        }
    });
    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

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

        @Override
        public void afterTextChanged(Editable s) {

            String text = Html.toHtml(editText.getText());
            Object tag = editText.getTag(R.id.control_tag);
            if (s.length() == 0 && tag != null)
                editText.setHint(tag.toString());
            if (s.length() > 0) {
                /*
                 * if user had pressed enter, replace it with br
                 */
                for (int i = 0; i < s.length(); i++) {
                    if (s.charAt(i) == '\n') {
                        CharSequence subChars = s.subSequence(0, i);
                        SpannableStringBuilder ssb = new SpannableStringBuilder(subChars);
                        text = Html.toHtml(ssb);
                        if (text.length() > 0)
                            setText(editText, text);

                        if (i + 1 == s.length()) {
                            s.clear();
                        }

                        int index = editorCore.getParentView().indexOfChild(editText);
                        /* if the index was 0, set the placeholder to empty, behaviour happens when the user just press enter
                         */
                        if (index == 0) {
                            editText.setHint(null);
                            editText.setTag(R.id.control_tag, hint);
                        }
                        int position = index + 1;
                        CharSequence newText = null;
                        SpannableStringBuilder editable = new SpannableStringBuilder();
                        int lastIndex = s.length();
                        int nextIndex = i + 1;
                        if (nextIndex < lastIndex) {
                            newText = s.subSequence(nextIndex, lastIndex);
                            for (int j = 0; j < newText.length(); j++) {
                                editable.append(newText.charAt(j));
                                if (newText.charAt(j) == '\n') {
                                    editable.append('\n');
                                }
                            }
                        }
                        insertEditText(position, hint, editable);
                        break;
                    }
                }
            }
            if (editorCore.getEditorListener() != null) {
                editorCore.getEditorListener().onTextChanged(editText, s);
            }
        }
    });
    if (this.lineSpacing != -1) {
        setLineSpacing(editText, this.lineSpacing);
    }
    return editText;
}

From source file:com.xperia64.rompatcher.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    staticThis = MainActivity.this;

    setContentView(R.layout.main);//from   ww  w .  ja  v a 2  s  . c  om
    // Load native libraries
    try {
        System.loadLibrary("apsn64patcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab apspatcher!");
    }
    try {
        System.loadLibrary("ipspatcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab ipspatcher!");
    }
    try {
        System.loadLibrary("ipspatcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab ipspatcher!");
    }
    try {
        System.loadLibrary("upspatcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab upspatcher!");
    }
    try {
        System.loadLibrary("xdelta3patcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab xdelta3patcher!");
    }
    try {
        System.loadLibrary("bpspatcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab bpspatcher!");
    }
    try {
        System.loadLibrary("bzip2");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab bzip2!");
    }
    try {
        System.loadLibrary("bsdiffpatcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab bsdiffpatcher!");
    }
    try {
        System.loadLibrary("ppfpatcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab ppfpatcher!");
    }
    try {
        System.loadLibrary("ips32patcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab ips32patcher!");
    }
    try {
        System.loadLibrary("glib-2.0");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab glib-2.0!");
    }
    try {
        System.loadLibrary("gmodule-2.0");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab gmodule-2.0!");
    }
    try {
        System.loadLibrary("edsio");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab edsio!");
    }
    try {
        System.loadLibrary("xdelta1patcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab ips32patcher!");
    }
    try {
        System.loadLibrary("ips32patcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab ips32patcher!");
    }
    try {
        System.loadLibrary("ecmpatcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab ecmpatcher!");
    }
    try {
        System.loadLibrary("dpspatcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab dpspatcher!");
    }
    try {
        System.loadLibrary("dldipatcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab dldipatcher!");
    }
    try {
        System.loadLibrary("xpcpatcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab xpcpatcher!");
    }
    try {
        System.loadLibrary("asarpatcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab asarpatcher!");
    }
    try {
        System.loadLibrary("asmpatcher");
    } catch (UnsatisfiedLinkError e) {
        Log.e("Bad:", "Cannot grab asmpatcher!");
    }
    c = (CheckBox) findViewById(R.id.backupCheckbox);
    d = (CheckBox) findViewById(R.id.altNameCheckbox);
    r = (CheckBox) findViewById(R.id.ignoreCRC);
    e = (CheckBox) findViewById(R.id.fileExtCheckbox);
    ed = (EditText) findViewById(R.id.txtOutFile);

    final Button romButton = (Button) findViewById(R.id.romButton);
    romButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Globals.mode = true;
            Intent intent = new Intent(staticThis, FileBrowserActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivityForResult(intent, 1);

        }
    });
    final Button patchButton = (Button) findViewById(R.id.patchButton);
    patchButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Globals.mode = false;
            Intent intent = new Intent(staticThis, FileBrowserActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivityForResult(intent, 1);
        }
    });
    final ImageButton bkHelp = (ImageButton) findViewById(R.id.backupHelp);
    bkHelp.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            AlertDialog dialog = new AlertDialog.Builder(staticThis).create();
            dialog.setTitle(getResources().getString(R.string.bkup_rom));
            dialog.setMessage(getResources().getString(R.string.bkup_rom_desc));
            dialog.setCancelable(true);
            dialog.setButton(DialogInterface.BUTTON_POSITIVE, getResources().getString(android.R.string.ok),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int buttonId) {

                        }
                    });
            dialog.show();
        }
    });
    final ImageButton altNameHelp = (ImageButton) findViewById(R.id.outfileHelp);
    altNameHelp.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            AlertDialog dialog = new AlertDialog.Builder(staticThis).create();
            dialog.setTitle(getResources().getString(R.string.rename1));
            dialog.setMessage(getResources().getString(R.string.rename_desc));
            dialog.setCancelable(true);
            dialog.setButton(DialogInterface.BUTTON_POSITIVE, getResources().getString(android.R.string.ok),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int buttonId) {

                        }
                    });
            dialog.show();
        }
    });
    final ImageButton chkHelp = (ImageButton) findViewById(R.id.ignoreHelp);
    chkHelp.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            AlertDialog dialog = new AlertDialog.Builder(staticThis).create();
            dialog.setTitle(getResources().getString(R.string.ignoreChks));
            dialog.setMessage(getResources().getString(R.string.ignoreChks_desc));
            dialog.setCancelable(true);
            dialog.setButton(DialogInterface.BUTTON_POSITIVE, getResources().getString(android.R.string.ok),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int buttonId) {

                        }
                    });
            dialog.show();
        }
    });

    InputFilter filter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
                int dend) {
            for (int i = start; i < end; i++) {
                String IC = "*/*\n*\r*\t*\0*\f*`*?***\\*<*>*|*\"*:*";
                if (IC.contains("*" + source.charAt(i) + "*")) {
                    return "";
                }
            }
            return null;
        }
    };
    ed.setFilters(new InputFilter[] { filter });
    c.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (((CheckBox) v).isChecked()) {
                d.setEnabled(true);
                if (d.isChecked()) {
                    ed.setEnabled(true);
                    e.setEnabled(true);
                }
            } else {
                d.setEnabled(false);
                ed.setEnabled(false);
                e.setEnabled(false);
            }
        }
    });
    d.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (((CheckBox) v).isChecked()) {
                ed.setEnabled(true);
                e.setEnabled(true);
            } else {
                e.setEnabled(false);
                ed.setEnabled(false);
            }
        }
    });
    final Button applyButton = (Button) findViewById(R.id.applyPatch);
    applyButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Warn about patching archives.
            if (Globals.fileToPatch.toLowerCase(Locale.US).endsWith(".7z")
                    || Globals.fileToPatch.toLowerCase(Locale.US).endsWith(".zip")
                    || Globals.fileToPatch.toLowerCase(Locale.US).endsWith(".rar")) {
                AlertDialog dialog = new AlertDialog.Builder(staticThis).create();
                dialog.setTitle(getResources().getString(R.string.warning));
                dialog.setMessage(getResources().getString(R.string.zip_warning_desc));
                dialog.setCancelable(false);
                dialog.setButton(DialogInterface.BUTTON_POSITIVE,
                        getResources().getString(android.R.string.yes), new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int buttonId) {

                                patchCheck();
                            }
                        });
                dialog.setButton(DialogInterface.BUTTON_NEGATIVE,
                        getResources().getString(android.R.string.cancel),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int buttonId) {
                                Toast t = Toast.makeText(staticThis, getResources().getString(R.string.nopatch),
                                        Toast.LENGTH_SHORT);
                                t.show();

                            }
                        });
                dialog.show();
            } else {
                patchCheck();
            }

        }
    });
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // Uggh.
        requestPermissions();
    }
}

From source file:net.sf.xfd.provider.ProviderBase.java

public String getTypeFast(@CanonPath String path, String name, Stat stat) throws FileNotFoundException {
    if ("/".equals(path)) {
        return MIME_TYPE_DIR;
    }/*  ww w.j a va  2s  .c om*/

    final OS os = getOS();
    if (os == null) {
        return DEFAULT_MIME;
    }

    try {
        @Fd
        int fd = Fd.NIL;

        @DirFd
        int parentFd = os.opendir(extractParent(path));
        try {
            int flags = 0;

            if (!os.faccessat(parentFd, name, OS.F_OK)) {
                flags = OS.AT_SYMLINK_NOFOLLOW;
            }

            os.fstatat(parentFd, name, stat, flags);

            if (stat.type == null) {
                return DEFAULT_MIME;
            }

            switch (stat.type) {
            case LINK:
                return LINK_MIME;
            case DIRECTORY:
                return MIME_TYPE_DIR;
            case CHAR_DEV:
                return CHAR_DEV_MIME;
            default:
            }

            final String extension = getExtensionFast(name);

            final MimeTypeMap mimeMap = MimeTypeMap.getSingleton();

            final String foundMime = mimeMap.getMimeTypeFromExtension(extension);
            if (foundMime != null) {
                return foundMime;
            }

            final boolean canSniffContent = stat.type == FsType.FILE
                    && (stat.st_size != 0 || isPossiblySpecial(stat));

            if (flags == 0) {
                os.fstatat(parentFd, name, stat, OS.AT_SYMLINK_NOFOLLOW);

                if (stat.type == FsType.LINK) {
                    CharSequence resolved = null;

                    if (canSniffContent) {
                        try {
                            fd = os.openat(parentFd, name, OS.O_RDONLY, 0);

                            resolved = os.readlinkat(DirFd.NIL, fdPath(fd));
                        } catch (IOException ioe) {
                            LogUtil.logCautiously("Unable to open target of " + name, ioe);
                        }
                    }

                    if (resolved == null) {
                        resolved = os.readlinkat(parentFd, name);

                        if (resolved.charAt(0) == '/') {
                            resolved = canonString(resolved);
                        }
                    }

                    final String linkTargetExtension = getExtensionFromPath(resolved);

                    if (linkTargetExtension != null && !linkTargetExtension.equals(extension)) {
                        final String sortaFastMime = mimeMap.getMimeTypeFromExtension(linkTargetExtension);
                        if (sortaFastMime != null) {
                            return sortaFastMime;
                        }
                    }
                }
            }

            if (canSniffContent) {
                if (fd < 0) {
                    fd = os.openat(parentFd, name, OS.O_RDONLY, 0);
                }

                final String contentInfo = magic.guessMime(fd);

                if (contentInfo != null) {
                    return contentInfo;
                }
            }

            switch (stat.type) {
            case LINK:
                return LINK_MIME;
            case DOMAIN_SOCKET:
                return SOCK_MIME;
            case NAMED_PIPE:
                return FIFO_MIME;
            case FILE:
                if (stat.st_size == 0) {
                    return EMPTY_MIME;
                }
            default:
                return DEFAULT_MIME;
            }
        } finally {
            if (fd > 0) {
                os.dispose(fd);
            }

            os.dispose(parentFd);
        }
    } catch (IOException e) {
        LogUtil.logCautiously("Encountered IO error during mime sniffing", e);

        throw new FileNotFoundException("Failed to stat " + name);
    }
}

From source file:jef.tools.StringUtils.java

/**
 * HTML//from  w  w w  .  j  a v a2 s. co  m
 */
public static CharSequence escapeHtml(CharSequence s, boolean unicode) {
    if (unicode)
        return StringEscapeUtils.escapeHtml(s.toString());
    StringBuilder sb = new StringBuilder(s.length() + 16);
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        int n = htmlEscEntities.indexOf(c);
        if (n > -1) {
            sb.append(htmlEscapeSequence[n]);
        } else {
            sb.append(c);
        }
    }
    return sb.length() == s.length() ? s : sb.toString();// ??string?
}

From source file:com.jecelyin.editor.v2.core.text.TextUtils.java

public static int indexOf(CharSequence s, char ch, int start, int end) {
    Class<? extends CharSequence> c = s.getClass();

    if (s instanceof GetChars || c == StringBuffer.class || c == StringBuilder.class || c == String.class) {
        final int INDEX_INCREMENT = 500;
        char[] temp = obtain(INDEX_INCREMENT);

        while (start < end) {
            int segend = start + INDEX_INCREMENT;
            if (segend > end)
                segend = end;/*from w  ww  .j a  v a 2s . com*/

            getChars(s, start, segend, temp, 0);

            int count = segend - start;
            for (int i = 0; i < count; i++) {
                if (temp[i] == ch) {
                    recycle(temp);
                    return i + start;
                }
            }

            start = segend;
        }

        recycle(temp);
        return -1;
    }

    for (int i = start; i < end; i++)
        if (s.charAt(i) == ch)
            return i;

    return -1;
}

From source file:com.android.talkback.SpeechController.java

/**
 * Spells the text.//from  w  w  w  .  j  a v  a  2s .com
 */
public boolean spellUtterance(CharSequence text) {
    if (TextUtils.isEmpty(text)) {
        return false;
    }

    final SpannableStringBuilder builder = new SpannableStringBuilder();

    for (int i = 0; i < text.length(); i++) {
        final String cleanedChar = SpeechCleanupUtils.getCleanValueFor(mService, text.charAt(i));

        StringBuilderUtils.appendWithSeparator(builder, cleanedChar);
    }

    speak(builder, null, null, QUEUE_MODE_FLUSH_ALL, UTTERANCE_GROUP_DEFAULT, FeedbackItem.FLAG_NO_HISTORY,
            null, null, null);
    return true;
}