diff --git a/CMakeLists.txt b/CMakeLists.txt index 3fbbd0f..e6858e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") diff --git a/src/gdlib/charmaps.cpp b/src/gdlib/charmaps.cpp index 35e2208..36adcd5 100644 --- a/src/gdlib/charmaps.cpp +++ b/src/gdlib/charmaps.cpp @@ -40,6 +40,42 @@ char quotecharx {}; std::array 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 ); @@ -54,23 +90,38 @@ void InitChars( const bool AllChars ) // everything not explicitly whitelisted afterward kills the compilation (or is it ignored?)? for( unsigned char c = std::numeric_limits::min(); c <= std::numeric_limits::max(); c++ ) mapcharBuf[c] = std::char_traits::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( '_' ); @@ -78,8 +129,8 @@ void InitCharacterMaps() // set label character array - unquoted characters labelchar = identchar; - utils::insertAllChars( labelchar, "+-" ); - utils::insertAllChars( textquote, "\"\'" ); + insertAllChars( labelchar, "+-" ); + insertAllChars( textquote, "\"\'" ); } // Brief: @@ -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 == '\'' ) diff --git a/src/gdlib/charmaps.h b/src/gdlib/charmaps.h index 68c3092..98c64cc 100644 --- a/src/gdlib/charmaps.h +++ b/src/gdlib/charmaps.h @@ -26,11 +26,8 @@ #pragma once #include -#include -#include #include #include -#include #include "utils.h" @@ -41,7 +38,7 @@ namespace gdlib::charmaps { -const char charff = '\f', +constexpr char charff = '\f', charcr = '\r', chareof = std::char_traits::eof(), chareol = '\0', @@ -64,36 +61,35 @@ extern char quotecharx; constexpr int numCharVals {std::numeric_limits::max()+1}; extern std::array 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( 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 == '\"'; } diff --git a/src/gdlib/glookup.h b/src/gdlib/glookup.h index 0fd34db..818b471 100644 --- a/src/gdlib/glookup.h +++ b/src/gdlib/glookup.h @@ -429,7 +429,7 @@ class TBucketArray [[nodiscard]] int MemoryUsed() const { - return FBaseCap * sizeof( T * ) + FBaseCnt * FBucketSize; + return FBaseCap * static_cast(sizeof( T * )) + FBaseCnt * FBucketSize; } void DeleteAtEnd( int Cnt ) diff --git a/src/gdlib/gmsstrm.cpp b/src/gdlib/gmsstrm.cpp index 106afda..1c92417 100644 --- a/src/gdlib/gmsstrm.cpp +++ b/src/gdlib/gmsstrm.cpp @@ -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" @@ -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); } } } @@ -675,7 +676,7 @@ 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 ) { @@ -683,14 +684,14 @@ bool TBufferedFileStream::FlushBuffer() CBufPtr->cxHeader.cxB1 = static_cast( 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; @@ -881,10 +882,10 @@ void TMiBufferedStream::WriteGmsInteger( int N ) std::array 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 ); } @@ -936,7 +937,7 @@ void TMiBufferedStream::WriteGmsDouble( double D ) if( gv == xvacr ) WriteGmsInteger( utils::round( D / GMS_SV_ACR ) ); return; } - int C {}; + uint8_t C {}; TDoubleVar Z {}; Z.V = D; if( NormalOrder ) @@ -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( Z.VA.size() ) - C ); } else @@ -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 ) diff --git a/src/gdlib/obfuscatestr.cpp b/src/gdlib/obfuscatestr.cpp index be9bc54..2e1a27d 100644 --- a/src/gdlib/obfuscatestr.cpp +++ b/src/gdlib/obfuscatestr.cpp @@ -121,7 +121,7 @@ void obfuscateInit() const int fcharmapLen = 254 - 32 - ( 'z' - 'a' + 1 ) - 1, ffcharmapLen = fcharmapLen - 1; LabelCharMapPtr = std::make_unique( 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 == '\'' ) ) { diff --git a/src/gdlib/strutilx.cpp b/src/gdlib/strutilx.cpp index 0f0a128..02e7948 100644 --- a/src/gdlib/strutilx.cpp +++ b/src/gdlib/strutilx.cpp @@ -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 ) { @@ -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?) @@ -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 ) @@ -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( 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( S1.length() - S2.length() ); } @@ -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; } diff --git a/src/gdlib/utils.h b/src/gdlib/utils.h index 2a432e2..c0c1de1 100644 --- a/src/gdlib/utils.h +++ b/src/gdlib/utils.h @@ -178,7 +178,11 @@ class charset { chars.set(i); } - [[nodiscard]] bool contains(char c) const { + [[nodiscard]] bool contains(const char c) const { + return chars[c+offset]; + } + + [[nodiscard]] bool operator[](const char c) const { return chars[c+offset]; } @@ -734,7 +738,7 @@ inline char *NewString( const char *s, int64_t queryPeakRSS(); -inline int ord( const char c ) +inline auto ord( const char c ) { return static_cast( c ); } @@ -753,14 +757,32 @@ std::string IntToStrW( int n, int w, char blankChar = ' ' ); void trimLeft( std::string &s ); template -inline int indexOfSameText(const std::array &strs, const std::string &s) { +int indexOfSameText(const std::array &strs, const std::string &s) { int i{firstValid}; for(const std::string &s2 : strs) { if(sameText(s, s2)) return i; - i++; + ++i; } return firstValid-1; } +template +auto ui8(const T x) +{ + return static_cast( x ); +} + +template +auto ui16(const T x) +{ + return static_cast( x ); +} + +template +auto ui32(const T x) +{ + return static_cast( x ); +} + }// namespace utils diff --git a/src/gxfile.cpp b/src/gxfile.cpp index 8f3a94a..cef4a33 100644 --- a/src/gxfile.cpp +++ b/src/gxfile.cpp @@ -1296,25 +1296,29 @@ bool TGXFileObj::DoWrite( const int *AElements, const double *AVals ) } if( FDim == FCurrentDim && delta <= DeltaForWrite ) {// small change in last dimension - FFile->WriteByte( FCurrentDim + delta ); + assert(FCurrentDim >= 0 && FCurrentDim <= 255); + FFile->WriteByte( utils::ui8(FCurrentDim + delta) ); LastElem[FCurrentDim - 1] = AElements[FCurrentDim - 1]; } else {// general change - FFile->WriteByte( FDim ); + assert(FDim >= 0 && FDim <= 255); + FFile->WriteByte( static_cast(FDim) ); for( int D { FDim - 1 }; D < FCurrentDim; D++ ) { - int v { AElements[D] - MinElem[D] }; + const int v { AElements[D] - MinElem[D] }; switch( ElemType[D] ) { case TgdxElemSize::sz_integer: FFile->WriteInteger( v ); break; case TgdxElemSize::sz_word: - FFile->WriteWord( v ); + assert(v >= 0 && v <= 65535); + FFile->WriteWord( static_cast(v) ); break; case TgdxElemSize::sz_byte: - FFile->WriteByte( v ); + assert(v >= 0 && v <= 255); + FFile->WriteByte( static_cast(v) ); break; } LastElem[D] = AElements[D]; @@ -1325,10 +1329,10 @@ bool TGXFileObj::DoWrite( const int *AElements, const double *AVals ) { for( int DV {}; DV <= LastDataField; DV++ ) { - double X { AVals[DV] }; + const double X { AVals[DV] }; int64_t i64; - TDblClass dClass { dblInfo( X, i64 ) }; - int xv { vm_valund }; + const TDblClass dClass { dblInfo( X, i64 ) }; + uint8_t xv { vm_valund }; for( ; xv < vm_normal; xv++ ) if( i64 == intlValueMapI64[xv] ) break; if( xv == vm_normal ) diff --git a/src/rtl/p3io.cpp b/src/rtl/p3io.cpp index 3baa13c..b859230 100644 --- a/src/rtl/p3io.cpp +++ b/src/rtl/p3io.cpp @@ -251,85 +251,81 @@ void P3_Val_i(const char *s, int *i, int *code) * where d is a decimal digit unless preceded by the 0x or $, * in which case it is a hex digit */ -void P3_Val_i(const char *s, size_t slen, int *i, int *code) +void P3_Val_i( const char *s, size_t slen, int *i, int *code ) { std::array buffer; - char *end, *s2, *sd; - long int li; - int sign = 1; + std::memcpy( buffer.data(), s, sizeof( char ) * ( slen + 1 ) ); - std::memcpy(buffer.data(), s, sizeof(char)*(slen+1)); - - /* skip over blanks - * - Kylix 3 does not treat any other chars as whitespace - */ - for (s2 = (char *)buffer.data(); ' ' == *s2; s2++); - if ('+' == *s2) { - sd = s2+1; - } - else if ('-' == *s2) { + // skip over blanks + // - Kylix 3 does not treat any other chars as whitespace + char *s2, *sd; + for( s2 = buffer.data(); ' ' == *s2; s2++ ) {} + int sign { 1 }; + if( '+' == *s2 ) + sd = s2 + 1; + else if( '-' == *s2 ) + { sign = -1; - sd = s2+1; + sd = s2 + 1; } else sd = s2; - /* first check for the usual case - decimal digits */ - if (((*sd > '0') && (*sd <= '9')) || - (('0' == *sd) && ('\0' == sd[1] || (sd[1] >= '0' && sd[1] <= '9') )) ) { - li = strtol((char *)s2, (char **)&end, 10); - *i = li; - if ('\0' == *end) { /* reached the end, things went OK */ - *code = 0; - } - else - *code = (int)(end - (char *)buffer.data() + 1); + // first check for the usual case - decimal digits + if( (*sd > '0' && *sd <= '9') || ('0' == *sd && ( '\0' == sd[1] || (sd[1] >= '0' && sd[1] <= '9') )) ) + { + char *end; + const auto li = strtol( s2, &end, 10 ); + *i = static_cast(li); + // reached the end, things went OK + *code = '\0' == *end ? 0 : static_cast( end - buffer.data() + 1 ); return; } - /* if not a decimal string, - * must be either $ffff or 0xffff or an error */ - if ('$' == *sd) { - if ((sd[1] >= '0' && sd[1] <= '9') || (sd[1] >= 'A' && sd[1] <= 'F')) { // isxdigit - if (-1 == sign) + // if not a decimal string, + // must be either $ffff or 0xffff or an error + if( '$' == *sd ) + { + if( ( sd[1] >= '0' && sd[1] <= '9' ) || ( sd[1] >= 'A' && sd[1] <= 'F' ) ) + { + // isxdigit + if( -1 == sign ) *sd = '-'; else sd++; - li = strtol((char *)sd, (char **)&end, 16); - *i = li; - if ('\0' == *end) { /* reached the end, things went OK */ - *code = 0; - } - else - *code = (int)(end - (char *)buffer.data() + 1); + char *end; + const auto li = strtol( sd, &end, 16 ); + *i = static_cast(li); + // reached the end, things went OK + *code = '\0' == *end ? 0 : static_cast( end - buffer.data() + 1 ); } - else { + else + { *i = 0; sd++; - *code = (int)(sd - (char *)buffer.data() + 1); + *code = static_cast( sd - buffer.data() + 1 ); } return; } - else if (('0' == *sd) && - (('x' == sd[1]) || ('X' == sd[1])) - ) { - li = strtol((char *)s2, (char **)&end, 16); - *i = li; - if ('\0' == *end) { /* reached the end, things went OK */ + if( '0' == *sd && ( 'x' == sd[1] || 'X' == sd[1] ) ) + { + char *end; + const auto li = strtol( s2, &end, 16 ); + *i = static_cast(li); + // reached the end, things went OK + if( '\0' == *end ) *code = 0; - } - else { - /* we alread read the 0x, that is not an error for val */ - if (end < sd+2) - end = sd+2; - *code = (int)(end - (char *)buffer.data() + 1); + else + { + // we already read the 0x, that is not an error for val + if( end < sd + 2 ) + end = sd + 2; + *code = static_cast( end - buffer.data() + 1 ); } return; } - else { - *i = 0; - *code = (int)(sd - (char *)buffer.data() + 1); - } + *i = 0; + *code = static_cast( sd - buffer.data() + 1 ); } //================================================================================= diff --git a/src/rtl/p3utils.cpp b/src/rtl/p3utils.cpp index 590aa35..89c7c0f 100644 --- a/src/rtl/p3utils.cpp +++ b/src/rtl/p3utils.cpp @@ -81,6 +81,7 @@ using namespace rtl::sysutils_p3; using namespace rtl::p3platform; using namespace std::literals::string_literals; +using utils::ui32; // ============================================================================================================== // Implementation @@ -162,7 +163,7 @@ uint32_t P3GetEnvPC( const std::string &name, char *buf, uint32_t bufSize ) #else const char *p = getenv( name.c_str() ); if( !p ) return 0;// no match in the env - const auto psiz = strlen( p ) + 1; + const auto psiz = utils::ui32(strlen( p ) + 1); if( psiz <= bufSize ) { // it fits: copy it over @@ -491,13 +492,12 @@ int p3FileRead( Tp3FileHandle h, char *buffer, uint32_t buflen, uint32_t &numRea res = EIO; } #else - auto rc = read( h, buffer, buflen ); - if( rc < 0 ) + if( const auto rc = read( h, buffer, buflen ); rc < 0 ) { res = errno; numRead = 0; } - else numRead = rc; + else numRead = ui32(rc); #endif return res; } @@ -514,13 +514,12 @@ int p3FileWrite( Tp3FileHandle h, const char *buffer, uint32_t buflen, uint32_t res = EIO; } #else - auto rc = write(h, buffer, buflen); - if (rc < 0) { + if ( const auto rc = write( h, buffer, buflen ); rc < 0) { res = errno; numWritten = 0; } else - numWritten = rc; + numWritten = ui32(rc); #endif return res; } @@ -1107,7 +1106,7 @@ int xGetExecName( std::string &execName, std::string &msg ) } else { - ssz = std::min( execBuf.size() - 1, ssz ); + ssz = std::min( execBuf.size() - 1, ssz ); rc = 0; } #elif defined( _WIN32 ) diff --git a/src/rtl/sysutils_p3.cpp b/src/rtl/sysutils_p3.cpp index d67d366..9053445 100644 --- a/src/rtl/sysutils_p3.cpp +++ b/src/rtl/sysutils_p3.cpp @@ -27,9 +27,12 @@ #include // for strerror, size_t, strcmp, strcpy #include "sysutils_p3.h" -#include "p3platform.h" // for OSFileType, tOSFileType +#include "p3platform.h"// for OSFileType, tOSFileType + #include "global/unit.h" // for UNIT_INIT_FINI +#include "gdlib/utils.h" // for ui16 + #if defined( _WIN32 ) #include #include @@ -44,6 +47,8 @@ using namespace rtl::p3platform; using namespace std::literals::string_literals; +using utils::ui16; + // ============================================================================================================== // Implementation // ============================================================================================================== @@ -112,8 +117,8 @@ void DecodeTime( const global::delphitypes::tDateTime DateTime, uint16_t &Hour, void DivMod( const int Dividend, const uint16_t Divisor, uint16_t &Result, uint16_t &Remainder ) { const auto res = div( Dividend, Divisor ); - Result = res.quot; - Remainder = res.rem; + Result = static_cast(res.quot); + Remainder = static_cast(res.rem); } double EncodeDate( uint16_t Year, uint16_t Month, const uint16_t Day ) @@ -286,7 +291,7 @@ std::string IncludeTrailingPathDelimiter( const std::string &S ) return S + PathDelim; } -const std::array +const std::array daysPerMonthRegularYear = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, daysPerMonthLeapYear = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; @@ -490,7 +495,7 @@ void FindClose( TSearchRec &F ) bool tryEncodeDate( const uint16_t year, const uint16_t month, uint16_t day, double &date ) { - if( const std::array &daysPerMonth = isLeapYear( year ) ? daysPerMonthLeapYear : daysPerMonthRegularYear; + if( const std::array &daysPerMonth = isLeapYear( year ) ? daysPerMonthLeapYear : daysPerMonthRegularYear; year >= 1 && year <= 9999 && month >= 1 && month <= 12 && day >= 1 && day <= daysPerMonth[month - 1] ) { const int stop = month - 1; @@ -535,8 +540,8 @@ double Now() if(gettimeofday(&tv, nullptr) || !localtime_r(&tv.tv_sec, <)) return 0.0; double dnow, tnow; - const bool rc1 = tryEncodeDate( lt.tm_year + 1900, lt.tm_mon + 1, lt.tm_mday, dnow ); - const bool rc2 = tryEncodeTime (lt.tm_hour, lt.tm_min, lt.tm_sec, tv.tv_usec/1000, tnow); + const bool rc1 = tryEncodeDate( ui16(lt.tm_year + 1900), ui16(lt.tm_mon + 1), ui16(lt.tm_mday), dnow ); + const bool rc2 = tryEncodeTime (ui16(lt.tm_hour), ui16(lt.tm_min), ui16(lt.tm_sec), ui16(tv.tv_usec/1000), tnow); return rc1 && rc2 ? dnow + tnow : 0.0; #endif } @@ -560,7 +565,7 @@ static bool DecodeDateFully(const double DateTime, uint16_t &Year, uint16_t &Mon Year = Month = Day = DOW = 0; return false; } - DOW = T % 7 + 1; + DOW = ui16(T % 7 + 1); T--; uint16_t Y = 1; while( T >= D400 ) @@ -575,9 +580,9 @@ static bool DecodeDateFully(const double DateTime, uint16_t &Year, uint16_t &Mon I--; D += D100; } - Y += I * 100; + Y += ui16(I * 100); DivMod( D, D4, I, D ); - Y += I * 4; + Y += ui16(I * 4); DivMod( D, D1, I, D ); if( I == 4 ) { @@ -700,8 +705,8 @@ double FileDateToDateTime( int fd ) time_t tim; tim = fd; localtime_r( &tim, &ut ); - return EncodeDate( ut.tm_year + 1900, ut.tm_mon + 1, ut.tm_mday ) + - EncodeTime( ut.tm_hour, ut.tm_min, ut.tm_sec, 0 ); + return EncodeDate( ui16(ut.tm_year + 1900), ui16(ut.tm_mon + 1), ui16(ut.tm_mday) ) + + EncodeTime( ui16(ut.tm_hour), ui16(ut.tm_min), ui16(ut.tm_sec), 0 ); #endif } diff --git a/src/tests/gdlib/glookuptests.cpp b/src/tests/gdlib/glookuptests.cpp index 386be0a..eaa1cf0 100644 --- a/src/tests/gdlib/glookuptests.cpp +++ b/src/tests/gdlib/glookuptests.cpp @@ -57,12 +57,13 @@ struct Item { } }; -class MyMapping : public TGAMSRecList +class MyMapping final : public TGAMSRecList { - std::pair AccessRecord( Item *prec ) override + std::pair AccessRecord( Item *prec ) override { - gdlib::strutilx::DelphiStrRef dsr {}; - dsr.length = static_cast(prec->s.length()); + DelphiStrRef dsr {}; + assert(prec->s.length() <= 255); + dsr.length = static_cast(prec->s.length()); dsr.chars = const_cast( prec->s.c_str() ); return { dsr, &prec->nextBucketIx }; }