core/logic/smn_string.cpp
changeset 3006 39a4611feaba
parent 3005 2db965f8db3f
child 4134 e86d4a5a1fe5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/logic/smn_string.cpp	Fri May 14 20:04:44 2010 -0700
     1.3 @@ -0,0 +1,682 @@
     1.4 +/**
     1.5 + * vim: set ts=4 sw=4 tw=99 noet :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#include "common_logic.h"
    1.36 +#include <string.h>
    1.37 +#include <stdlib.h>
    1.38 +#include <ITextParsers.h>
    1.39 +#include <ctype.h>
    1.40 +#include "stringutil.h"
    1.41 +
    1.42 +inline const char *_strstr(const char *str, const char *substr)
    1.43 +{
    1.44 +#ifdef PLATFORM_WINDOWS
    1.45 +	return strstr(str, substr);
    1.46 +#elif defined PLATFORM_LINUX || defined PLATFORM_APPLE
    1.47 +	return (const char *)strstr(str, substr);
    1.48 +#endif
    1.49 +}
    1.50 +
    1.51 +/*********************************************
    1.52 +*                                            *
    1.53 +* STRING MANIPULATION NATIVE IMPLEMENTATIONS *
    1.54 +*                                            *
    1.55 +*********************************************/
    1.56 +
    1.57 +static cell_t sm_strlen(IPluginContext *pCtx, const cell_t *params)
    1.58 +{
    1.59 +	char *str;
    1.60 +	pCtx->LocalToString(params[1], &str);
    1.61 +
    1.62 +	return strlen(str);
    1.63 +}
    1.64 +
    1.65 +static cell_t sm_contain(IPluginContext *pCtx, const cell_t *params)
    1.66 +{
    1.67 +	typedef const char *(*STRSEARCH)(const char *, const char *);
    1.68 +	STRSEARCH func;
    1.69 +	char *str, *substr;
    1.70 +
    1.71 +	pCtx->LocalToString(params[1], &str);
    1.72 +	pCtx->LocalToString(params[2], &substr);
    1.73 +
    1.74 +	func = (params[3]) ? _strstr : stristr;
    1.75 +	const char *pos = func(str, substr);
    1.76 +	if (pos)
    1.77 +	{
    1.78 +		return (pos - str);
    1.79 +	}
    1.80 +
    1.81 +	return -1;
    1.82 +}
    1.83 +
    1.84 +static cell_t sm_strcmp(IPluginContext *pCtx, const cell_t *params)
    1.85 +{
    1.86 +	typedef int (*STRCOMPARE)(const char *, const char *);
    1.87 +	STRCOMPARE func;
    1.88 +	char *str1, *str2;
    1.89 +
    1.90 +	pCtx->LocalToString(params[1], &str1);
    1.91 +	pCtx->LocalToString(params[2], &str2);
    1.92 +
    1.93 +	func = (params[3]) ? strcmp : stricmp;
    1.94 +
    1.95 +	return (func(str1, str2));
    1.96 +}
    1.97 +
    1.98 +static cell_t sm_strncmp(IPluginContext *pCtx, const cell_t *params)
    1.99 +{
   1.100 +	char *str1, *str2;
   1.101 +
   1.102 +	pCtx->LocalToString(params[1], &str1);
   1.103 +	pCtx->LocalToString(params[2], &str2);
   1.104 +	
   1.105 +	if  (params[4])
   1.106 +	{
   1.107 +		return strncmp(str1, str2, (size_t)params[3]);
   1.108 +	} else {
   1.109 +		return strncasecmp(str1, str2, (size_t)params[3]);
   1.110 +	}
   1.111 +}
   1.112 +
   1.113 +static cell_t sm_strcopy(IPluginContext *pCtx, const cell_t *params)
   1.114 +{
   1.115 +	char *dest, *src;
   1.116 +
   1.117 +	pCtx->LocalToString(params[1], &dest);
   1.118 +	pCtx->LocalToString(params[3], &src);
   1.119 +
   1.120 +	return strncopy(dest, src, params[2]);
   1.121 +}
   1.122 +
   1.123 +static cell_t sm_strconvint(IPluginContext *pCtx, const cell_t *params)
   1.124 +{
   1.125 +	char *str, *dummy;
   1.126 +	pCtx->LocalToString(params[1], &str);
   1.127 +
   1.128 +	return static_cast<cell_t>(strtol(str, &dummy, params[2]));
   1.129 +}
   1.130 +
   1.131 +static cell_t StringToIntEx(IPluginContext *pCtx, const cell_t *params)
   1.132 +{
   1.133 +	char *str, *dummy = NULL;
   1.134 +	cell_t *addr;
   1.135 +	pCtx->LocalToString(params[1], &str);
   1.136 +	pCtx->LocalToPhysAddr(params[2], &addr);
   1.137 +
   1.138 +	*addr = static_cast<cell_t>(strtol(str, &dummy, params[3]));
   1.139 +
   1.140 +	return dummy - str;
   1.141 +}
   1.142 +
   1.143 +static cell_t sm_numtostr(IPluginContext *pCtx, const cell_t *params)
   1.144 +{
   1.145 +	char *str;
   1.146 +	pCtx->LocalToString(params[2], &str);
   1.147 +	size_t res = smcore.Format(str, params[3], "%d", params[1]);
   1.148 +
   1.149 +	return static_cast<cell_t>(res);
   1.150 +}
   1.151 +
   1.152 +static cell_t sm_strtofloat(IPluginContext *pCtx, const cell_t *params)
   1.153 +{
   1.154 +	char *str, *dummy;
   1.155 +	pCtx->LocalToString(params[1], &str);
   1.156 +
   1.157 +	float val = (float)strtod(str, &dummy);
   1.158 +
   1.159 +	return sp_ftoc(val);
   1.160 +}
   1.161 +
   1.162 +static cell_t StringToFloatEx(IPluginContext *pCtx, const cell_t *params)
   1.163 +{
   1.164 +	char *str, *dummy = NULL;
   1.165 +	cell_t *addr;
   1.166 +	pCtx->LocalToString(params[1], &str);
   1.167 +	pCtx->LocalToPhysAddr(params[2], &addr);
   1.168 +
   1.169 +	float val = (float)strtod(str, &dummy);
   1.170 +
   1.171 +	*addr = sp_ftoc(val);
   1.172 +
   1.173 +	return dummy - str;
   1.174 +}
   1.175 +
   1.176 +static cell_t sm_floattostr(IPluginContext *pCtx, const cell_t *params)
   1.177 +{
   1.178 +	char *str;
   1.179 +	pCtx->LocalToString(params[2], &str);
   1.180 +	size_t res = smcore.Format(str, params[3], "%f", sp_ctof(params[1]));
   1.181 +
   1.182 +	return static_cast<cell_t>(res);
   1.183 +}
   1.184 +
   1.185 +static cell_t sm_formatex(IPluginContext *pCtx, const cell_t *params)
   1.186 +{
   1.187 +	char *buf, *fmt;
   1.188 +	size_t res;
   1.189 +	int arg = 4;
   1.190 +
   1.191 +	pCtx->LocalToString(params[1], &buf);
   1.192 +	pCtx->LocalToString(params[3], &fmt);
   1.193 +	res = smcore.atcprintf(buf, static_cast<size_t>(params[2]), fmt, pCtx, params, &arg);
   1.194 +
   1.195 +	return static_cast<cell_t>(res);
   1.196 +}
   1.197 +
   1.198 +class StaticCharBuf
   1.199 +{
   1.200 +    char *buffer;
   1.201 +    size_t max_size;
   1.202 +public:
   1.203 +    StaticCharBuf() : buffer(NULL), max_size(0)
   1.204 +    {
   1.205 +    }
   1.206 +    ~StaticCharBuf()
   1.207 +    {
   1.208 +        free(buffer);
   1.209 +    }
   1.210 +    char* GetWithSize(size_t len)
   1.211 +    {
   1.212 +        if (len > max_size)
   1.213 +        {
   1.214 +            buffer = (char *)realloc(buffer, len);
   1.215 +            max_size = len;
   1.216 +        }
   1.217 +        return buffer;
   1.218 +    }
   1.219 +};
   1.220 +
   1.221 +static char g_formatbuf[2048];
   1.222 +static StaticCharBuf g_extrabuf;
   1.223 +static cell_t sm_format(IPluginContext *pCtx, const cell_t *params)
   1.224 +{
   1.225 +	char *buf, *fmt, *destbuf;
   1.226 +	cell_t start_addr, end_addr, maxparam;
   1.227 +	size_t res, maxlen;
   1.228 +	int arg = 4;
   1.229 +	bool copy = false;
   1.230 +	char *__copy_buf;
   1.231 +
   1.232 +	pCtx->LocalToString(params[1], &destbuf);
   1.233 +	pCtx->LocalToString(params[3], &fmt);
   1.234 +
   1.235 +	maxlen = static_cast<size_t>(params[2]);
   1.236 +	start_addr = params[1];
   1.237 +	end_addr = params[1] + maxlen;
   1.238 +	maxparam = params[0];
   1.239 +
   1.240 +	for (cell_t i=3; i<=maxparam; i++)
   1.241 +	{
   1.242 +		if ((params[i] >= start_addr) && (params[i] <= end_addr))
   1.243 +		{
   1.244 +			copy = true;
   1.245 +			break;
   1.246 +		}
   1.247 +	}
   1.248 +
   1.249 +	if (copy)
   1.250 +	{
   1.251 +		if (maxlen > sizeof(g_formatbuf))
   1.252 +		{
   1.253 +			__copy_buf = g_extrabuf.GetWithSize(maxlen);
   1.254 +		}
   1.255 +		else
   1.256 +		{
   1.257 +			__copy_buf = g_formatbuf;
   1.258 +		}
   1.259 +	}
   1.260 +
   1.261 +	buf = (copy) ? __copy_buf : destbuf;
   1.262 +	res = smcore.atcprintf(buf, maxlen, fmt, pCtx, params, &arg);
   1.263 +
   1.264 +	if (copy)
   1.265 +	{
   1.266 +		memcpy(destbuf, __copy_buf, res+1);
   1.267 +	}
   1.268 +
   1.269 +	return static_cast<cell_t>(res);
   1.270 +}
   1.271 +
   1.272 +static cell_t sm_vformat(IPluginContext *pContext, const cell_t *params)
   1.273 +{
   1.274 +	int vargPos = static_cast<int>(params[4]);
   1.275 +
   1.276 +	/* Get the parent parameter array */
   1.277 +	cell_t *local_params = pContext->GetLocalParams();
   1.278 +
   1.279 +	cell_t max = local_params[0];
   1.280 +	if (vargPos > (int)max + 1)
   1.281 +	{
   1.282 +		return pContext->ThrowNativeError("Argument index is invalid: %d", vargPos);
   1.283 +	}
   1.284 +
   1.285 +	cell_t addr_start = params[1];
   1.286 +	cell_t addr_end = addr_start + params[2];
   1.287 +	bool copy = false;
   1.288 +	for (int i=vargPos; i<=max; i++)
   1.289 +	{
   1.290 +		/* Does this clip bounds? */
   1.291 +		if ((local_params[i] >= addr_start)
   1.292 +			&& (local_params[i] <= addr_end))
   1.293 +		{
   1.294 +			copy = true;
   1.295 +			break;
   1.296 +		}
   1.297 +	}
   1.298 +
   1.299 +	/* Get destination info */
   1.300 +	char *format, *destination;
   1.301 +	size_t maxlen = static_cast<size_t>(params[2]);
   1.302 +
   1.303 +	if (copy)
   1.304 +	{
   1.305 +		destination = g_formatbuf;
   1.306 +	} else {
   1.307 +		pContext->LocalToString(params[1], &destination);
   1.308 +	}
   1.309 +
   1.310 +	pContext->LocalToString(params[3], &format);
   1.311 +
   1.312 +	size_t total = smcore.atcprintf(destination, maxlen, format, pContext, local_params, &vargPos);
   1.313 +
   1.314 +	/* Perform copy-on-write if we need to */
   1.315 +	if (copy)
   1.316 +	{
   1.317 +		pContext->StringToLocal(params[1], maxlen, g_formatbuf);
   1.318 +	}
   1.319 +
   1.320 +	return total;
   1.321 +}
   1.322 +
   1.323 +/* :TODO: make this UTF8 safe */
   1.324 +static cell_t BreakString(IPluginContext *pContext, const cell_t *params)
   1.325 +{
   1.326 +	const char *input;
   1.327 +	char *out;
   1.328 +	size_t outMax;
   1.329 +
   1.330 +	/* Get parameters */
   1.331 +	pContext->LocalToString(params[1], (char **)&input);
   1.332 +	pContext->LocalToString(params[2], &out);
   1.333 +	outMax = params[3];
   1.334 +
   1.335 +	const char *inptr = input;
   1.336 +	/* Eat up whitespace */
   1.337 +	while (*inptr != '\0' && textparsers->IsWhitespace(inptr))
   1.338 +	{
   1.339 +		inptr++;
   1.340 +	}
   1.341 +
   1.342 +	if (*inptr == '\0')
   1.343 +	{
   1.344 +		if (outMax)
   1.345 +		{
   1.346 +			*out = '\0';
   1.347 +		}
   1.348 +		return -1;
   1.349 +	}
   1.350 +
   1.351 +	const char *start, *end = NULL;
   1.352 +
   1.353 +	bool quoted = (*inptr == '"');
   1.354 +	if (quoted)
   1.355 +	{
   1.356 +		inptr++;
   1.357 +		start = inptr;
   1.358 +		/* Read input until we reach a quote. */
   1.359 +		while (*inptr != '\0' && *inptr != '"')
   1.360 +		{
   1.361 +			/* Update the end point, increment the stream. */
   1.362 +			end = inptr++;
   1.363 +		}
   1.364 +		/* Read one more token if we reached an end quote */
   1.365 +		if (*inptr == '"')
   1.366 +		{
   1.367 +			inptr++;
   1.368 +		}
   1.369 +	} else {
   1.370 +		start = inptr;
   1.371 +		/* Read input until we reach a space */
   1.372 +		while (*inptr != '\0' && !textparsers->IsWhitespace(inptr))
   1.373 +		{
   1.374 +			/* Update the end point, increment the stream. */
   1.375 +			end = inptr++;
   1.376 +		}
   1.377 +	}
   1.378 +
   1.379 +	/* Copy the string we found, if necessary */
   1.380 +	if (end == NULL)
   1.381 +	{
   1.382 +		if (outMax)
   1.383 +		{
   1.384 +			*out = '\0';
   1.385 +		}
   1.386 +	} else if (outMax) {
   1.387 +		char *outptr = out;
   1.388 +		outMax--;
   1.389 +		for (const char *ptr=start;
   1.390 +			(ptr <= end) && ((unsigned)(outptr - out) < (outMax));
   1.391 +			ptr++, outptr++)
   1.392 +			{
   1.393 +			*outptr = *ptr;
   1.394 +		}
   1.395 +		*outptr = '\0';
   1.396 +	}
   1.397 +
   1.398 +	/* Consume more of the string until we reach non-whitespace */
   1.399 +	while (*inptr != '\0' && textparsers->IsWhitespace(inptr))
   1.400 +	{
   1.401 +		inptr++;
   1.402 +	}
   1.403 +
   1.404 +	if (*inptr == '\0')
   1.405 +	{
   1.406 +		return -1;
   1.407 +	}
   1.408 +
   1.409 +	return inptr - input;
   1.410 +}
   1.411 +
   1.412 +static cell_t GetCharBytes(IPluginContext *pContext, const cell_t *params)
   1.413 +{
   1.414 +	char *str;
   1.415 +	pContext->LocalToString(params[1], &str);
   1.416 +
   1.417 +	return _GetUTF8CharBytes(str);
   1.418 +};
   1.419 +
   1.420 +static cell_t IsCharAlpha(IPluginContext *pContext, const cell_t *params)
   1.421 +{
   1.422 +	char chr = params[1];
   1.423 +
   1.424 +	if (_GetUTF8CharBytes(&chr) != 1)
   1.425 +	{
   1.426 +		return 0;
   1.427 +	}
   1.428 +
   1.429 +	return isalpha(chr);
   1.430 +}
   1.431 +
   1.432 +static cell_t IsCharNumeric(IPluginContext *pContext, const cell_t *params)
   1.433 +{
   1.434 +	char chr = params[1];
   1.435 +
   1.436 +	if (_GetUTF8CharBytes(&chr) != 1)
   1.437 +	{
   1.438 +		return 0;
   1.439 +	}
   1.440 +
   1.441 +	return isdigit(chr);
   1.442 +}
   1.443 +
   1.444 +static cell_t IsCharSpace(IPluginContext *pContext, const cell_t *params)
   1.445 +{
   1.446 +	char chr = params[1];
   1.447 +
   1.448 +	if (_GetUTF8CharBytes(&chr) != 1)
   1.449 +	{
   1.450 +		return 0;
   1.451 +	}
   1.452 +
   1.453 +	return isspace(chr);
   1.454 +}
   1.455 +
   1.456 +static cell_t IsCharMB(IPluginContext *pContext, const cell_t *params)
   1.457 +{
   1.458 +	char chr = params[1];
   1.459 +
   1.460 +	unsigned int bytes = _GetUTF8CharBytes(&chr);
   1.461 +	if (bytes == 1)
   1.462 +	{
   1.463 +		return 0;
   1.464 +	}
   1.465 +
   1.466 +	return bytes;
   1.467 +}
   1.468 +
   1.469 +static cell_t IsCharUpper(IPluginContext *pContext, const cell_t *params)
   1.470 +{
   1.471 +	char chr = params[1];
   1.472 +
   1.473 +	if (_GetUTF8CharBytes(&chr) != 1)
   1.474 +	{
   1.475 +		return 0;
   1.476 +	}
   1.477 +
   1.478 +	return isupper(chr);
   1.479 +}
   1.480 +
   1.481 +static cell_t IsCharLower(IPluginContext *pContext, const cell_t *params)
   1.482 +{
   1.483 +	char chr = params[1];
   1.484 +
   1.485 +	if (_GetUTF8CharBytes(&chr) != 1)
   1.486 +	{
   1.487 +		return 0;
   1.488 +	}
   1.489 +
   1.490 +	return islower(chr);
   1.491 +}
   1.492 +
   1.493 +static cell_t ReplaceString(IPluginContext *pContext, const cell_t *params)
   1.494 +{
   1.495 +	char *text, *search, *replace;
   1.496 +	size_t maxlength;
   1.497 +	bool caseSensitive;
   1.498 +	
   1.499 +	pContext->LocalToString(params[1], &text);
   1.500 +	pContext->LocalToString(params[3], &search);
   1.501 +	pContext->LocalToString(params[4], &replace);
   1.502 +	maxlength = (size_t)params[2];
   1.503 +
   1.504 +	/* Account for old binary plugins that won't pass the last parameter */
   1.505 +	if (params[0] == 5) 
   1.506 +	{
   1.507 +		caseSensitive = (bool)params[5];
   1.508 +	}
   1.509 +	else
   1.510 +	{
   1.511 +		caseSensitive = true;
   1.512 +	}
   1.513 +
   1.514 +	if (search[0] == '\0')
   1.515 +	{
   1.516 +		return pContext->ThrowNativeError("Cannot replace searches of empty strings");
   1.517 +	}
   1.518 +
   1.519 +	return UTIL_ReplaceAll(text, maxlength, search, replace, caseSensitive);
   1.520 +}
   1.521 +
   1.522 +static cell_t ReplaceStringEx(IPluginContext *pContext, const cell_t *params)
   1.523 +{
   1.524 +	char *text, *search, *replace;
   1.525 +	size_t maxlength;
   1.526 +	bool caseSensitive;
   1.527 +
   1.528 +	pContext->LocalToString(params[1], &text);
   1.529 +	pContext->LocalToString(params[3], &search);
   1.530 +	pContext->LocalToString(params[4], &replace);
   1.531 +	maxlength = (size_t)params[2];
   1.532 +
   1.533 +	size_t searchLen = (params[5] == -1) ? strlen(search) : (size_t)params[5];
   1.534 +	size_t replaceLen = (params[6] == -1) ? strlen(replace) : (size_t)params[6];
   1.535 +
   1.536 +	/* Account for old binary plugins that won't pass the last parameter */
   1.537 +	if (params[0] == 7)
   1.538 +	{
   1.539 +		caseSensitive = (bool)params[7];
   1.540 +	}
   1.541 +	else
   1.542 +	{
   1.543 +		caseSensitive = true;
   1.544 +	}
   1.545 +
   1.546 +	if (searchLen == 0)
   1.547 +	{
   1.548 +		return pContext->ThrowNativeError("Cannot replace searches of empty strings");
   1.549 +	}
   1.550 +
   1.551 +	char *ptr = UTIL_ReplaceEx(text, maxlength, search, searchLen, replace, replaceLen, caseSensitive);
   1.552 +
   1.553 +	if (ptr == NULL)
   1.554 +	{
   1.555 +		return -1;
   1.556 +	}
   1.557 +
   1.558 +	return ptr - text;
   1.559 +}
   1.560 +
   1.561 +static cell_t TrimString(IPluginContext *pContext, const cell_t *params)
   1.562 +{
   1.563 +	char *str;
   1.564 +	pContext->LocalToString(params[1], &str);
   1.565 +
   1.566 +	size_t chars = strlen(str);
   1.567 +
   1.568 +	if (chars == 0)
   1.569 +	{
   1.570 +		return 0;
   1.571 +	}
   1.572 +
   1.573 +	char *end = str + chars - 1;
   1.574 +
   1.575 +	/* Iterate backwards through string until we reach first non-whitespace char */
   1.576 +	while (end >= str && textparsers->IsWhitespace(end))
   1.577 +	{
   1.578 +		end--;
   1.579 +	}
   1.580 +
   1.581 +	/* Replace first whitespace char (at the end) with null terminator */
   1.582 +	*(end + 1) = '\0';
   1.583 +
   1.584 +	/* Iterate forwards through string until first non-whitespace char is reached */
   1.585 +	while (textparsers->IsWhitespace(str))
   1.586 +	{
   1.587 +		str++;
   1.588 +	}
   1.589 +
   1.590 +	size_t bytes;
   1.591 +	pContext->StringToLocalUTF8(params[1], chars + 1, str, &bytes);
   1.592 +
   1.593 +	return bytes;
   1.594 +}
   1.595 +
   1.596 +static cell_t SplitString(IPluginContext *pContext, const cell_t *params)
   1.597 +{
   1.598 +	char *text, *split;
   1.599 +	
   1.600 +	pContext->LocalToString(params[1], &text);
   1.601 +	pContext->LocalToString(params[2], &split);
   1.602 +
   1.603 +	size_t maxLen = (size_t)params[4];
   1.604 +	size_t textLen = strlen(text);
   1.605 +	size_t splitLen = strlen(split);
   1.606 +
   1.607 +	if (splitLen > textLen)
   1.608 +	{
   1.609 +		return -1;
   1.610 +	}
   1.611 +
   1.612 +	/**
   1.613 +	 * Note that it's <= ... you could also just add 1,
   1.614 +	 * but this is a bit nicer
   1.615 +	 */
   1.616 +	for (size_t i=0; i<=textLen - splitLen; i++)
   1.617 +	{
   1.618 +		if (strncmp(&text[i], split, splitLen) == 0)
   1.619 +		{
   1.620 +			/* Split hereeeee */
   1.621 +			if (i >= maxLen)
   1.622 +			{
   1.623 +				pContext->StringToLocalUTF8(params[3], maxLen, text, NULL);
   1.624 +			} else {
   1.625 +				pContext->StringToLocalUTF8(params[3], i+1, text, NULL);
   1.626 +			}
   1.627 +			return (cell_t)(i + splitLen);
   1.628 +		}
   1.629 +	}
   1.630 +
   1.631 +	return -1;
   1.632 +}
   1.633 +
   1.634 +static cell_t StripQuotes(IPluginContext *pContext, const cell_t *params)
   1.635 +{
   1.636 +	char *text;
   1.637 +	size_t length;
   1.638 +
   1.639 +	pContext->LocalToString(params[1], &text);
   1.640 +	length = strlen(text);
   1.641 +
   1.642 +	if (text[0] == '"' && text[length-1] == '"')
   1.643 +	{
   1.644 +		/* Null-terminate */
   1.645 +		text[--length] = '\0';
   1.646 +		/* Move the remaining bytes (including null terminator) down by one */
   1.647 +		memmove(&text[0], &text[1], length);
   1.648 +
   1.649 +		return 1;
   1.650 +	}
   1.651 +
   1.652 +	return 0;	
   1.653 +}
   1.654 +
   1.655 +REGISTER_NATIVES(basicStrings)
   1.656 +{
   1.657 +	{"BreakString",			BreakString},
   1.658 +	{"FloatToString",		sm_floattostr},
   1.659 +	{"Format",				sm_format},
   1.660 +	{"FormatEx",			sm_formatex},
   1.661 +	{"GetCharBytes",		GetCharBytes},
   1.662 +	{"IntToString",			sm_numtostr},
   1.663 +	{"IsCharAlpha",			IsCharAlpha},
   1.664 +	{"IsCharLower",			IsCharLower},
   1.665 +	{"IsCharMB",			IsCharMB},
   1.666 +	{"IsCharNumeric",		IsCharNumeric},
   1.667 +	{"IsCharSpace",			IsCharSpace},
   1.668 +	{"IsCharUpper",			IsCharUpper},
   1.669 +	{"ReplaceString",		ReplaceString},
   1.670 +	{"ReplaceStringEx",		ReplaceStringEx},
   1.671 +	{"SplitString",			SplitString},
   1.672 +	{"strlen",				sm_strlen},
   1.673 +	{"StrContains",			sm_contain},
   1.674 +	{"strcmp",				sm_strcmp},
   1.675 +	{"strncmp",				sm_strncmp},
   1.676 +	{"strcopy",				sm_strcopy},
   1.677 +	{"StringToInt",			sm_strconvint},
   1.678 +	{"StringToIntEx",		StringToIntEx},
   1.679 +	{"StringToFloat",		sm_strtofloat},
   1.680 +	{"StringToFloatEx",		StringToFloatEx},
   1.681 +	{"StripQuotes",			StripQuotes},
   1.682 +	{"TrimString",			TrimString},
   1.683 +	{"VFormat",				sm_vformat},
   1.684 +	{NULL,					NULL},
   1.685 +};