Browse Source

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

pull/2/head
Alibek Omarov 3 years ago
parent
commit
12815bfbf2
  1. 93
      public/crtlib.c
  2. 63
      public/crtlib.h

93
public/crtlib.c

@ -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 ) static qboolean Q_starcmp( const char *pattern, const char *text )
{ {
char c, c1; char c, c1;
@ -470,32 +408,8 @@ const char* Q_timestamp( int format )
return timestamp; return timestamp;
} }
char *Q_strstr( 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;
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 )
{ {
int c; int c;
size_t len; size_t len;
@ -517,8 +431,9 @@ char *Q_stristr( const char *string, const char *string2 )
} }
else return NULL; 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 ) int Q_vsnprintf( char *buffer, size_t buffersize, const char *format, va_list args )
{ {

63
public/crtlib.h

@ -16,9 +16,10 @@ GNU General Public License for more details.
#ifndef STDLIB_H #ifndef STDLIB_H
#define STDLIB_H #define STDLIB_H
#include <stdarg.h>
#include <string.h> #include <string.h>
#include <stdarg.h>
#include "build.h" #include "build.h"
#include "xash3d_types.h"
// timestamp modes // timestamp modes
enum enum
@ -60,14 +61,8 @@ float Q_atof( const char *str );
void Q_atov( float *vec, const char *str, size_t siz ); void Q_atov( float *vec, const char *str, size_t siz );
#define Q_strchr strchr #define Q_strchr strchr
#define Q_strrchr strrchr #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 ); qboolean Q_stricmpext( const char *s1, const char *s2 );
const char *Q_timestamp( int format ); 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 ) #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_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 ); 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( 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 ); 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 #endif//STDLIB_H

Loading…
Cancel
Save