Skip to content

Commit d11b44b

Browse files
committed
Add missing IndexOf
1 parent 2061946 commit d11b44b

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

src/CLR/CorLib/corlib_native_System_String.cpp

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,157 @@ static inline bool MatchString(CLR_RT_UnicodeHelper& inputIter, const char* sear
627627
return true;
628628
}
629629

630+
HRESULT Library_corlib_native_System_String::IndexOf(CLR_RT_StackFrame &stack, int mode) {
631+
NATIVE_PROFILE_CLR_CORE();
632+
NANOCLR_HEADER();
633+
634+
const char *szText;
635+
int startIndex;
636+
int count;
637+
int pos;
638+
const char *pString = NULL;
639+
const CLR_UINT16 *pChars = NULL;
640+
int iChars = 0;
641+
CLR_RT_UnicodeHelper inputIterator;
642+
int inputLen;
643+
int searchLen = 1;
644+
645+
szText = stack.Arg0().RecoverString();
646+
if (!szText) szText = "";
647+
pos = -1;
648+
649+
// Argument processing (unchanged)
650+
if (mode & c_IndexOf__SingleChar) {
651+
pChars = (CLR_UINT16 *)&stack.Arg1().NumericByRefConst().u2;
652+
iChars = 1;
653+
}
654+
else if (mode & c_IndexOf__MultipleChars) {
655+
CLR_RT_HeapBlock_Array *array = stack.Arg1().DereferenceArray();
656+
FAULT_ON_NULL(array);
657+
pChars = (const CLR_UINT16 *)array->GetFirstElement();
658+
iChars = array->m_numOfElements;
659+
}
660+
else if (mode & c_IndexOf__String) {
661+
pString = stack.Arg1().RecoverString();
662+
FAULT_ON_NULL(pString);
663+
inputIterator.SetInputUTF8(pString);
664+
searchLen = inputIterator.CountNumberOfCharacters();
665+
}
666+
667+
// Calculate input length
668+
inputIterator.SetInputUTF8(szText);
669+
inputLen = inputIterator.CountNumberOfCharacters();
670+
if (0 == inputLen) {
671+
pos = -1;
672+
goto Exit;
673+
}
674+
675+
// Start index handling (unchanged)
676+
if (mode & c_IndexOf__StartIndex) {
677+
startIndex = stack.Arg2().NumericByRefConst().s4;
678+
}
679+
else {
680+
if (mode & c_IndexOf__Last) {
681+
startIndex = inputLen - 1;
682+
}
683+
else {
684+
startIndex = 0;
685+
}
686+
}
687+
688+
// Bounds checks (unchanged)
689+
if (startIndex < 0 || startIndex > inputLen)
690+
NANOCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);
691+
692+
// Adjust start index for reverse search
693+
if ((mode & c_IndexOf__String_Last) == c_IndexOf__String_Last) {
694+
startIndex -= searchLen - 1;
695+
if (startIndex < 0 || startIndex > inputLen) goto Exit;
696+
}
697+
698+
// Count calculation (unchanged)
699+
if (mode & c_IndexOf__Count) {
700+
count = stack.Arg3().NumericByRefConst().s4;
701+
}
702+
else {
703+
if (mode & c_IndexOf__Last) {
704+
count = startIndex + 1;
705+
}
706+
else {
707+
count = inputLen - startIndex;
708+
}
709+
}
710+
711+
// Adjust count for forward search
712+
if ((mode & c_IndexOf__String_Last) == c_IndexOf__String) {
713+
count -= searchLen - 1;
714+
}
715+
716+
// Validate count (unchanged)
717+
if (mode & c_IndexOf__Last) {
718+
if (count > startIndex + 1) goto Exit;
719+
}
720+
else {
721+
if (startIndex + count > inputLen)
722+
NANOCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);
723+
}
724+
725+
// Position iterator at start position
726+
if (inputIterator.ConvertFromUTF8(startIndex, true)) {
727+
// String search mode - FIXED
728+
if (pString) {
729+
while (count-- > 0) {
730+
// Use helper for proper UTF-8 comparison
731+
if (MatchString(inputIterator, pString, searchLen)) {
732+
pos = startIndex;
733+
break;
734+
}
735+
736+
// Move to next candidate position
737+
if (mode & c_IndexOf__Last) {
738+
startIndex--;
739+
if (!inputIterator.MoveBackwardInUTF8(szText, 1)) break;
740+
}
741+
else {
742+
startIndex++;
743+
if (!inputIterator.ConvertFromUTF8(1, true)) break;
744+
}
745+
}
746+
}
747+
// Character search mode (unchanged)
748+
else if (pChars) {
749+
while (count-- > 0) {
750+
CLR_UINT16 buf[3] = {0};
751+
inputIterator.m_outputUTF16 = buf;
752+
inputIterator.m_outputUTF16_size = MAXSTRLEN(buf);
753+
754+
if (!inputIterator.ConvertFromUTF8(1, false)) break;
755+
756+
for (int i = 0; i < iChars; i++) {
757+
if (buf[0] == pChars[i]) {
758+
pos = startIndex;
759+
break;
760+
}
761+
}
762+
763+
if (pos != -1) break;
764+
765+
if (mode & c_IndexOf__Last) {
766+
startIndex--;
767+
if (!inputIterator.MoveBackwardInUTF8(szText, 2)) break;
768+
}
769+
else {
770+
startIndex++;
771+
}
772+
}
773+
}
774+
}
775+
776+
Exit:
777+
stack.SetResult_I4(pos);
778+
NANOCLR_NOCLEANUP();
779+
}
780+
630781
HRESULT Library_corlib_native_System_String::ChangeCase(CLR_RT_StackFrame &stack, bool fToUpper)
631782
{
632783
NATIVE_PROFILE_CLR_CORE();

0 commit comments

Comments
 (0)