< prev index next >

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

Print this page
rev 58157 : 8240094: Optimize empty substring handling
Reviewed-by: redestad, igerasim, jlaskey
Contributed-by: sergei.tsypanov@yandex.ru


1802      * If no such value of {@code k} exists, then {@code -1} is returned.
1803      *
1804      * @param   str         the substring to search for.
1805      * @param   fromIndex   the index to start the search from.
1806      * @return  the index of the last occurrence of the specified substring,
1807      *          searching backward from the specified index,
1808      *          or {@code -1} if there is no such occurrence.
1809      */
1810     public int lastIndexOf(String str, int fromIndex) {
1811         return lastIndexOf(value, coder(), length(), str, fromIndex);
1812     }
1813 
1814     /**
1815      * Code shared by String and AbstractStringBuilder to do searches. The
1816      * source is the character array being searched, and the target
1817      * is the string being searched for.
1818      *
1819      * @param   src         the characters being searched.
1820      * @param   srcCoder    coder handles the mapping between bytes/chars
1821      * @param   srcCount    count of the source string.
1822      * @param   tgt         the characters being searched for.
1823      * @param   fromIndex   the index to begin searching from.
1824      */
1825     static int lastIndexOf(byte[] src, byte srcCoder, int srcCount,
1826                            String tgtStr, int fromIndex) {
1827         byte[] tgt = tgtStr.value;
1828         byte tgtCoder = tgtStr.coder();
1829         int tgtCount = tgtStr.length();
1830         /*
1831          * Check arguments; return immediately where possible. For
1832          * consistency, don't check for null str.
1833          */
1834         int rightIndex = srcCount - tgtCount;
1835         if (fromIndex > rightIndex) {
1836             fromIndex = rightIndex;
1837         }
1838         if (fromIndex < 0) {
1839             return -1;
1840         }
1841         /* Empty string always matches. */
1842         if (tgtCount == 0) {


1883      * <p>
1884      * Examples:
1885      * <blockquote><pre>
1886      * "hamburger".substring(4, 8) returns "urge"
1887      * "smiles".substring(1, 5) returns "mile"
1888      * </pre></blockquote>
1889      *
1890      * @param      beginIndex   the beginning index, inclusive.
1891      * @param      endIndex     the ending index, exclusive.
1892      * @return     the specified substring.
1893      * @throws     IndexOutOfBoundsException  if the
1894      *             {@code beginIndex} is negative, or
1895      *             {@code endIndex} is larger than the length of
1896      *             this {@code String} object, or
1897      *             {@code beginIndex} is larger than
1898      *             {@code endIndex}.
1899      */
1900     public String substring(int beginIndex, int endIndex) {
1901         int length = length();
1902         checkBoundsBeginEnd(beginIndex, endIndex, length);
1903         int subLen = endIndex - beginIndex;
1904         if (beginIndex == 0 && endIndex == length) {
1905             return this;
1906         }

1907         return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen)
1908                           : StringUTF16.newString(value, beginIndex, subLen);
1909     }
1910 
1911     /**
1912      * Returns a character sequence that is a subsequence of this sequence.
1913      *
1914      * <p> An invocation of this method of the form
1915      *
1916      * <blockquote><pre>
1917      * str.subSequence(begin,&nbsp;end)</pre></blockquote>
1918      *
1919      * behaves in exactly the same way as the invocation
1920      *
1921      * <blockquote><pre>
1922      * str.substring(begin,&nbsp;end)</pre></blockquote>
1923      *
1924      * @apiNote
1925      * This method is defined so that the {@code String} class can implement
1926      * the {@link CharSequence} interface.




1802      * If no such value of {@code k} exists, then {@code -1} is returned.
1803      *
1804      * @param   str         the substring to search for.
1805      * @param   fromIndex   the index to start the search from.
1806      * @return  the index of the last occurrence of the specified substring,
1807      *          searching backward from the specified index,
1808      *          or {@code -1} if there is no such occurrence.
1809      */
1810     public int lastIndexOf(String str, int fromIndex) {
1811         return lastIndexOf(value, coder(), length(), str, fromIndex);
1812     }
1813 
1814     /**
1815      * Code shared by String and AbstractStringBuilder to do searches. The
1816      * source is the character array being searched, and the target
1817      * is the string being searched for.
1818      *
1819      * @param   src         the characters being searched.
1820      * @param   srcCoder    coder handles the mapping between bytes/chars
1821      * @param   srcCount    count of the source string.
1822      * @param   tgtStr      the characters being searched for.
1823      * @param   fromIndex   the index to begin searching from.
1824      */
1825     static int lastIndexOf(byte[] src, byte srcCoder, int srcCount,
1826                            String tgtStr, int fromIndex) {
1827         byte[] tgt = tgtStr.value;
1828         byte tgtCoder = tgtStr.coder();
1829         int tgtCount = tgtStr.length();
1830         /*
1831          * Check arguments; return immediately where possible. For
1832          * consistency, don't check for null str.
1833          */
1834         int rightIndex = srcCount - tgtCount;
1835         if (fromIndex > rightIndex) {
1836             fromIndex = rightIndex;
1837         }
1838         if (fromIndex < 0) {
1839             return -1;
1840         }
1841         /* Empty string always matches. */
1842         if (tgtCount == 0) {


1883      * <p>
1884      * Examples:
1885      * <blockquote><pre>
1886      * "hamburger".substring(4, 8) returns "urge"
1887      * "smiles".substring(1, 5) returns "mile"
1888      * </pre></blockquote>
1889      *
1890      * @param      beginIndex   the beginning index, inclusive.
1891      * @param      endIndex     the ending index, exclusive.
1892      * @return     the specified substring.
1893      * @throws     IndexOutOfBoundsException  if the
1894      *             {@code beginIndex} is negative, or
1895      *             {@code endIndex} is larger than the length of
1896      *             this {@code String} object, or
1897      *             {@code beginIndex} is larger than
1898      *             {@code endIndex}.
1899      */
1900     public String substring(int beginIndex, int endIndex) {
1901         int length = length();
1902         checkBoundsBeginEnd(beginIndex, endIndex, length);

1903         if (beginIndex == 0 && endIndex == length) {
1904             return this;
1905         }
1906         int subLen = endIndex - beginIndex;
1907         return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen)
1908                           : StringUTF16.newString(value, beginIndex, subLen);
1909     }
1910 
1911     /**
1912      * Returns a character sequence that is a subsequence of this sequence.
1913      *
1914      * <p> An invocation of this method of the form
1915      *
1916      * <blockquote><pre>
1917      * str.subSequence(begin,&nbsp;end)</pre></blockquote>
1918      *
1919      * behaves in exactly the same way as the invocation
1920      *
1921      * <blockquote><pre>
1922      * str.substring(begin,&nbsp;end)</pre></blockquote>
1923      *
1924      * @apiNote
1925      * This method is defined so that the {@code String} class can implement
1926      * the {@link CharSequence} interface.


< prev index next >