From 4153221ca8cc235558e3d54697a8221d174ff422 Mon Sep 17 00:00:00 2001 From: Borja Ferrer Date: Fri, 29 Dec 2006 23:28:44 +0000 Subject: [PATCH] fixed a "-1" bug in atcprintf moved string functions to the stringutils file added strncopy --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40235 --- core/sm_stringutil.cpp | 70 +++++++++++++++++++++++++++++++++++++++--- core/sm_stringutil.h | 4 +++ core/smn_string.cpp | 56 ++------------------------------- 3 files changed, 72 insertions(+), 58 deletions(-) diff --git a/core/sm_stringutil.cpp b/core/sm_stringutil.cpp index 6b817cda..adca1a57 100644 --- a/core/sm_stringutil.cpp +++ b/core/sm_stringutil.cpp @@ -1,3 +1,5 @@ +#include +#include #include "sm_stringutil.h" #define ALT 0x00000001 /* alternate form */ @@ -262,7 +264,7 @@ size_t atcprintf(char *buffer, size_t maxlen, const char *format, IPluginContext int n; char sign; const char *fmt; - size_t llen = maxlen; + size_t llen = maxlen - 1; buf_p = buffer; arg = *param; @@ -337,7 +339,7 @@ reswitch: case 'c': { CHECK_ARGS(0); - if (llen <= 1) + if (!llen) { goto done; } @@ -387,7 +389,7 @@ reswitch: } case '%': { - if (llen <= 1) + if (!llen) { goto done; } @@ -397,7 +399,7 @@ reswitch: } case '\0': { - if (llen <= 1) + if (!llen) { goto done; } @@ -407,7 +409,7 @@ reswitch: } default: { - if (llen <= 1) + if (!llen) { goto done; } @@ -423,3 +425,61 @@ done: *param = arg; return (maxlen - llen); } + +const char *stristr(const char *str, const char *substr) +{ + if (!*substr) + { + return ((char *)str); + } + + char *needle = (char *)substr; + char *prevloc = (char *)str; + char *haystack = (char *)str; + + while (*haystack) + { + if (tolower(*haystack) == tolower(*needle)) + { + haystack++; + if (!*++needle) + { + return prevloc; + } + } else { + haystack = ++prevloc; + needle = (char *)substr; + } + } + + return NULL; +} + +inline int StrConvInt(const char *str) +{ + char *dummy; + return strtol(str, &dummy, 10); +} + +inline float StrConvFloat(const char *str) +{ + char *dummy; + return (float)strtod(str, &dummy); +} + +int strncopy(char *dest, const char *src, size_t count) +{ + if (!count) + { + return 0; + } + + char *start = dest; + while ((*src) && (--count)) + { + *dest++ = *src++; + } + *dest = '\0'; + + return (dest - start); +} diff --git a/core/sm_stringutil.h b/core/sm_stringutil.h index b9ae216a..2ac314c0 100644 --- a/core/sm_stringutil.h +++ b/core/sm_stringutil.h @@ -8,5 +8,9 @@ using namespace SourcePawn; size_t atcprintf(char *buffer, size_t maxlen, const char *format, IPluginContext *pCtx, const cell_t *params, int *param); +const char *stristr(const char *str, const char *substr); +int StrConvInt(const char *str); +float StrConvFloat(const char *str); +int strncopy(char *dest, const char *src, size_t count); #endif // _INCLUDE_SOURCEMOD_STRINGUTIL_H_ diff --git a/core/smn_string.cpp b/core/smn_string.cpp index b76c485f..e41df049 100644 --- a/core/smn_string.cpp +++ b/core/smn_string.cpp @@ -1,41 +1,11 @@ #include "sm_platform.h" -#include #include -#include #include "sp_vm_api.h" #include "sp_vm_typeutil.h" +#include "sm_stringutil.h" using namespace SourcePawn; -const char *stristr(const char *str, const char *substr) -{ - if (!*substr) - { - return ((char *)str); - } - - char *needle = (char *)substr; - char *prevloc = (char *)str; - char *haystack = (char *)str; - - while (*haystack) - { - if (tolower(*haystack) == tolower(*needle)) - { - haystack++; - if (!*++needle) - { - return prevloc; - } - } else { - haystack = ++prevloc; - needle = (char *)substr; - } - } - - return NULL; -} - inline const char *_strstr(const char *str, const char *substr) { #ifdef PLATFORM_WINDOWS @@ -45,17 +15,6 @@ inline const char *_strstr(const char *str, const char *substr) #endif } -inline int StrConvInt(const char *str) -{ - char *dummy; - return strtol(str, &dummy, 10); -} - -inline float StrConvFloat(const char *str) -{ - char *dummy; - return (float)strtod(str, &dummy); -} /********************************************* * * @@ -107,21 +66,12 @@ static cell_t sm_equal(IPluginContext *pCtx, const cell_t *params) static cell_t sm_strcopy(IPluginContext *pCtx, const cell_t *params) { - char *dest, *src, *start; - int len; + char *dest, *src; pCtx->LocalToString(params[1], &dest); pCtx->LocalToString(params[3], &src); - len = params[2]; - start = dest; - while ((*src) && (len--)) - { - *dest++ = *src++; - } - *dest = '\0'; - - return (dest - start); + return strncopy(dest, src, params[2]); } static cell_t sm_strtonum(IPluginContext *pCtx, const cell_t *params)