Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
added 651 characters in body
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97

Increase productivity, use an autoformatter.

OP intentionally keeps trailing whitespace. Editing and review work increases because of it.

Not cleaned out trailing spaces, they are - debateable - intentional.

An autoformatter would fix that and also improve many other code attributes.

Increase productivity, use an autoformatter.

OP intentionally keeps trailing whitespace. Editing and review work increases because of it.

Not cleaned out trailing spaces, they are - debateable - intentional.

An autoformatter would fix that and also improve many other code attributes.

added 651 characters in body
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97

Questionable use of ERANGE.

The standard library never uses ERANGE to indicate a no conversion.

if( !isdigit( *p ) ) { // capture invalid strings like "-", " -xyz", 
        errno = ERANGE;

Additional u128toasc() improvements:

Rather than UB if size is too small, detect insufficient size and return an error code. (NULL, "", etc.)

Simplify with a do { ... } while loop to eliminate unneeded str[ --j ] = ( x + '0');.


Questionable use of ERANGE.

The standard library never uses ERANGE to indicate a no conversion.

if( !isdigit( *p ) ) { // capture invalid strings like "-", " -xyz", 
        errno = ERANGE;

Additional u128toasc() improvements:

Rather than UB if size is too small, detect insufficient size and return an error code. (NULL, "", etc.)

Simplify with a do { ... } while loop to eliminate unneeded str[ --j ] = ( x + '0');.

added 651 characters in body
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97

Do not comment out relevant code

// #include "int128_test_5.h", with the // removed, fails the compile with error: unknown type name 'uint128'

OP failed to test the header file.

Header is missing code guards and int128 definition.


Do not comment out relevant code

// #include "int128_test_5.h", with the // removed, fails the compile with error: unknown type name 'uint128'

OP failed to test the header file.



Use size_t for array sizing and indexing rather than int.

// char*               u128toasc( char *s, int len, uint128 x ) 
char*               u128toasc( char *s, size_t len, uint128 x ) 

Advanced: Consider leading with the size

uint128 asctou128( char *sThe below allows for more error checking.

// char*               u128toasc( char *s, int len, uint128 x ) 
char*               u128toasc(size_t len, char *s, uint128 x     
 // or using later compilers
char*               u128toasc(size_t len, char s[len], uint128 x ) 

Minor: simplification

memmove( str, str + j, len - j );
return( str );

simpler as:

return memmove( str, str + j, len - j );

OP got lucky

What is the type and value of )( ~U128_MIN )?
int128 asctoi128( char s ) char u128toasc( char s, int len, uint128 x ) char i128toasc( char *s, int len, int128 x )~ is not operating on 128 bits.

#define U128_MIN 0
#define U128_MAX (uint128)( ~U128_MIN )  

Do not comment out relevant code

// #include "int128_test_5.h", with the // removed, fails the compile with error: unknown type name 'uint128'

OP failed to test the header file.


uint128 asctou128( char *s ) int128 asctoi128( char s ) char u128toasc( char s, int len, uint128 x ) char i128toasc( char *s, int len, int128 x )

Do not comment out relevant code

// #include "int128_test_5.h", with the // removed, fails the compile with error: unknown type name 'uint128'

OP failed to test the header file.

Header is missing code guards and int128 definition.



Use size_t for array sizing and indexing rather than int.

// char*               u128toasc( char *s, int len, uint128 x ) 
char*               u128toasc( char *s, size_t len, uint128 x ) 

Advanced: Consider leading with the size

The below allows for more error checking.

// char*               u128toasc( char *s, int len, uint128 x ) 
char*               u128toasc(size_t len, char *s, uint128 x     
 // or using later compilers
char*               u128toasc(size_t len, char s[len], uint128 x ) 

Minor: simplification

memmove( str, str + j, len - j );
return( str );

simpler as:

return memmove( str, str + j, len - j );

OP got lucky

What is the type and value of ( ~U128_MIN )?
~ is not operating on 128 bits.

#define U128_MIN 0
#define U128_MAX (uint128)( ~U128_MIN )  
added 651 characters in body
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97
Loading
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97
Loading