Created
January 30, 2014 22:01
-
-
Save nelhage/8720899 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
1728 * Code shared by String and StringBuffer to do searches. The | |
1729 * source is the character array being searched, and the target | |
1730 * is the string being searched for. | |
1731 * | |
1732 * @param source the characters being searched. | |
1733 * @param sourceOffset offset of the source string. | |
1734 * @param sourceCount count of the source string. | |
1735 * @param target the characters being searched for. | |
1736 * @param targetOffset offset of the target string. | |
1737 * @param targetCount count of the target string. | |
1738 * @param fromIndex the index to begin searching from. | |
1739 */ | |
1740 static int indexOf(char[] source, int sourceOffset, int sourceCount, | |
1741 char[] target, int targetOffset, int targetCount, | |
1742 int fromIndex) { | |
1743 if (fromIndex >= sourceCount) { | |
1744 return (targetCount == 0 ? sourceCount : -1); | |
1745 } | |
1746 if (fromIndex < 0) { | |
1747 fromIndex = 0; | |
1748 } | |
1749 if (targetCount == 0) { | |
1750 return fromIndex; | |
1751 } | |
1752 | |
1753 char first = target[targetOffset]; | |
1754 int max = sourceOffset + (sourceCount - targetCount); | |
1755 | |
1756 for (int i = sourceOffset + fromIndex; i <= max; i++) { | |
1757 /* Look for first character. */ | |
1758 if (source[i] != first) { | |
1759 while (++i <= max && source[i] != first); | |
1760 } | |
1761 | |
1762 /* Found first character, now look at the rest of v2 */ | |
1763 if (i <= max) { | |
1764 int j = i + 1; | |
1765 int end = j + targetCount - 1; | |
1766 for (int k = targetOffset + 1; j < end && source[j] | |
1767 == target[k]; j++, k++); | |
1768 | |
1769 if (j == end) { | |
1770 /* Found whole string. */ | |
1771 return i - sourceOffset; | |
1772 } | |
1773 } | |
1774 } | |
1775 return -1; | |
1776 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment