added StrBreak from AMX Mod X, with various improvements

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40637
This commit is contained in:
David Anderson 2007-03-16 20:10:13 +00:00
parent 632608b5bb
commit 19cfb041aa
4 changed files with 114 additions and 2 deletions

View File

@ -25,6 +25,11 @@ TextParsers g_TextParser;
static int g_ini_chartable1[255] = {0}; static int g_ini_chartable1[255] = {0};
static int g_ws_chartable[255] = {0}; static int g_ws_chartable[255] = {0};
bool IsWhitespace(const char *stream)
{
return g_ws_chartable[(unsigned)*stream] == 1;
}
TextParsers::TextParsers() TextParsers::TextParsers()
{ {
g_ini_chartable1[(unsigned)'_'] = 1; g_ini_chartable1[(unsigned)'_'] = 1;

View File

@ -37,6 +37,8 @@ inline unsigned int _GetUTF8CharBytes(const char *stream)
return 1; return 1;
} }
bool IsWhitespace(const char *stream);
/** /**
* @param void * IN: Stream pointer * @param void * IN: Stream pointer
* @param char * IN/OUT: Stream buffer * @param char * IN/OUT: Stream buffer

View File

@ -15,6 +15,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "sm_globals.h" #include "sm_globals.h"
#include "sm_stringutil.h" #include "sm_stringutil.h"
#include "TextParsers.h"
inline const char *_strstr(const char *str, const char *substr) inline const char *_strstr(const char *str, const char *substr)
{ {
@ -219,9 +220,99 @@ static cell_t sm_vformat(IPluginContext *pContext, const cell_t *params)
return total; return total;
} }
REGISTER_NATIVES(basicstrings) /* :TODO: make this UTF8 safe */
static cell_t StrBreak(IPluginContext *pContext, const cell_t *params)
{
const char *input;
char *out;
size_t outMax;
/* Get parameters */
pContext->LocalToString(params[1], (char **)&input);
pContext->LocalToString(params[2], &out);
outMax = params[3];
const char *inptr = input;
/* Eat up whitespace */
while (*inptr != '\0' && IsWhitespace(inptr))
{
inptr++;
}
if (*inptr == '\0')
{
if (outMax)
{
*out = '\0';
}
return -1;
}
const char *start, *end = NULL;
bool quoted = (*inptr == '"');
if (quoted)
{
inptr++;
start = inptr;
/* Read input until we reach a quote. */
while (*inptr != '\0' && *inptr != '"')
{
/* Update the end point, increment the stream. */
end = inptr++;
}
/* Read one more token if we reached an end quote */
if (*inptr == '"')
{
inptr++;
}
} else {
start = inptr;
/* Read input until we reach a space */
while (*inptr != '\0' && !IsWhitespace(inptr))
{
/* Update the end point, increment the stream. */
end = inptr++;
}
}
/* Copy the string we found, if necessary */
if (end == NULL)
{
if (outMax)
{
*out = '\0';
}
} else if (outMax) {
char *outptr = out;
outMax--;
for (const char *ptr=start;
(ptr <= end) && ((unsigned)(outptr - out) < (outMax));
ptr++, outptr++)
{
*outptr = *ptr;
}
*outptr = '\0';
}
/* Consume more of the string until we reach non-whitespace */
while (*inptr != '\0' && IsWhitespace(inptr))
{
inptr++;
}
if (*inptr == '\0')
{
return -1;
}
return inptr - input;
}
REGISTER_NATIVES(basicStrings)
{ {
{"strlen", sm_strlen}, {"strlen", sm_strlen},
{"StrBreak", StrBreak},
{"StrContains", sm_contain}, {"StrContains", sm_contain},
{"StrCompare", sm_strcmp}, {"StrCompare", sm_strcmp},
{"StrCopy", sm_strcopy}, {"StrCopy", sm_strcopy},

View File

@ -159,3 +159,17 @@ native Float:StringToFloat(const String:str[]);
* @return Number of cells written to buffer. * @return Number of cells written to buffer.
*/ */
native FloatToString(Float:num, String:str[], maxlength); native FloatToString(Float:num, String:str[], maxlength);
/**
* Finds the first "argument" in a string; either a set of space
* terminated characters, or a fully quoted string. After the
* argument is found, whitespace is read until the next portion
* of the string is reached. If nothing remains, -1 is returned.
* Otherwise, the index to the first character is returned.
*
* @param source Source input string.
* @param arg Stores argument read from string.
* @param argLen Maximum length of argument buffer.
* @return Index to next piece of string, or -1 if none.
*/
native StrBreak(const String:source[], String:arg[], argLen);