public: remove naive implementations of standard function, add them with standard C with few extensions

This commit is contained in:
Alibek Omarov 2022-06-29 02:39:18 +03:00
parent b9b8b0521b
commit 12815bfbf2
2 changed files with 60 additions and 96 deletions

View File

@ -317,68 +317,6 @@ void Q_atov( float *vec, const char *str, size_t siz )
}
}
int Q_strnicmp( const char *s1, const char *s2, int n )
{
int c1, c2;
if( s1 == NULL )
{
if( s2 == NULL )
return 0;
else return -1;
}
else if( s2 == NULL )
{
return 1;
}
do {
c1 = *s1++;
c2 = *s2++;
if( !n-- ) return 0; // strings are equal until end point
if( c1 != c2 )
{
if( c1 >= 'a' && c1 <= 'z' ) c1 -= ('a' - 'A');
if( c2 >= 'a' && c2 <= 'z' ) c2 -= ('a' - 'A');
if( c1 != c2 ) return c1 < c2 ? -1 : 1;
}
} while( c1 );
// strings are equal
return 0;
}
int Q_strncmp( const char *s1, const char *s2, int n )
{
int c1, c2;
if( s1 == NULL )
{
if( s2 == NULL )
return 0;
else return -1;
}
else if( s2 == NULL )
{
return 1;
}
do {
c1 = *s1++;
c2 = *s2++;
// strings are equal until end point
if( !n-- ) return 0;
if( c1 != c2 ) return c1 < c2 ? -1 : 1;
} while( c1 );
// strings are equal
return 0;
}
static qboolean Q_starcmp( const char *pattern, const char *text )
{
char c, c1;
@ -470,32 +408,8 @@ const char* Q_timestamp( int format )
return timestamp;
}
char *Q_strstr( const char *string, const char *string2 )
{
int c;
size_t len;
if( !string || !string2 ) return NULL;
c = *string2;
len = Q_strlen( string2 );
while( string )
{
for( ; *string && *string != c; string++ );
if( *string )
{
if( !Q_strncmp( string, string2, len ))
break;
string++;
}
else return NULL;
}
return (char *)string;
}
char *Q_stristr( const char *string, const char *string2 )
#if !defined( HAVE_STRCASESTR )
const char *Q_stristr( const char *string, const char *string2 )
{
int c;
size_t len;
@ -517,8 +431,9 @@ char *Q_stristr( const char *string, const char *string2 )
}
else return NULL;
}
return (char *)string;
return string;
}
#endif // !defined( HAVE_STRCASESTR )
int Q_vsnprintf( char *buffer, size_t buffersize, const char *format, va_list args )
{

View File

@ -16,9 +16,10 @@ GNU General Public License for more details.
#ifndef STDLIB_H
#define STDLIB_H
#include <stdarg.h>
#include <string.h>
#include <stdarg.h>
#include "build.h"
#include "xash3d_types.h"
// timestamp modes
enum
@ -60,14 +61,8 @@ float Q_atof( const char *str );
void Q_atov( float *vec, const char *str, size_t siz );
#define Q_strchr strchr
#define Q_strrchr strrchr
#define Q_stricmp( s1, s2 ) Q_strnicmp( s1, s2, 99999 )
int Q_strnicmp( const char *s1, const char *s2, int n );
#define Q_strcmp( s1, s2 ) Q_strncmp( s1, s2, 99999 )
int Q_strncmp( const char *s1, const char *s2, int n );
qboolean Q_stricmpext( const char *s1, const char *s2 );
const char *Q_timestamp( int format );
char *Q_stristr( const char *string, const char *string2 );
char *Q_strstr( const char *string, const char *string2 );
#define Q_vsprintf( buffer, format, args ) Q_vsnprintf( buffer, 99999, format, args )
int Q_vsnprintf( char *buffer, size_t buffersize, const char *format, va_list args );
int Q_snprintf( char *buffer, size_t buffersize, const char *format, ... ) _format( 3 );
@ -95,4 +90,58 @@ char *_COM_ParseFileSafe( char *data, char *token, const int size, unsigned int
int matchpattern( const char *in, const char *pattern, qboolean caseinsensitive );
int matchpattern_with_separator( const char *in, const char *pattern, qboolean caseinsensitive, const char *separators, qboolean wildcard_least_one );
// libc implementations
static inline int Q_strcmp( const char *s1, const char *s2 )
{
return unlikely(!s1) ?
( !s2 ? 0 : -1 ) :
( unlikely(!s2) ? 1 : strcmp( s1, s2 ));
}
static inline int Q_strncmp( const char *s1, const char *s2, size_t n )
{
return unlikely(!s1) ?
( !s2 ? 0 : -1 ) :
( unlikely(!s2) ? 1 : strncmp( s1, s2, n ));
}
static inline const char *Q_strstr( const char *s1, const char *s2 )
{
return unlikely( !s1 || !s2 ) ? NULL : strstr( s1, s2 );
}
// libc extensions, be careful
#if XASH_WIN32
#define strcasecmp stricmp
#define strncasecmp strnicmp
#endif // XASH_WIN32
static inline int Q_stricmp( const char *s1, const char *s2 )
{
return unlikely(!s1) ?
( !s2 ? 0 : -1 ) :
( unlikely(!s2) ? 1 : strcasecmp( s1, s2 ));
}
static inline int Q_strnicmp( const char *s1, const char *s2, size_t n )
{
return unlikely(!s1) ?
( !s2 ? 0 : -1 ) :
( unlikely(!s2) ? 1 : strncasecmp( s1, s2, n ));
}
#if defined( HAVE_STRCASESTR )
#if XASH_WIN32
#define strcasestr stristr
#endif
static inline const char *Q_stristr( const char *s1, const char *s2 )
{
return unlikely( !s1 || !s2 ) ? NULL : strcasestr( s1, s2 );
}
#else // defined( HAVE_STRCASESTR )
const char *Q_stristr( const char *s1, const char *s2 );
#endif // defined( HAVE_STRCASESTR )
#endif//STDLIB_H