From 7a561fc4d53c6ea67017a85f090187dff7bfd93a Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 4 Sep 2005 23:04:21 +0000 Subject: [PATCH] move helpers into RageUtil ToLower attributs, not ToUpper --- stepmania/src/FontCharAliases.cpp | 57 +----------------- stepmania/src/RageUtil.cpp | 98 +++++++++++++++++++++++++++++++ stepmania/src/RageUtil.h | 4 ++ 3 files changed, 104 insertions(+), 55 deletions(-) diff --git a/stepmania/src/FontCharAliases.cpp b/stepmania/src/FontCharAliases.cpp index f601b7e8ef..7eeb607067 100644 --- a/stepmania/src/FontCharAliases.cpp +++ b/stepmania/src/FontCharAliases.cpp @@ -324,69 +324,16 @@ static void InitCharAliases() { CString from = i->first; CString to = WcharToUTF8(i->second); - from.MakeUpper(); + from.MakeLower(); CharAliasRepl[from] = to; } } -static void ReplaceText( CString &sText, const map &m ) -{ - CString sRet; - - size_t iOffset = 0; - while( iOffset != sText.size() ) - { - size_t iStart = sText.find( '&', iOffset ); - if( iStart == sText.npos ) - { - /* Optimization: if we didn't replace anything at all, do nothing. */ - if( iOffset == 0 ) - return; - - // Append the rest of the string. - sRet.append( sText, iOffset, sRet.npos ); - break; - } - - // Append the text between iOffset and iStart. - sRet.append( sText, iOffset, iStart-iOffset ); - iOffset += iStart-iOffset; - - // Optimization: stop early on "&", so "&&&&&&&&&&&" isn't n^2. - size_t iEnd = sText.find_first_of( "&;", iStart+1 ); - if( iEnd == sText.npos || sText[iEnd] == '&' ) - { - /* & with no matching ;, or two & in a row. Append the & and - * continue. */ - sRet.append( sText, iStart, 1 ); - ++iOffset; - continue; - } - - CString sElement = sText.substr( iStart+1, iEnd-iStart-1 ); - sElement.MakeUpper(); - - map::const_iterator it = m.find( sElement ); - if( it == m.end() ) - { - sRet.append( sText, iStart, iEnd-iStart+1 ); - iOffset = iEnd + 1; - continue; - } - - const CString &sTo = it->second; - sRet.append( sTo ); - iOffset = iEnd + 1; - } - - sText = sRet; -} - /* Replace all &markers; and &#NNNN;s with UTF-8. */ void FontCharAliases::ReplaceMarkers( CString &sText ) { InitCharAliases(); - ReplaceText(sText, CharAliasRepl); + ReplaceEntityText( sText, CharAliasRepl ); Replace_Unicode_Markers(sText); } diff --git a/stepmania/src/RageUtil.cpp b/stepmania/src/RageUtil.cpp index 0a57145ea0..22a9a0cdf7 100644 --- a/stepmania/src/RageUtil.cpp +++ b/stepmania/src/RageUtil.cpp @@ -3,6 +3,7 @@ #include "RageMath.h" #include "RageLog.h" #include "RageFile.h" +#include "Foreach.h" #include #include @@ -1191,6 +1192,103 @@ CString WcharToUTF8( wchar_t c ) return ret; } +/* &a; -> a */ +void ReplaceEntityText( CString &sText, const map &m ) +{ + CString sRet; + + size_t iOffset = 0; + while( iOffset != sText.size() ) + { + size_t iStart = sText.find( '&', iOffset ); + if( iStart == sText.npos ) + { + /* Optimization: if we didn't replace anything at all, do nothing. */ + if( iOffset == 0 ) + return; + + // Append the rest of the string. + sRet.append( sText, iOffset, sRet.npos ); + break; + } + + // Append the text between iOffset and iStart. + sRet.append( sText, iOffset, iStart-iOffset ); + iOffset += iStart-iOffset; + + // Optimization: stop early on "&", so "&&&&&&&&&&&" isn't n^2. + size_t iEnd = sText.find_first_of( "&;", iStart+1 ); + if( iEnd == sText.npos || sText[iEnd] == '&' ) + { + /* & with no matching ;, or two & in a row. Append the & and + * continue. */ + sRet.append( sText, iStart, 1 ); + ++iOffset; + continue; + } + + CString sElement = sText.substr( iStart+1, iEnd-iStart-1 ); + sElement.MakeLower(); + + map::const_iterator it = m.find( sElement ); + if( it == m.end() ) + { + sRet.append( sText, iStart, iEnd-iStart+1 ); + iOffset = iEnd + 1; + continue; + } + + const CString &sTo = it->second; + sRet.append( sTo ); + iOffset = iEnd + 1; + } + + sText = sRet; +} + +/* abcd -> &a; &b; &c; &d; */ +void ReplaceEntityText( CString &sText, const map &m ) +{ + CString sFind; + + FOREACHM_CONST( char, CString, m, c ) + sFind.append( 1, c->first ); + + CString sRet; + + size_t iOffset = 0; + while( iOffset != sText.size() ) + { + size_t iStart = sText.find_first_of( sFind, iOffset ); + if( iStart == sText.npos ) + { + /* Optimization: if we didn't replace anything at all, do nothing. */ + if( iOffset == 0 ) + return; + + // Append the rest of the string. + sRet.append( sText, iOffset, sRet.npos ); + break; + } + + // Append the text between iOffset and iStart. + sRet.append( sText, iOffset, iStart-iOffset ); + iOffset += iStart-iOffset; + + char sElement = sText[iStart]; + + map::const_iterator it = m.find( sElement ); + ASSERT( it != m.end() ); + + const CString &sTo = it->second; + sRet.append( 1, '&' ); + sRet.append( sTo ); + sRet.append( 1, ';' ); + ++iOffset; + } + + sText = sRet; +} /* Replace &#nnnn; (decimal) &xnnnn; (hex) with corresponding UTF-8 characters. */ void Replace_Unicode_Markers( CString &Text ) diff --git a/stepmania/src/RageUtil.h b/stepmania/src/RageUtil.h index b13b7c506d..2afd37dac9 100644 --- a/stepmania/src/RageUtil.h +++ b/stepmania/src/RageUtil.h @@ -3,6 +3,8 @@ #ifndef RAGE_UTIL_H #define RAGE_UTIL_H +#include + #define SAFE_DELETE(p) { delete (p); (p)=NULL; } #define SAFE_DELETE_ARRAY(p) { delete[] (p); (p)=NULL; } @@ -348,6 +350,8 @@ public: }; +void ReplaceEntityText( CString &sText, const map &m ); +void ReplaceEntityText( CString &sText, const map &m ); void Replace_Unicode_Markers( CString &Text ); CString WcharDisplayText(wchar_t c);