core/smn_string.cpp
changeset 830 97cd070a7ffa
parent 748 0433498ab05c
child 833 732bedfcbe3e
     1.1 --- a/core/smn_string.cpp	Tue May 22 03:39:18 2007 +0000
     1.2 +++ b/core/smn_string.cpp	Tue May 22 03:42:04 2007 +0000
     1.3 @@ -238,7 +238,7 @@
     1.4  }
     1.5  
     1.6  /* :TODO: make this UTF8 safe */
     1.7 -static cell_t StrBreak(IPluginContext *pContext, const cell_t *params)
     1.8 +static cell_t BreakString(IPluginContext *pContext, const cell_t *params)
     1.9  {
    1.10  	const char *input;
    1.11  	char *out;
    1.12 @@ -383,7 +383,7 @@
    1.13  	return bytes;
    1.14  }
    1.15  
    1.16 -static cell_t IsCharUpper(IPluginContext  *pContext, const cell_t *params)
    1.17 +static cell_t IsCharUpper(IPluginContext *pContext, const cell_t *params)
    1.18  {
    1.19  	char chr = params[1];
    1.20  
    1.21 @@ -395,7 +395,7 @@
    1.22  	return isupper(chr);
    1.23  }
    1.24  
    1.25 -static cell_t IsCharLower(IPluginContext  *pContext, const cell_t *params)
    1.26 +static cell_t IsCharLower(IPluginContext *pContext, const cell_t *params)
    1.27  {
    1.28  	char chr = params[1];
    1.29  
    1.30 @@ -407,6 +407,41 @@
    1.31  	return islower(chr);
    1.32  }
    1.33  
    1.34 +static cell_t TrimString(IPluginContext *pContext, const cell_t *params)
    1.35 +{
    1.36 +	char *str;
    1.37 +	pContext->LocalToString(params[1], &str);
    1.38 +
    1.39 +	size_t chars = strlen(str);
    1.40 +
    1.41 +	if (chars == 0)
    1.42 +	{
    1.43 +		return 0;
    1.44 +	}
    1.45 +
    1.46 +	char *end = str + chars - 1;
    1.47 +
    1.48 +	/* Iterate backwards through string until we reach first non-whitespace char */
    1.49 +	while (end >= str && IsWhitespace(end))
    1.50 +	{
    1.51 +		end--;
    1.52 +	}
    1.53 +
    1.54 +	/* Replace first whitespace char (at the end) with null terminator */
    1.55 +	*(end + 1) = '\0';
    1.56 +
    1.57 +	/* Iterate forwards through string until first non-whitespace char is reached */
    1.58 +	while (IsWhitespace(str))
    1.59 +	{
    1.60 +		str++;
    1.61 +	}
    1.62 +
    1.63 +	size_t bytes;
    1.64 +	pContext->StringToLocalUTF8(params[1], chars + 1, str, &bytes);
    1.65 +
    1.66 +	return bytes;
    1.67 +}
    1.68 +
    1.69  REGISTER_NATIVES(basicStrings)
    1.70  {
    1.71  	{"GetCharBytes",		GetCharBytes},
    1.72 @@ -418,7 +453,8 @@
    1.73  	{"IsCharSpace",			IsCharSpace},
    1.74  	{"IsCharUpper",			IsCharUpper},
    1.75  	{"strlen",				sm_strlen},
    1.76 -	{"StrBreak",			StrBreak},
    1.77 +	{"StrBreak",			BreakString},		/* Backwards compat shim */
    1.78 +	{"BreakString",			BreakString},
    1.79  	{"StrContains",			sm_contain},
    1.80  	{"strcmp",				sm_strcmp},
    1.81  	{"StrCompare",			sm_strcmp},			/* Backwards compat shim */
    1.82 @@ -431,5 +467,6 @@
    1.83  	{"Format",				sm_format},
    1.84  	{"FormatEx",			sm_formatex},
    1.85  	{"VFormat",				sm_vformat},
    1.86 +	{"TrimString",			TrimString},
    1.87  	{NULL,					NULL},
    1.88  };