Skip to content

Commit

Permalink
Merge branch 'incomplete-initchars' into 'develop'
Browse files Browse the repository at this point in the history
Add code missing from charmaps::InitChars

See merge request devel/gdx!76
  • Loading branch information
0x17 committed Sep 19, 2024
2 parents bd94bf8 + cd58e05 commit 516d0f2
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 136 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ endif ()
if (UNIX)
# -fsanitize=undefined -fno-inline
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wreturn-type -Wmissing-declarations -Wno-unknown-pragmas")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wconversion -funsigned-char") # aggressive signage warnings
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DZ_HAVE_UNISTD_H")
if (NOT APPLE AND NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wformat-truncation=0")
Expand Down
72 changes: 61 additions & 11 deletions src/gdlib/charmaps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ char quotecharx {};

std::array<char, numCharVals> mapcharBuf;

constexpr std::array mapToSelf {
' ', // blank
'!', // exclamation mark !
'"', // double quote "
'#', // number sign #
'$', // dollar $
'%', // per-cent %
'&', // ampersand &
'(', // left paren (
')', // right paren )
'*', // asterisk *
'+', // plus +
',', // comma ,
'-', // minus -
'.', // period .
'/', // slash /
':', // colon :
',', // semi-colon ,
'<', // less than <
'=', // equals =
'>', // greater than >
'?', // question mark ?
'@', // at sign @
'\\',// backslash
'_', // underscore _
'\'',// single quote '
'[', // left sq bracket [
']', // right sq bracket ]
'^', // circumflex ^
'|', // vert bar |
'`', // accent grave `
'}', // right brace
'{', // left brace
'~', // tilde ~
};

void InitChars( const bool AllChars )
{
static_assert( mapcharBuf.size() == 256 );
Expand All @@ -54,32 +90,47 @@ void InitChars( const bool AllChars )
// everything not explicitly whitelisted afterward kills the compilation (or is it ignored?)?
for( unsigned char c = std::numeric_limits<unsigned char>::min(); c <= std::numeric_limits<unsigned char>::max(); c++ )
mapcharBuf[c] = std::char_traits<char>::eof();

for(char c {'A'}; c <= 'Z'; c++)
if(letter[c])
mapcharBuf[c] = c;

for(char c {'a'}; c <= 'z'; c++)
if(letter[c])
mapcharBuf[c] = c;

for(char c {'0'}; c <= '9'; c++)
if(digit[c])
mapcharBuf[c] = c;

for( const char c : mapToSelf )
mapcharBuf[c] = c;
}

void InitCharacterMaps()
{
// set other characeter arrays useful in compiler and execution
// set other character arrays useful in compiler and execution
// make sets empty first
const utils::charset Empty;
capletter = textquote = digit = lowletter = letter = identchar = Empty;
utils::charRangeInsert( digit, '0', '9' );
charRangeInsert( digit, '0', '9' );

utils::charRangeInsert( letter, 'A', 'Z' );
utils::charRangeInsert( capletter, 'A', 'Z' );
charRangeInsert( letter, 'A', 'Z' );
charRangeInsert( capletter, 'A', 'Z' );

utils::charRangeInsert( letter, 'a', 'z' );
utils::charRangeInsert( lowletter, 'a', 'z' );
charRangeInsert( letter, 'a', 'z' );
charRangeInsert( lowletter, 'a', 'z' );

identchar = utils::unionOp( letter, digit );
identchar = unionOp( letter, digit );
alphanum = identchar;
// set ident character array
identchar.insert( '_' );

// set label character array - unquoted characters
labelchar = identchar;

utils::insertAllChars( labelchar, "+-" );
utils::insertAllChars( textquote, "\"\'" );
insertAllChars( labelchar, "+-" );
insertAllChars( textquote, "\"\'" );
}

// Brief:
Expand All @@ -97,9 +148,8 @@ char DetermineQuote( const char *s )

for( int i {}; s[i] != '\0'; i++ )
{
char sk = s[i];
// don't need quotes
if( !utils::in( sk, labelchar ) )
if( const char sk = s[i]; !utils::in( sk, labelchar ) )
{
quoted = true;
if( sk == '\'' )
Expand Down
20 changes: 8 additions & 12 deletions src/gdlib/charmaps.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@
#pragma once

#include <string>
#include <unordered_map>
#include <set>
#include <array>
#include <limits>
#include <cassert>

#include "utils.h"

Expand All @@ -41,7 +38,7 @@
namespace gdlib::charmaps
{

const char charff = '\f',
constexpr char charff = '\f',
charcr = '\r',
chareof = std::char_traits<char>::eof(),
chareol = '\0',
Expand All @@ -64,36 +61,35 @@ extern char quotecharx;
constexpr int numCharVals {std::numeric_limits<unsigned char>::max()+1};
extern std::array<char, numCharVals> mapcharBuf;

inline char mapchar(char c) {
assert( (unsigned char) c >= 0 && (unsigned char) c <= 255 );
return mapcharBuf[(unsigned char)c];
inline char mapchar( const char c) {
return mapcharBuf[static_cast<unsigned char>( c )];
}

void InitChars( bool AllChars );
void InitCharacterMaps();
char DetermineQuote( const char *s );

inline bool IsLetter( char c )
inline bool IsLetter( const char c )
{
return ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' );
}

inline bool IsDigit( char c )
inline bool IsDigit( const char c )
{
return c >= '0' && c <= '9';
}

inline bool IsLabelChar(char c)
inline bool IsLabelChar( const char c )
{
return IsLetter( c ) || IsDigit(c) || c == '_' || c == '+' || c == '-';
}

inline bool IsIdentChar(char c)
inline bool IsIdentChar(const char c)
{
return IsLetter( c ) || IsDigit(c) || c == '_';
}

inline bool IsTextQuote(char c)
inline bool IsTextQuote(const char c)
{
return c == '\'' || c == '\"';
}
Expand Down
2 changes: 1 addition & 1 deletion src/gdlib/glookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ class TBucketArray

[[nodiscard]] int MemoryUsed() const
{
return FBaseCap * sizeof( T * ) + FBaseCnt * FBucketSize;
return FBaseCap * static_cast<int>(sizeof( T * )) + FBaseCnt * FBucketSize;
}

void DeleteAtEnd( int Cnt )
Expand Down
25 changes: 13 additions & 12 deletions src/gdlib/gmsstrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

using namespace std::literals::string_literals;
using namespace rtl::p3utils;
using utils::ui8, utils::ui16, utils::ui32;

#if defined(__IN_CPPMEX__)
#include "../gdlib/statlib.h"
Expand Down Expand Up @@ -588,19 +589,19 @@ bool TBufferedFileStream::FillBuffer()
}
else
{
if( const uint16_t RLen = TXFileStream::Read( &CBufPtr->cxHeader, sizeof( TCompressHeader ) );
if( const auto RLen = ui16(TXFileStream::Read( &CBufPtr->cxHeader, sizeof( TCompressHeader ) ));
RLen < sizeof( TCompressHeader ) )
NrLoaded = 0;
else
{
const uint16_t WLen = ( CBufPtr->cxHeader.cxB1 << 8 ) + CBufPtr->cxHeader.cxB2;
const auto WLen = ui16( ( CBufPtr->cxHeader.cxB1 << 8 ) + CBufPtr->cxHeader.cxB2 );
if( !CBufPtr->cxHeader.cxTyp ) NrLoaded = TXFileStream::Read( BufPtr.data(), WLen );
else
{
TXFileStream::Read( &CBufPtr->cxData, WLen );
unsigned long XLen = BufSize;// we need a var parameter
uncompress( BufPtr.data(), &XLen, &CBufPtr->cxData, WLen );
NrLoaded = XLen;
NrLoaded = ui32(XLen);
}
}
}
Expand Down Expand Up @@ -675,22 +676,22 @@ bool TBufferedFileStream::FlushBuffer()
}
else
{
unsigned long Len = CBufSize - sizeof( TCompressHeader );
unsigned long Len { CBufSize - sizeof( TCompressHeader ) };
compress( &CBufPtr->cxData, &Len, BufPtr.data(), NrWritten );
if( Len < NrWritten )
{
CBufPtr->cxHeader.cxTyp = 1;// indicates compressed
CBufPtr->cxHeader.cxB1 = static_cast<uint8_t>( Len >> 8 );
CBufPtr->cxHeader.cxB2 = Len & 0xFF;
Len += sizeof( TCompressHeader );
ActWritten = TXFileStream::Write( &CBufPtr->cxHeader.cxTyp, Len );
ActWritten = TXFileStream::Write( &CBufPtr->cxHeader.cxTyp, ui32(Len) );
res = Len == ActWritten;
}
else
{
CBufPtr->cxHeader.cxTyp = 0;// indicates no compression
CBufPtr->cxHeader.cxB1 = NrWritten >> 8;
CBufPtr->cxHeader.cxB2 = NrWritten & 0xFF;
CBufPtr->cxHeader.cxB1 = ui8(NrWritten >> 8);
CBufPtr->cxHeader.cxB2 = ui8(NrWritten & 0xFF);
TXFileStream::Write( &CBufPtr->cxHeader.cxTyp, sizeof( TCompressHeader ) );
ActWritten = TXFileStream::Write( BufPtr.data(), NrWritten );
res = NrWritten == ActWritten;
Expand Down Expand Up @@ -881,10 +882,10 @@ void TMiBufferedStream::WriteGmsInteger( int N )
std::array<uint8_t, 5> W {};
while( N )
{
W[++C] = N & 255;
W[++C] = ui8(N & 255);
N >>= 8;
}
W[0] = B | C << 4;
W[0] = ui8(B | C << 4);
Write( W.data(), C + 1 );
}

Expand Down Expand Up @@ -936,7 +937,7 @@ void TMiBufferedStream::WriteGmsDouble( double D )
if( gv == xvacr ) WriteGmsInteger( utils::round<int>( D / GMS_SV_ACR ) );
return;
}
int C {};
uint8_t C {};
TDoubleVar Z {};
Z.V = D;
if( NormalOrder )
Expand All @@ -949,7 +950,7 @@ void TMiBufferedStream::WriteGmsDouble( double D )
}
B = 128 | C;
Write( &B, 1 );
assert( C >= 0 && C <= 7 );
assert( /*C >= 0 &&*/ C <= 7 );
Write( &Z.VA[C], static_cast<uint32_t>( Z.VA.size() ) - C );
}
else
Expand All @@ -971,7 +972,7 @@ int TMiBufferedStream::ReadGmsInteger()
{
uint8_t B;
#if !defined( NDEBUG )
auto numBytesRead { Read( &B, 1 ) };
const auto numBytesRead { Read( &B, 1 ) };
assert( numBytesRead == 1 );
// should not happen
if( !numBytesRead )
Expand Down
2 changes: 1 addition & 1 deletion src/gdlib/obfuscatestr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void obfuscateInit()
const int fcharmapLen = 254 - 32 - ( 'z' - 'a' + 1 ) - 1, ffcharmapLen = fcharmapLen - 1;
LabelCharMapPtr = std::make_unique<TObfuscateCharMap>( fcharmapLen, ffcharmapLen );
int k {};
for( int i { 33 }; i <= 254; i++ )
for( uint8_t i { 33 }; i <= 254; i++ )
{
if( i != 127 && !( ( i >= 'a' && i <= 'z' ) || i == '\'' ) )
{
Expand Down
18 changes: 10 additions & 8 deletions src/gdlib/strutilx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ using namespace std::literals::string_literals;
using namespace rtl::sysutils_p3;
using namespace rtl::p3platform;

using utils::ui8;

// ==============================================================================================================
// Implementation
// ==============================================================================================================
namespace gdlib::strutilx
{

const std::string MAXINT_S = "maxint"s, MININT_S = "minint"s;
const std::string MAXDOUBLE_S = "maxdouble"s, EPSDOUBLE_S = "eps", MINDOUBLE_S = "mindouble";
const auto MAXINT_S = "maxint"s, MININT_S = "minint"s;
const auto MAXDOUBLE_S = "maxdouble"s, EPSDOUBLE_S = "eps"s, MINDOUBLE_S = "mindouble"s;

std::string UpperCase( const std::string_view s )
{
Expand Down Expand Up @@ -287,7 +289,7 @@ static uint8_t DblToStrSepCore(double V, const char DecimalSep, char *s)
break;
}
}
return slen;
return ui8(slen);
}

// Closer port of corresponding Delphi function (faster?)
Expand Down Expand Up @@ -331,7 +333,7 @@ uint8_t DblToStrSep(double V, const char DecimalSep, char* sout)
}
sout[i] = sout[l];
}
return i - 1;
return ui8(i - 1);
}

std::string DblToStr( const double V )
Expand Down Expand Up @@ -724,8 +726,8 @@ int StrUCmp( const std::string_view S1, const std::string_view S2 )
if( L > S2.length() ) L = S2.length();
for( int K {}; K < static_cast<int>( L ); K++ )
{
const int d = utils::toupper( S1[K] ) - utils::toupper( S2[K] );
if( d ) return d;
if( const int d = utils::toupper( S1[K] ) - utils::toupper( S2[K] ) )
return d;
}
return static_cast<int>( S1.length() - S2.length() );
}
Expand All @@ -749,8 +751,8 @@ int StrUCmp( const DelphiStrRef &S1, const DelphiStrRef &S2 )
if( L > S2.length ) L = S2.length;
for( int K {}; K < L; K++ )
{
const int d = utils::toupper( S1.chars[K] ) - utils::toupper( S2.chars[K] );
if( d ) return d;
if( const int d = utils::toupper( S1.chars[K] ) - utils::toupper( S2.chars[K] ) )
return d;
}
return S1.length - S2.length;
}
Expand Down
Loading

0 comments on commit 516d0f2

Please sign in to comment.