- #include "stdafx.h"
- #include
- #include
- #include
- void MatchString(const wchar_t *pcszSource, const wchar_t *pcszMatchChars,
- std::vector & index)
- {
- std::locale loc;
- bool bMatchIsHigh;
- bool bSrcIsHigh;
- for(size_t idx = 0; *pcszSource != '\0'; idx++)
- {
- if(*pcszMatchChars != '\0')
- {
- bMatchIsHigh = IS_HIGH_SURROGATE(*pcszMatchChars);
- bSrcIsHigh = IS_HIGH_SURROGATE(*pcszSource);
- if(!bMatchIsHigh && !bSrcIsHigh)
- {
- if(std::tolower(*pcszSource,loc) == std::tolower(*pcszMatchChars,loc))
- {
- index.push_back(idx);
- pcszMatchChars++;
- }
- pcszSource++;
- }
- else if(bMatchIsHigh && bSrcIsHigh)
- {
- if( *pcszSource+1 != '\0' && *pcszMatchChars+1 != '\0')
- {
- UINT32 a = ((*pcszSource - 0xD800) * 0x400) +
- (*pcszSource+1 - 0xDC00) + 0x10000;
- UINT32 b = ((*pcszMatchChars - 0xD800) * 0x400) +
- (*pcszMatchChars+1 - 0xDC00) + 0x10000;
- if(std::tolower(a,loc) == std::tolower(b,loc))
- {
- index.push_back(idx);
- pcszMatchChars++;
- pcszMatchChars++;
- }
- pcszSource++;
- pcszSource++;
- }
- else
- {
- return;// bad format
- }
- }
- else if(bMatchIsHigh && !bSrcIsHigh)
- {
- pcszSource++;
- }
- else if(!bMatchIsHigh && bSrcIsHigh)
- {
- pcszSource++;
- pcszSource++;
- }
- }
- else
- {
- break;
- }
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- std::vector index;
- wchar_t a[] = {'d', 'a', 0xD834, 0xDD1E,'\0'};
- wchar_t b[] = {'a', 0xD834, 0xDD1E,'\0'};
- //wchar_t a[] = {'L','u','k','e',' ',0xD834,0xDD1E,'w','a','r','m',' ','m','i','l','k','\0'};
- //wchar_t b[] = {'K',0xD834,0xDD1E,'L','\0'}; // 2 5 13
- //wchar_t a[] = {'L','u','k','e',' ','w','a','r','m',' ','m','i','l','k','\0'};
- //wchar_t b[] = {'K','M','L','\0'};
- MatchString(a,b,index);
- for(size_t i = 0; i < index.size(); i++)
- {
- wprintf(_T("%ld "),index[i]);
- }
- system("pause");
- return 0;
- }