Skip to content

Commit

Permalink
More explicit shrinking from bigger to smaller integral types
Browse files Browse the repository at this point in the history
  • Loading branch information
0x17 committed Sep 19, 2024
1 parent b602e77 commit cd58e05
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 45 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +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_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
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
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
24 changes: 21 additions & 3 deletions src/gdlib/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -738,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<unsigned char>( c );
}
Expand All @@ -757,14 +757,32 @@ std::string IntToStrW( int n, int w, char blankChar = ' ' );
void trimLeft( std::string &s );

template<int N, int firstValid=0>
inline int indexOfSameText(const std::array<std::string, N> &strs, const std::string &s) {
int indexOfSameText(const std::array<std::string, N> &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<typename T>
auto ui8(const T x)
{
return static_cast<uint8_t>( x );
}

template<typename T>
auto ui16(const T x)
{
return static_cast<uint16_t>( x );
}

template<typename T>
auto ui32(const T x)
{
return static_cast<uint32_t>( x );
}

}// namespace utils
3 changes: 2 additions & 1 deletion src/gxfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,8 @@ 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
Expand Down
15 changes: 7 additions & 8 deletions src/rtl/p3utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
using namespace rtl::sysutils_p3;
using namespace rtl::p3platform;
using namespace std::literals::string_literals;
using utils::ui32;

// ==============================================================================================================
// Implementation
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -1107,7 +1106,7 @@ int xGetExecName( std::string &execName, std::string &msg )
}
else
{
ssz = std::min<int>( execBuf.size() - 1, ssz );
ssz = std::min<decltype(ssz)>( execBuf.size() - 1, ssz );
rc = 0;
}
#elif defined( _WIN32 )
Expand Down
29 changes: 17 additions & 12 deletions src/rtl/sysutils_p3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
#include <cstring> // 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 <Windows.h>
#include <io.h>
Expand All @@ -44,6 +47,8 @@
using namespace rtl::p3platform;
using namespace std::literals::string_literals;

using utils::ui16;

// ==============================================================================================================
// Implementation
// ==============================================================================================================
Expand Down Expand Up @@ -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<uint16_t>(res.quot);
Remainder = static_cast<uint16_t>(res.rem);
}

double EncodeDate( uint16_t Year, uint16_t Month, const uint16_t Day )
Expand Down Expand Up @@ -286,7 +291,7 @@ std::string IncludeTrailingPathDelimiter( const std::string &S )
return S + PathDelim;
}

const std::array<int, 12>
const std::array<uint8_t, 12>
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 };

Expand Down Expand Up @@ -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<int, 12> &daysPerMonth = isLeapYear( year ) ? daysPerMonthLeapYear : daysPerMonthRegularYear;
if( const std::array<uint8_t, 12> &daysPerMonth = isLeapYear( year ) ? daysPerMonthLeapYear : daysPerMonthRegularYear;
year >= 1 && year <= 9999 && month >= 1 && month <= 12 && day >= 1 && day <= daysPerMonth[month - 1] )
{
const int stop = month - 1;
Expand Down Expand Up @@ -535,8 +540,8 @@ double Now()
if(gettimeofday(&tv, nullptr) || !localtime_r(&tv.tv_sec, &lt))
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
}
Expand All @@ -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 )
Expand All @@ -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 )
{
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit cd58e05

Please sign in to comment.