move helpers into RageUtil

ToLower attributs, not ToUpper
This commit is contained in:
Glenn Maynard
2005-09-04 23:04:21 +00:00
parent 7c88d5ed88
commit 7a561fc4d5
3 changed files with 104 additions and 55 deletions
+2 -55
View File
@@ -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<CString,CString> &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<CString,CString>::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);
}
+98
View File
@@ -3,6 +3,7 @@
#include "RageMath.h"
#include "RageLog.h"
#include "RageFile.h"
#include "Foreach.h"
#include <numeric>
#include <ctime>
@@ -1191,6 +1192,103 @@ CString WcharToUTF8( wchar_t c )
return ret;
}
/* &a; -> a */
void ReplaceEntityText( CString &sText, const map<CString,CString> &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<CString,CString>::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<char,CString> &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<char,CString>::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 )
+4
View File
@@ -3,6 +3,8 @@
#ifndef RAGE_UTIL_H
#define RAGE_UTIL_H
#include <map>
#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<CString,CString> &m );
void ReplaceEntityText( CString &sText, const map<char,CString> &m );
void Replace_Unicode_Markers( CString &Text );
CString WcharDisplayText(wchar_t c);