src/share/classes/java/lang/String.java

Print this page




1735      * @param   sourceCount  count of the source string.
1736      * @param   target       the characters being searched for.
1737      * @param   targetOffset offset of the target string.
1738      * @param   targetCount  count of the target string.
1739      * @param   fromIndex    the index to begin searching from.
1740      */
1741     static int indexOf(char[] source, int sourceOffset, int sourceCount,
1742             char[] target, int targetOffset, int targetCount,
1743             int fromIndex) {
1744         if (fromIndex >= sourceCount) {
1745             return (targetCount == 0 ? sourceCount : -1);
1746         }
1747         if (fromIndex < 0) {
1748             fromIndex = 0;
1749         }
1750         if (targetCount == 0) {
1751             return fromIndex;
1752         }
1753 
1754         char first = target[targetOffset];




1755         int max = sourceOffset + (sourceCount - targetCount);
1756 
1757         for (int i = sourceOffset + fromIndex; i <= max; i++) {
1758             /* Look for first character. */
1759             if (source[i] != first) {
1760                 while (++i <= max && source[i] != first);
1761             }
1762 


1763             /* Found first character, now look at the rest of v2 */
1764             if (i <= max) {
1765                 int j = i + 1;
1766                 int end = j + targetCount - 1;
1767                 for (int k = targetOffset + 1; j < end && source[j]
1768                         == target[k]; j++, k++);
1769 
1770                 if (j == end) {
1771                     /* Found whole string. */
1772                     return i - sourceOffset;
1773                 }
1774             }
1775         }
1776         return -1;
1777     }
1778 
1779     /**
1780      * Returns the index within this string of the last occurrence of the
1781      * specified substring.  The last occurrence of the empty string ""
1782      * is considered to occur at the index value {@code this.length()}.
1783      *
1784      * <p>The returned index is the largest value {@code k} for which:
1785      * <pre>{@code
1786      * this.startsWith(str, k)
1787      * }</pre>




1735      * @param   sourceCount  count of the source string.
1736      * @param   target       the characters being searched for.
1737      * @param   targetOffset offset of the target string.
1738      * @param   targetCount  count of the target string.
1739      * @param   fromIndex    the index to begin searching from.
1740      */
1741     static int indexOf(char[] source, int sourceOffset, int sourceCount,
1742             char[] target, int targetOffset, int targetCount,
1743             int fromIndex) {
1744         if (fromIndex >= sourceCount) {
1745             return (targetCount == 0 ? sourceCount : -1);
1746         }
1747         if (fromIndex < 0) {
1748             fromIndex = 0;
1749         }
1750         if (targetCount == 0) {
1751             return fromIndex;
1752         }
1753 
1754         char first = target[targetOffset];
1755         char second = 0;
1756         if (targetCount != 1) {
1757             second = target[targetOffset + 1];
1758         }
1759         int max = sourceOffset + (sourceCount - targetCount);
1760 
1761         for (int i = sourceOffset + fromIndex; i <= max; i++) {
1762             /* Look for first character. */
1763             if (source[i] != first) {
1764                 while (++i <= max && source[i] != first);
1765             }
1766             if (targetCount == 1) {
1767                 return (i <= max) ? i - sourceOffset : -1;
1768             }
1769             /* Found first character, now look at the rest of v2 */
1770             if (i <= max && source[i + 1] == second) {
1771                 int j = i + 2;
1772                 int end = i + targetCount;
1773                 for (int k = targetOffset + 2; j < end && source[j]
1774                         == target[k]; j++, k++);
1775 
1776                 if (j == end) {
1777                     /* Found whole string. */
1778                     return i - sourceOffset;
1779                 }
1780             }
1781         }
1782         return -1;
1783     }
1784 
1785     /**
1786      * Returns the index within this string of the last occurrence of the
1787      * specified substring.  The last occurrence of the empty string ""
1788      * is considered to occur at the index value {@code this.length()}.
1789      *
1790      * <p>The returned index is the largest value {@code k} for which:
1791      * <pre>{@code
1792      * this.startsWith(str, k)
1793      * }</pre>