Example usage for java.lang String getChars

List of usage examples for java.lang String getChars

Introduction

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

Prototype

public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) 

Source Link

Document

Copies characters from this string into the destination character array.

Usage

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * Appends an object to the builder padding on the left to a fixed width.
 * The <code>toString</code> of the object is used.
 * If the object is larger than the length, the left hand side is lost.
 * If the object is null, the null text value is used.
 * //  w  w w  .  j a  v  a2  s.  c om
 * @param obj
 *            the object to append, null uses null text
 * @param width
 *            the fixed field width, zero or negative has no effect
 * @param padChar
 *            the pad character to use
 * @return this, to enable chaining
 */
public StringCompiler appendFixedWidthPadLeft(Object obj, int width, char padChar) {
    if (width > 0) {
        ensureCapacity(size + width);
        String str = (obj == null ? getNullText() : obj.toString());
        if (str == null) {
            str = "";
        }
        int strLen = str.length();
        if (strLen >= width) {
            str.getChars(strLen - width, strLen, buffer, size);
        } else {
            int padLen = width - strLen;
            for (int i = 0; i < padLen; i++) {
                buffer[size + i] = padChar;
            }
            str.getChars(0, strLen, buffer, size + padLen);
        }
        size += width;
    }
    return this;
}

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * Appends an object to the builder padding on the right to a fixed length.
 * The <code>toString</code> of the object is used.
 * If the object is larger than the length, the right hand side is lost.
 * If the object is null, null text value is used.
 * /*from   www .jav  a 2s  .  co  m*/
 * @param obj
 *            the object to append, null uses null text
 * @param width
 *            the fixed field width, zero or negative has no effect
 * @param padChar
 *            the pad character to use
 * @return this, to enable chaining
 */
