65 public static int indexOf(char[] source, int sourceOffset, int sourceCount,
66 @ConstantNodeParameter char[] target, int targetOffset, int targetCount,
67 int origFromIndex) {
68 int fromIndex = origFromIndex;
69 if (fromIndex >= sourceCount) {
70 return (targetCount == 0 ? sourceCount : -1);
71 }
72 if (fromIndex < 0) {
73 fromIndex = 0;
74 }
75 if (targetCount == 0) {
76 // The empty string is in every string.
77 return fromIndex;
78 }
79
80 int totalOffset = sourceOffset + fromIndex;
81 if (sourceCount - fromIndex < targetCount) {
82 // The empty string contains nothing except the empty string.
83 return -1;
84 }
85 assert sourceCount - fromIndex > 0 && targetCount > 0;
86
87 if (targetCount == 1) {
88 Pointer sourcePointer = Word.objectToTrackedPointer(source).add(charArrayBaseOffset(INJECTED)).add(totalOffset * charArrayIndexScale(INJECTED));
89 int indexOfResult = AMD64ArrayIndexOf.indexOf1Char(sourcePointer, sourceCount - fromIndex, target[targetOffset]);
90 if (indexOfResult >= 0) {
91 return indexOfResult + totalOffset;
92 }
93 return indexOfResult;
94 } else if (targetCount == 2) {
95 Pointer sourcePointer = Word.objectToTrackedPointer(source).add(charArrayBaseOffset(INJECTED)).add(totalOffset * charArrayIndexScale(INJECTED));
96 int indexOfResult = AMD64ArrayIndexOf.indexOfTwoConsecutiveChars(sourcePointer, sourceCount - fromIndex, target[targetOffset], target[targetOffset + 1]);
97 if (indexOfResult >= 0) {
98 return indexOfResult + totalOffset;
99 }
100 return indexOfResult;
101 } else {
102 int haystackLength = sourceCount - (fromIndex + (targetCount - 2));
103 while (haystackLength > 0) {
104 Pointer sourcePointer = Word.objectToTrackedPointer(source).add(charArrayBaseOffset(INJECTED)).add(totalOffset * charArrayIndexScale(INJECTED));
105 int indexOfResult = AMD64ArrayIndexOf.indexOfTwoConsecutiveChars(sourcePointer, haystackLength, target[targetOffset], target[targetOffset + 1]);
|
65 public static int indexOf(char[] source, int sourceOffset, int sourceCount,
66 @ConstantNodeParameter char[] target, int targetOffset, int targetCount,
67 int origFromIndex) {
68 int fromIndex = origFromIndex;
69 if (fromIndex >= sourceCount) {
70 return (targetCount == 0 ? sourceCount : -1);
71 }
72 if (fromIndex < 0) {
73 fromIndex = 0;
74 }
75 if (targetCount == 0) {
76 // The empty string is in every string.
77 return fromIndex;
78 }
79
80 int totalOffset = sourceOffset + fromIndex;
81 if (sourceCount - fromIndex < targetCount) {
82 // The empty string contains nothing except the empty string.
83 return -1;
84 }
85
86 if (targetCount == 1) {
87 Pointer sourcePointer = Word.objectToTrackedPointer(source).add(charArrayBaseOffset(INJECTED)).add(totalOffset * charArrayIndexScale(INJECTED));
88 int indexOfResult = AMD64ArrayIndexOf.indexOf1Char(sourcePointer, sourceCount - fromIndex, target[targetOffset]);
89 if (indexOfResult >= 0) {
90 return indexOfResult + totalOffset;
91 }
92 return indexOfResult;
93 } else if (targetCount == 2) {
94 Pointer sourcePointer = Word.objectToTrackedPointer(source).add(charArrayBaseOffset(INJECTED)).add(totalOffset * charArrayIndexScale(INJECTED));
95 int indexOfResult = AMD64ArrayIndexOf.indexOfTwoConsecutiveChars(sourcePointer, sourceCount - fromIndex, target[targetOffset], target[targetOffset + 1]);
96 if (indexOfResult >= 0) {
97 return indexOfResult + totalOffset;
98 }
99 return indexOfResult;
100 } else {
101 int haystackLength = sourceCount - (fromIndex + (targetCount - 2));
102 while (haystackLength > 0) {
103 Pointer sourcePointer = Word.objectToTrackedPointer(source).add(charArrayBaseOffset(INJECTED)).add(totalOffset * charArrayIndexScale(INJECTED));
104 int indexOfResult = AMD64ArrayIndexOf.indexOfTwoConsecutiveChars(sourcePointer, haystackLength, target[targetOffset], target[targetOffset + 1]);
|