// **************************************************************************** unsigned long cHashedString::CalculateHash(const cString & strIdent) { if(strIdent.IsEmpty()) { return 0; } // largest prime smaller than 65536 unsigned long lBASE = 65521L; // lMAX is the largest n such that // 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 unsigned int lMAX = 5522; unsigned int s1 = 0; unsigned int s2 = 0; int i = 0; unsigned int length = strIdent.GetLength(); while(length > 0) { int k = length < lMAX ? length : lMAX; length -= k; while(--k >= 0) { s1 += (unsigned int)(tolower(strIdent[i++]) & 0XFF); s2 += s1; } s1 %= lBASE; s2 %= lBASE; } return ((s2 << 16) | s1); }
// ******************************************************************************************************************* uint64 cHashedString::CalculateChecksum(const cString & str) { if (str.IsEmpty()) { return 0; } #if 0 // use simple checksum. Keeping this for reference purpose // largest prime smaller than 65536 UINT64 MOD_ADLER = 65521L; // MAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 unsigned int MAX_LENGTH = 5522; unsigned int s1 = 1; unsigned int s2 = 0; unsigned int length = str.GetLength(); while(length > 0) { int k = length < MAX_LENGTH ? length : MAX_LENGTH; length -= k; for(int index = 0; index < k; index++) { s1 = (s1 + (str[index] & 0xFF)) % MOD_ADLER; s2 = (s2 + s1) % MOD_ADLER; } } return ((s2 << 16) | s1); #else uint64 hash = 0; UINT length = str.GetLength(); for (UINT i = 0; i < length; i++) { hash = str[i] + (hash << 6) + (hash << 16) - hash; } return hash & 0x7fffffff; #endif }
// ******************************************************************************************************************* float cSentence::VGetWidth(const cString & strText) const { float fWidth = 0.0f; int istrLength = strText.GetLength(); IMyFont::stVertexData vertexData; for (int i=0; i<istrLength; i++) { int val = (int)strText[i]; vertexData = m_pFont->VGetCharVertexData(val); fWidth += vertexData.ch.XAdvance; } return m_fScale * fWidth; }
// ******************************************************************************************************************* void cString::Tokenize(const cString & delimiters, std::vector<cString> & parts) const { if (IsEmpty() || delimiters.GetLength() == 0) { return; } int iStartIndex = 0; tOptional<int> nextIndex; while (iStartIndex < GetLength()) { nextIndex = FindFirstOfAny(delimiters, iStartIndex); if (nextIndex.IsInvalid()) { nextIndex = GetLength(); } if (iStartIndex != nextIndex.GetValue()) { parts.push_back(GetSubString(iStartIndex, nextIndex.GetValue() - 1)); } iStartIndex = nextIndex.GetValue() + 1; } }