Skip to content

Commit

Permalink
Replace isalpha, toupper, etc. with ascii versions
Browse files Browse the repository at this point in the history
  • Loading branch information
mungre committed Jun 10, 2024
1 parent 1fb5ca4 commit 8ba405c
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 54 deletions.
13 changes: 7 additions & 6 deletions src/assemble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "objectcode.h"
#include "asmexception.h"
#include "sourcecode.h"
#include "stringutils.h"


using namespace std;
Expand Down Expand Up @@ -165,7 +166,7 @@ int LineParser::GetInstructionAndAdvanceColumn(bool requireDistinctOpcodes)
bool bMatch = true;
for ( unsigned int j = 0; j < len; j++ )
{
if ( token[ j ] != toupper( m_line[ m_column + j ] ) )
if ( token[ j ] != Ascii::ToUpper( m_line[ m_column + j ] ) )
{
bMatch = false;
break;
Expand Down Expand Up @@ -480,7 +481,7 @@ void LineParser::HandleAssembler( int instruction )

// see if it's accumulator mode

if ( m_column < m_line.length() && toupper( m_line[ m_column ] ) == 'A' && HasAddressingMode( instruction, ACC ) )
if ( m_column < m_line.length() && Ascii::ToUpper( m_line[ m_column ] ) == 'A' && HasAddressingMode( instruction, ACC ) )
{
// might be... but only if the next character is a separator or whitespace
// otherwise, we must assume a label beginning with A
Expand Down Expand Up @@ -591,7 +592,7 @@ void LineParser::HandleAssembler( int instruction )
throw AsmException_SyntaxError_BadIndirect( m_line, m_column );
}

if ( toupper( m_line[ m_column ] ) != 'Y' )
if ( Ascii::ToUpper( m_line[ m_column ] ) != 'Y' )
{
// We were expecting an Y
throw AsmException_SyntaxError_BadIndirect( m_line, m_column );
Expand Down Expand Up @@ -648,7 +649,7 @@ void LineParser::HandleAssembler( int instruction )
throw AsmException_SyntaxError_BadIndirect( m_line, m_column );
}

if ( toupper( m_line[ m_column ] ) != 'X' )
if ( Ascii::ToUpper( m_line[ m_column ] ) != 'X' )
{
// We were expecting an X
throw AsmException_SyntaxError_BadIndirect( m_line, m_column );
Expand Down Expand Up @@ -826,7 +827,7 @@ void LineParser::HandleAssembler( int instruction )
throw AsmException_SyntaxError_BadAbsolute( m_line, m_column );
}

if ( m_column < m_line.length() && toupper( m_line[ m_column ] ) == 'X' )
if ( m_column < m_line.length() && Ascii::ToUpper( m_line[ m_column ] ) == 'X' )
{
m_column++;

Expand Down Expand Up @@ -857,7 +858,7 @@ void LineParser::HandleAssembler( int instruction )
}
}

if ( m_column < m_line.length() && toupper( m_line[ m_column ] ) == 'Y' )
if ( m_column < m_line.length() && Ascii::ToUpper( m_line[ m_column ] ) == 'Y' )
{
m_column++;

Expand Down
10 changes: 5 additions & 5 deletions src/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ int LineParser::GetTokenAndAdvanceColumn()
bool bMatch = true;
for ( unsigned int j = 0; j < len; j++ )
{
if ( token[ j ] != toupper( m_line[ m_column + j ] ) )
if ( token[ j ] != Ascii::ToUpper( m_line[ m_column + j ] ) )
{
bMatch = false;
break;
Expand Down Expand Up @@ -493,7 +493,7 @@ void LineParser::HandleDefineLabel()
}
}

if ( ( m_column < m_line.length() ) && ( isalpha( m_line[ m_column ] ) || m_line[ m_column ] == '_' ) )
if ( ( m_column < m_line.length() ) && ( Ascii::IsAlpha( m_line[ m_column ] ) || m_line[ m_column ] == '_' ) )
{
// Symbol starts with a valid character

Expand Down Expand Up @@ -1252,7 +1252,7 @@ void LineParser::HandleFor()

// first look for the variable name

if ( !isalpha( m_line[ m_column ] ) && m_line[ m_column ] != '_' )
if ( !Ascii::IsAlpha( m_line[ m_column ] ) && m_line[ m_column ] != '_' )
{
throw AsmException_SyntaxError_InvalidSymbolName( m_line, m_column );
}
Expand Down Expand Up @@ -1677,7 +1677,7 @@ void LineParser::HandleMacro()

string macroName;

if ( isalpha( m_line[ m_column ] ) || m_line[ m_column ] == '_' )
if ( Ascii::IsAlpha( m_line[ m_column ] ) || m_line[ m_column ] == '_' )
{
macroName = GetSymbolName();

Expand Down Expand Up @@ -1713,7 +1713,7 @@ void LineParser::HandleMacro()
throw AsmException_SyntaxError_MissingComma( m_line, m_column );
}
}
else if ( isalpha( m_line[ m_column ] ) || m_line[ m_column ] == '_' )
else if ( Ascii::IsAlpha( m_line[ m_column ] ) || m_line[ m_column ] == '_' )
{
string param = GetSymbolName();

Expand Down
5 changes: 3 additions & 2 deletions src/discimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "discimage.h"
#include "asmexception.h"
#include "globaldata.h"
#include "stringutils.h"

using namespace std;

Expand Down Expand Up @@ -208,14 +209,14 @@ void DiscImage::AddFile( const char* pName, const unsigned char* pAddr, int load

for ( size_t j = 0; j < 7; j++ )
{
if ( toupper( pPaddedName[ j ] ) != toupper( m_aCatalog[ i + j ] ) )
if ( Ascii::ToUpper( pPaddedName[ j ] ) != Ascii::ToUpper( m_aCatalog[ i + j ] ) )
{
bTheSame = false;
break;
}
}

if ( bTheSame && ( toupper( m_aCatalog[ i + 7 ] & 0x7F ) ) == toupper( dirName ) )
if ( bTheSame && ( Ascii::ToUpper( m_aCatalog[ i + 7 ] & 0x7F ) ) == Ascii::ToUpper( dirName ) )
{
// File already exists
throw AsmException_FileError_FileExists( m_outputFilename );
Expand Down
6 changes: 3 additions & 3 deletions src/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ Value LineParser::GetValue()
}
value = String(text.data(), text.size());
}
else if ( m_column < m_line.length() && ( isalpha( m_line[ m_column ] ) || m_line[ m_column ] == '_' ) )
else if ( m_column < m_line.length() && ( Ascii::IsAlpha( m_line[ m_column ] ) || m_line[ m_column ] == '_' ) )
{
// get a symbol

Expand Down Expand Up @@ -284,7 +284,7 @@ Value LineParser::EvaluateExpression( bool bAllowOneMismatchedCloseBracket )
bool bMatch = true;
for ( unsigned int j = 0; j < len; j++ )
{
if ( ( m_column + j >= m_line.length() ) || ( token[ j ] != toupper( m_line[ m_column + j ] ) ) )
if ( ( m_column + j >= m_line.length() ) || ( token[ j ] != Ascii::ToUpper( m_line[ m_column + j ] ) ) )
{
bMatch = false;
break;
Expand Down Expand Up @@ -400,7 +400,7 @@ Value LineParser::EvaluateExpression( bool bAllowOneMismatchedCloseBracket )
bool bMatch = true;
for ( unsigned int j = 0; j < len; j++ )
{
if ( ( m_column + j >= m_line.length() ) || ( token[ j ] != toupper( m_line[ m_column + j ] ) ) )
if ( ( m_column + j >= m_line.length() ) || ( token[ j ] != Ascii::ToUpper( m_line[ m_column + j ] ) ) )
{
bMatch = false;
break;
Expand Down
14 changes: 7 additions & 7 deletions src/lineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ void LineParser::Process( string line )

bool bIsSymbolAssignment = false;

if ( isalpha( m_line[ m_column ] ) || m_line[ m_column ] == '_' )
if ( Ascii::IsAlpha( m_line[ m_column ] ) || m_line[ m_column ] == '_' )
{
do
{
m_column++;

} while ( m_column < m_line.length() &&
( isalpha( m_line[ m_column ] ) ||
isdigit( m_line[ m_column ] ) ||
( Ascii::IsAlpha( m_line[ m_column ] ) ||
Ascii::IsDigit( m_line[ m_column ] ) ||
m_line[ m_column ] == '_' ||
m_line[ m_column ] == '%' ||
m_line[ m_column ] == '$' ) &&
Expand Down Expand Up @@ -216,7 +216,7 @@ void LineParser::Process( string line )

// Check macro matches

if ( isalpha( m_line[ m_column ] ) || m_line[ m_column ] == '_' )
if ( Ascii::IsAlpha( m_line[ m_column ] ) || m_line[ m_column ] == '_' )
{
string macroName = GetSymbolName();
const Macro* macro = MacroTable::Instance().Get( macroName );
Expand Down Expand Up @@ -547,7 +547,7 @@ bool LineParser::AdvanceAndCheckEndOfSubStatement(bool includeComma)
/*************************************************************************************************/
string LineParser::GetSymbolName()
{
assert( isalpha( m_line[ m_column ] ) || m_line[ m_column ] == '_' );
assert( Ascii::IsAlpha( m_line[ m_column ] ) || m_line[ m_column ] == '_' );

string symbolName;

Expand All @@ -556,8 +556,8 @@ string LineParser::GetSymbolName()
symbolName += m_line[ m_column++ ];

} while ( m_column < m_line.length() &&
( isalpha( m_line[ m_column ] ) ||
isdigit( m_line[ m_column ] ) ||
( Ascii::IsAlpha( m_line[ m_column ] ) ||
Ascii::IsDigit( m_line[ m_column ] ) ||
m_line[ m_column ] == '_' ||
m_line[ m_column ] == '%' ||
m_line[ m_column ] == '$' ) &&
Expand Down
13 changes: 4 additions & 9 deletions src/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@

#include "asmexception.h"
#include "literals.h"

// Don't use isdigit because compilers can't agree on its locale dependence
static bool is_decimal_digit(char c)
{
return '0' <= c && c <= '9';
}
#include "stringutils.h"

static int hex_digit_value(char c)
{
Expand Down Expand Up @@ -119,7 +114,7 @@ static bool CopyDigitsSkippingUnderscores(const std::string& line, size_t& index
}

size_t start_index = index;
while ( index < line.length() && (is_decimal_digit(line[index]) || line[index] == '_') )
while ( index < line.length() && (Ascii::IsDigit(line[index]) || line[index] == '_') )
{
if (line[index] == '_')
{
Expand Down Expand Up @@ -165,7 +160,7 @@ bool Literals::ParseNumeric(const std::string& line, size_t& index, double& resu
return false;
}

if ( is_decimal_digit(line[index]) || line[index] == '.' || line[index] == '-' )
if ( Ascii::IsDigit(line[index]) || line[index] == '.' || line[index] == '-' )
{
// Copy the number without underscores to this buffer
std::string buffer;
Expand Down Expand Up @@ -198,7 +193,7 @@ bool Literals::ParseNumeric(const std::string& line, size_t& index, double& resu
// Copy exponent if it's followed by a sign or digit
if ( index + 1 < line.length() &&
( line[index] == 'e' || line[index] == 'E' ) &&
( line[index + 1] == '+' || line[index + 1] == '-' || is_decimal_digit(line[index + 1]) ) )
( line[index + 1] == '+' || line[index + 1] == '-' || Ascii::IsDigit(line[index + 1]) ) )
{
buffer.push_back('e');
index++;
Expand Down
43 changes: 43 additions & 0 deletions src/stringutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,47 @@ namespace StringUtils
}


// Built-in char functions like isdigit, toupper, etc. are locale dependent and quite slow.
struct Ascii
{
static bool IsAlpha(char c)
{
unsigned int uc = static_cast<unsigned char>(c);
return (uc | 0x20) - 'a' < 26;
}

static bool IsDigit(char c)
{
unsigned int uc = static_cast<unsigned char>(c);
return (uc - '0') < 10;
}

static char ToLower(char c)
{
unsigned int uc = static_cast<unsigned char>(c);
if (uc - 'A' < 26)
{
return c | 0x20;
}
else
{
return c;
}
}

static char ToUpper(char c)
{
unsigned int uc = static_cast<unsigned char>(c);
if (uc - 'a' < 26)
{
return c & 0xDF;
}
else
{
return c;
}
}
};


#endif // STRINGUTILS_H_
5 changes: 3 additions & 2 deletions src/symboltable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "constants.h"
#include "asmexception.h"
#include "literals.h"
#include "stringutils.h"


using namespace std;
Expand Down Expand Up @@ -189,8 +190,8 @@ bool SymbolTable::AddCommandLineSymbol( const std::string& expr )
}
for ( std::string::size_type i = 0; i < symbol.length(); ++i )
{
bool valid = ( isalpha( symbol[ i ] ) || ( symbol[ i ] == '_' ) );
valid = valid || ( ( i > 0 ) && isdigit( symbol[ i ] ) );
bool valid = ( Ascii::IsAlpha( symbol[ i ] ) || ( symbol[ i ] == '_' ) );
valid = valid || ( ( i > 0 ) && Ascii::IsDigit( symbol[ i ] ) );
if ( !valid )
{
return false;
Expand Down
24 changes: 4 additions & 20 deletions src/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <string.h>
#include <cstdlib>

#include "stringutils.h"

// A simple immutable string buffer with a length and a reference count.
// This doesn't have constructors, etc. so it can be stuffed into a union.
struct StringHeader
Expand Down Expand Up @@ -133,7 +135,7 @@ struct StringHeader
char* pdata = StringBuffer(result);
for (unsigned int i = 0; i != length; ++i)
{
*pdata = ascii_upper(*pdata);
*pdata = Ascii::ToUpper(*pdata);
++pdata;
}
}
Expand All @@ -149,7 +151,7 @@ struct StringHeader
char* pdata = StringBuffer(result);
for (unsigned int i = 0; i != length; ++i)
{
*pdata = ascii_lower(*pdata);
*pdata = Ascii::ToLower(*pdata);
++pdata;
}
}
Expand Down Expand Up @@ -191,24 +193,6 @@ struct StringHeader
return 1;
}

static char ascii_lower(char c)
{
if (('A' <= c) && (c <= 'Z'))
{
c += 'a' - 'A';
}
return c;
}

static char ascii_upper(char c)
{
if (('a' <= c) && (c <= 'z'))
{
c -= 'a' - 'A';
}
return c;
}

};

// A simple immutable string.
Expand Down

0 comments on commit 8ba405c

Please sign in to comment.