cleanup, remove old regex code

This commit is contained in:
Glenn Maynard
2005-12-23 08:22:21 +00:00
parent 8d93b172ef
commit 81559b2916
+28 -111
View File
@@ -300,7 +300,7 @@ RString ConvertUTF8ToACP( RString s )
#endif #endif
static map<RString,RString> g_mapLangToName2Letter; static map<RString,RString> g_mapLangToName2Letter;
void InitLanguages() static void InitLanguages()
{ {
if( !g_mapLangToName2Letter.empty() ) if( !g_mapLangToName2Letter.empty() )
return; return;
@@ -457,37 +457,37 @@ RString GetLanguageNameFromISO639Code( RString sName )
return ""; return "";
} }
RString join( const RString &Deliminator, const vector<RString>& Source) RString join( const RString &sDeliminator, const vector<RString> &sSource)
{ {
if( Source.empty() ) if( sSource.empty() )
return RString(); return RString();
RString csTmp; RString sTmp;
// Loop through the Array and Append the Deliminator for( unsigned iNum = 0; iNum < sSource.size()-1; iNum++ )
for( unsigned iNum = 0; iNum < Source.size()-1; iNum++ ) { {
csTmp += Source[iNum]; sTmp += sSource[iNum];
csTmp += Deliminator; sTmp += sDeliminator;
} }
csTmp += Source.back(); sTmp += sSource.back();
return csTmp; return sTmp;
} }
RString join( const RString &Delimitor, vector<RString>::const_iterator begin, vector<RString>::const_iterator end ) RString join( const RString &sDelimitor, vector<RString>::const_iterator begin, vector<RString>::const_iterator end )
{ {
if( begin == end ) if( begin == end )
return RString(); return RString();
RString ret; RString sRet;
while( begin != end ) while( begin != end )
{ {
ret += *begin; sRet += *begin;
++begin; ++begin;
if( begin != end ) if( begin != end )
ret += Delimitor; sRet += sDelimitor;
} }
return ret; return sRet;
} }
template <class S> template <class S>
@@ -964,8 +964,6 @@ bool GetFileContents( const RString &sPath, RString &sOut, bool bOneLine )
return true; return true;
} }
#if 1
/* PCRE */
#include "pcre/pcre.h" #include "pcre/pcre.h"
void Regex::Compile() void Regex::Compile()
{ {
@@ -1059,87 +1057,6 @@ bool Regex::Compare(const RString &str, vector<RString> &matches)
return true; return true;
} }
#else
/* GNU regex */
#include "regex.h"
void Regex::Compile()
{
reg = new regex_t;
int ret = regcomp((regex_t *) reg, pattern.c_str(), REG_EXTENDED|REG_ICASE);
if(ret != 0)
RageException::Throw("Invalid regex: '%s'", pattern.c_str());
/* Count the number of backreferences. */
backrefs = 0;
for(int i = 0; i < int(pattern.size()); ++i)
if(pattern[i] == '(') backrefs++;
ASSERT(backrefs+1 < 128);
}
void Regex::Set(const RString &str)
{
Release();
pattern=str;
Compile();
}
void Regex::Release()
{
delete (regex_t *)reg;
reg = NULL;
pattern = "";
}
Regex::Regex(const RString &str)
{
reg = NULL;
Set(str);
}
Regex::Regex(const Regex &rhs)
{
reg = NULL;
Set(rhs.pattern);
}
Regex &Regex::operator=(const Regex &rhs)
{
if(this != &rhs) Set(rhs.pattern);
return *this;
}
Regex::~Regex()
{
Release();
}
bool Regex::Compare(const RString &str)
{
return regexec((regex_t *) reg, str.c_str(), 0, NULL, 0) != REG_NOMATCH;
}
bool Regex::Compare(const RString &str, vector<RString> &matches)
{
matches.clear();
regmatch_t mat[128];
int ret = regexec((regex_t *) reg, str.c_str(), 128, mat, 0);
if(ret == REG_NOMATCH)
return false;
for(unsigned i = 1; i < backrefs+1; ++i)
{
if(mat[i].rm_so == -1)
matches.push_back(""); /* no match */
else
matches.push_back(str.substr(mat[i].rm_so, mat[i].rm_eo - mat[i].rm_so));
}
return true;
}
#endif
// Arguments and behavior are the same are similar to // Arguments and behavior are the same are similar to
// http://us3.php.net/manual/en/function.preg-replace.php // http://us3.php.net/manual/en/function.preg-replace.php
@@ -1522,7 +1439,7 @@ void ReplaceEntityText( RString &sText, const map<char,RString> &m )
sText = sRet; sText = sRet;
} }
/* Replace &#nnnn; (decimal) &xnnnn; (hex) with corresponding UTF-8 characters. */ /* Replace &#nnnn; (decimal) and &xnnnn; (hex) with corresponding UTF-8 characters. */
void Replace_Unicode_Markers( RString &Text ) void Replace_Unicode_Markers( RString &Text )
{ {
unsigned start = 0; unsigned start = 0;
@@ -1808,58 +1725,58 @@ RString ToString( bool value )
// //
// Helper function for reading/writing scores // Helper function for reading/writing scores
// //
bool FileRead(RageFileBasic& f, RString& sOut) bool FileRead( RageFileBasic& f, RString& sOut )
{ {
if (f.AtEOF()) if( f.AtEOF() )
return false; return false;
if( f.GetLine(sOut) == -1 ) if( f.GetLine(sOut) == -1 )
return false; return false;
return true; return true;
} }
bool FileRead(RageFileBasic& f, int& iOut) bool FileRead( RageFileBasic& f, int& iOut )
{ {
RString s; RString s;
if (!FileRead(f, s)) if( !FileRead(f, s) )
return false; return false;
iOut = atoi(s); iOut = atoi(s);
return true; return true;
} }
bool FileRead(RageFileBasic& f, unsigned& uOut) bool FileRead( RageFileBasic& f, unsigned& uOut )
{ {
RString s; RString s;
if (!FileRead(f, s)) if( !FileRead(f, s) )
return false; return false;
uOut = atoi(s); uOut = atoi(s);
return true; return true;
} }
bool FileRead(RageFileBasic& f, float& fOut) bool FileRead( RageFileBasic& f, float& fOut )
{ {
RString s; RString s;
if (!FileRead(f, s)) if( !FileRead(f, s) )
return false; return false;
fOut = strtof( s, NULL ); fOut = strtof( s, NULL );
return true; return true;
} }
void FileWrite(RageFileBasic& f, const RString& sWrite) void FileWrite( RageFileBasic& f, const RString& sWrite )
{ {
f.PutLine( sWrite ); f.PutLine( sWrite );
} }
void FileWrite(RageFileBasic& f, int iWrite) void FileWrite( RageFileBasic& f, int iWrite )
{ {
f.PutLine( ssprintf("%d", iWrite) ); f.PutLine( ssprintf("%d", iWrite) );
} }
void FileWrite(RageFileBasic& f, size_t uWrite) void FileWrite( RageFileBasic& f, size_t uWrite )
{ {
f.PutLine( ssprintf("%i", (int)uWrite) ); f.PutLine( ssprintf("%i", (int)uWrite) );
} }
void FileWrite(RageFileBasic& f, float fWrite) void FileWrite( RageFileBasic& f, float fWrite )
{ {
f.PutLine( ssprintf("%f", fWrite) ); f.PutLine( ssprintf("%f", fWrite) );
} }