public StringCompiler appendFixedWidthPadRight(Object obj, int width, char padChar) {
    if (width > 0) {
        ensureCapacity(size + width);
        String str = (obj == null ? getNullText() : obj.toString());
        if (str == null) {
            str = "";
        }
        int strLen = str.length();
        if (strLen >= width) {
            str.getChars(0, width, buffer, size);
        } else {
            int padLen = width - strLen;
            str.getChars(0, strLen, buffer, size);
            for (int i = 0; i < padLen; i++) {
                buffer[size + strLen + i] = padChar;
            }
        }
        size += width;
    }
    return this;
}

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * Appends a string to this string builder.
 * Appending null will call {@link #appendNull()}.
 * /* w w  w.j a v a2  s .co  m*/
 * @param str
 *            the string to append
 * @return this, to enable chaining
 */
public StringCompiler append(String str) {
    preliminars();
    if (str == null) {
        return appendNull();
    }
    int strLen = str.length();
    if (strLen > 0) {
        int len = length();
        ensureCapacity(len + strLen);
        str.getChars(0, strLen, buffer, len);
        size += strLen;
    }
    return this;
}

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * Internal method to delete a range without validation.
 * // w w w.ja  v a  2  s .c o  m
 * @param startIndex
 *            the start index, must be valid
 * @param endIndex
 *            the end index (exclusive), must be valid
 * @param removeLen
 *            the length to remove (endIndex - startIndex), must be valid
 * @param insertStr
 *            the string to replace with, null means delete range
 * @param insertLen
 *            the length of the insert string, must be valid
 * @throws IndexOutOfBoundsException
 *             if any index is invalid
 */
private void replaceImpl(int startIndex, int endIndex, int removeLen, String insertStr, int insertLen) {
    int newSize = size - removeLen + insertLen;
    if (insertLen != removeLen) {
        ensureCapacity(newSize);
        System.arraycopy(buffer, endIndex, buffer, startIndex + insertLen, size - endIndex);
        size = newSize;
    }
    if (insertLen > 0) {
        insertStr.getChars(0, insertLen, buffer, startIndex);
    }
}

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * Inserts the string into this builder.
 * Inserting null will use the stored null text value.
 * //from  w  w  w  . ja  v a  2  s . com
 * @param index
 *            the index to add at, must be valid
 * @param str
 *            the string to insert
 * @return this, to enable chaining
 * @throws IndexOutOfBoundsException
 *             if the index is invalid
 */
public StringCompiler insert(int index, String str) {
    validateIndex(index);
    if (str == null) {
        str = nullText;
    }
    int strLen = (str == null ? 0 : str.length());
    if (strLen > 0) {
        int newSize = size + strLen;
        ensureCapacity(newSize);
        System.arraycopy(buffer, index, buffer, index + strLen, size - index);
        size = newSize;
        str.getChars(0, strLen, buffer, index); // str cannot be null here
    }
    return this;
}

From source file:it.d4nguard.rgrpg.util.StringCompiler.java

/**
 * Appends part of a string to this string builder.
 * Appending null will call {@link #appendNull()}.
 * /*from   w w  w.j  a  v  a 2s.c  o m*/
 * @param str
 *            the string to append
 * @param startIndex
 *            the start index, inclusive, must be valid
 * @param length
 *            the length to append, must be valid
 * @return this, to enable chaining
 */
public StringCompiler append(String str, int startIndex, int length) {
    preliminars();
    if (str == null) {
        return appendNull();
    }
    if (startIndex < 0 || startIndex > str.length()) {
        throw new StringIndexOutOfBoundsException("startIndex must be valid");
    }
    if (length < 0 || (startIndex + length) > str.length()) {
        throw new StringIndexOutOfBoundsException("length must be valid");
    }
    if (length > 0) {
        int len = length();
        ensureCapacity(len + length);
        str.getChars(startIndex, startIndex + length, buffer, len);
        size += length;
    }
    return this;
}

From source file:org.eclipse.swt.examples.accessibility.CTable.java

String removeMnemonics(String string) {
    /* removes single ampersands and preserves double-ampersands */
    char[] chars = new char[string.length()];
    string.getChars(0, chars.length, chars, 0);
    int i = 0, j = 0;
    for (; i < chars.length; i++, j++) {
        if (chars[i] == '&') {
            if (++i == chars.length)
                break;
            if (chars[i] == '&') {
                chars[j++] = chars[i - 1];
            }//from w  ww . jav a2s  . c  om
        }
        chars[j] = chars[i];
    }
    if (i == j)
        return string;
    return new String(chars, 0, j);
}

From source file:org.openmicroscopy.shoola.env.data.OMEROGateway.java

/**
 * Formats the terms to search for./*from www  . ja  v  a 2 s  . co  m*/
 *
 * @param terms      The terms to search for.
 * @param service   The search service.
 * @return See above.
 * @throws DSOutOfServiceException If the connection is broken, or logged in
 * @throws DSAccessException If an error occurred while trying to
 * retrieve data from OMERO service.
 */
private List<String> prepareTextSearch(String[] terms, SearchPrx service)
        throws DSAccessException, DSOutOfServiceException {
    if (terms == null || terms.length == 0)
        return null;
    String value;
    int n;
    char[] arr;
    String v;
    List<String> formattedTerms = new ArrayList<String>(terms.length);
    String formatted;
    try {
        for (int j = 0; j < terms.length; j++) {
            value = terms[j];
            if (startWithWildCard(value))
                service.setAllowLeadingWildcard(true);
            //format string
            n = value.length();
            arr = new char[n];
            v = "";
            value.getChars(0, n, arr, 0);
            for (int i = 0; i < arr.length; i++) {
                if (SUPPORTED_SPECIAL_CHAR.contains(arr[i]))
                    v += "\\" + arr[i];
                else
                    v += arr[i];
            }
            if (value.contains(" "))
                formatted = "\"" + v.toLowerCase() + "\"";
            else
                formatted = v.toLowerCase();
            formattedTerms.add(formatted);
        }
    } catch (Throwable e) {
        handleException(e, "Cannot format text for search.");
    }
    return formattedTerms;
}

From source file:com.clark.func.Functions.java

/**
 * Internal method to perform the normalization.
 * //from  w  w  w  .  j a v a  2s .com
 * @param filename
 *            the filename
 * @param separator
 *            The separator character to use
 * @param keepSeparator
 *            true to keep the final separator
 * @return the normalized filename
 */
private static String doNormalizePath(String filename, char separator, boolean keepSeparator) {
    if (filename == null) {
        return null;
    }
    int size = filename.length();
    if (size == 0) {
        return filename;
    }
    int prefix = getPrefixLength(filename);
    if (prefix < 0) {
        return null;
    }

    char[] array = new char[size + 2]; // +1 for possible extra slash, +2
    // for arraycopy
    filename.getChars(0, filename.length(), array, 0);

    // fix separators throughout
    char otherSeparator = (separator == SYSTEM_SEPARATOR ? OTHER_SEPARATOR : SYSTEM_SEPARATOR);
    for (int i = 0; i < array.length; i++) {
        if (array[i] == otherSeparator) {
            array[i] = separator;
        }
    }

    // add extra separator on the end to simplify code below
    boolean lastIsDirectory = true;
    if (array[size - 1] != separator) {
        array[size++] = separator;
        lastIsDirectory = false;
    }

    // adjoining slashes
    for (int i = prefix + 1; i < size; i++) {
        if (array[i] == separator && array[i - 1] == separator) {
            System.arraycopy(array, i, array, i - 1, size - i);
            size--;
            i--;
        }
    }

    // dot slash
    for (int i = prefix + 1; i < size; i++) {
        if (array[i] == separator && array[i - 1] == '.' && (i == prefix + 1 || array[i - 2] == separator)) {
            if (i == size - 1) {
                lastIsDirectory = true;
            }
            System.arraycopy(array, i + 1, array, i - 1, size - i);
            size -= 2;
            i--;
        }
    }

    // double dot slash
    outer: for (int i = prefix + 2; i < size; i++) {
        if (array[i] == separator && array[i - 1] == '.' && array[i - 2] == '.'
                && (i == prefix + 2 || array[i - 3] == separator)) {
            if (i == prefix + 2) {
                return null;
            }
            if (i == size - 1) {
                lastIsDirectory = true;
            }
            int j;
            for (j = i - 4; j >= prefix; j--) {
                if (array[j] == separator) {
                    // remove b/../ from a/b/../c
                    System.arraycopy(array, i + 1, array, j + 1, size - i);
                    size -= (i - j);
                    i = j + 1;
                    continue outer;
                }
            }
            // remove a/../ from a/../c
            System.arraycopy(array, i + 1, array, prefix, size - i);
            size -= (i + 1 - prefix);
            i = prefix + 1;
        }
    }

    if (size <= 0) { // should never be less than 0
        return "";
    }
    if (size <= prefix) { // should never be less than prefix
        return new String(array, 0, size);
    }
    if (lastIsDirectory && keepSeparator) {
        return new String(array, 0, size); // keep trailing separator
    }
    return new String(array, 0, size - 1); // lose trailing separator
}