Guest User

Untitled

a guest
Oct 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. /**
  2. * Code shared by String and StringBuffer to do searches. The
  3. * source is the character array being searched, and the target
  4. * is the string being searched for.
  5. *
  6. * @param source the characters being searched.
  7. * @param sourceOffset offset of the source string.
  8. * @param sourceCount count of the source string.
  9. * @param target the characters being searched for.
  10. * @param targetOffset offset of the target string.
  11. * @param targetCount count of the target string.
  12. * @param fromIndex the index to begin searching from.
  13. */
  14. static int indexOf(char[] source, int sourceOffset, int sourceCount,
  15. char[] target, int targetOffset, int targetCount,
  16. int fromIndex) {
  17. if (fromIndex >= sourceCount) {
  18. return (targetCount == 0 ? sourceCount : -1);
  19. }
  20. if (fromIndex < 0) {
  21. fromIndex = 0;
  22. }
  23. if (targetCount == 0) {
  24. return fromIndex;
  25. }
  26.  
  27. char first = target[targetOffset];
  28. int max = sourceOffset + (sourceCount - targetCount);
  29.  
  30. for (int i = sourceOffset + fromIndex; i <= max; i++) {
  31. /* Look for first character. */
  32. if (source[i] != first) {
  33. while (++i <= max && source[i] != first);
  34. }
  35.  
  36. /* Found first character, now look at the rest of v2 */
  37. if (i <= max) {
  38. int j = i + 1;
  39. int end = j + targetCount - 1;
  40. for (int k = targetOffset + 1; j < end && source[j]
  41. == target[k]; j++, k++);
  42.  
  43. if (j == end) {
  44. /* Found whole string. */
  45. return i - sourceOffset;
  46. }
  47. }
  48. }
  49. return -1;
  50. }
Add Comment
Please, Sign In to add comment