plugins/include/string.inc
author David Anderson <dvander@alliedmods.net>
Fri Aug 15 05:22:26 2008 +0000 (2008-08-15)
changeset 2226 d10b38524be3
parent 2070 da14ca036929
child 2289 44045ead61cd
permissions -rw-r--r--
Landed sourcepawn-1.2. The big changes:

1) JIT compilation/optimization now occurs per-function, and only when functions are first used. We're now officially a whole-method JIT rather than an AOT compiler (albiet, still a simple JIT). This has two implications: Functions are now much better abstracted internally, and loading a plugin is now much less expensive. If a function contains calls to other functions, THOSE functions are only compiled when they're invoked as well.

2) I've removed debug mode. We always show full backtraces now, as there was a very cheap way to implement this which really cleaned up everything. This is great for a number of reasons -- there's less code, the JIT is better designed, we don't need to relocate debug tables, and best of all we no longer have to tell users to enable debug mode at their own expense.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     5  * =============================================================================
     6  *
     7  * This file is part of the SourceMod/SourcePawn SDK.
     8  *
     9  * This program is free software; you can redistribute it and/or modify it under
    10  * the terms of the GNU General Public License, version 3.0, as published by the
    11  * Free Software Foundation.
    12  * 
    13  * This program is distributed in the hope that it will be useful, but WITHOUT
    14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    16  * details.
    17  *
    18  * You should have received a copy of the GNU General Public License along with
    19  * this program.  If not, see <http://www.gnu.org/licenses/>.
    20  *
    21  * As a special exception, AlliedModders LLC gives you permission to link the
    22  * code of this program (as well as its derivative works) to "Half-Life 2," the
    23  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    24  * by the Valve Corporation.  You must obey the GNU General Public License in
    25  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    26  * this exception to all derivative works.  AlliedModders LLC defines further
    27  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    28  * or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32  
    33 #if defined _string_included
    34  #endinput
    35 #endif
    36 #define _string_included
    37 
    38 /**
    39  * @global Unless otherwise noted, all string functions which take in a 
    40  * writable buffer and maximum length should have the null terminator INCLUDED
    41  * in the length.  This means that this is valid: 
    42  * strcopy(string, sizeof(string), ...)
    43  */
    44  
    45 /**
    46  * Calculates the length of a string.
    47  *
    48  * @param str			String to check.
    49  * @return				Number of valid character bytes in the string.
    50  */
    51 native strlen(const String:str[]);
    52 
    53 /**
    54  * Tests whether a string is found inside another string.
    55  *
    56  * @param str			String to search in.
    57  * @param substr		Substring to find inside the original string.
    58  * @param caseSensitive	If true (default), search is case sensitive.
    59  *						If false, search is case insensitive.
    60  * @return				-1 on failure (no match found). Any other value
    61  *						indicates a position in the string where the match starts.
    62  */
    63 native StrContains(const String:str[], const String:substr[], bool:caseSensitive=true);
    64 
    65 /**
    66  * Compares two strings lexographically.
    67  *
    68  * @param str1			First string (left).
    69  * @param str2			Second string (right).
    70  * @param caseSensitive	If true (default), comparison is case sensitive.
    71  *						If false, comparison is case insensitive.
    72  * @return				-1 if str1 < str2
    73  *						0 if str1 == str2
    74  *						1 if str1 > str2
    75  */
    76 native strcmp(const String:str1[], const String:str2[], bool:caseSensitive=true);
    77 
    78 /**
    79  * Compares two strings parts lexographically.
    80  *
    81  * @param str1			First string (left).
    82  * @param str2			Second string (right).
    83  * @param num			Number of characters to compare.
    84  * @param caseSensitive	If true (default), comparison is case sensitive.
    85  *						If false, comparison is case insensitive.
    86  * @return				-1 if str1 < str2
    87  *						0 if str1 == str2
    88  *						1 if str1 > str2
    89  */
    90 native strncmp(const String:str1[], const String:str2[], num, bool:caseSensitive=true);
    91 
    92 /**
    93  * Backwards compatible stock - StrCompare is now strcmp
    94  * @deprecated		Renamed to strcmp
    95  */
    96 #pragma deprecated Use strcmp() instead
    97 stock StrCompare(const String:str1[], const String:str2[], bool:caseSensitive=true)
    98 {
    99 	return strcmp(str1, str2, caseSensitive);
   100 }
   101 
   102 /**
   103  * Returns whether two strings are equal.
   104  *
   105  * @param str1			First string (left).
   106  * @param str2			Second string (right).
   107  * @param caseSensitive	If true (default), comparison is case sensitive.
   108  * 						If false, comparison is case insensitive.
   109  * @return				True if equal, false otherwise.
   110  */
   111 stock bool:StrEqual(const String:str1[], const String:str2[], bool:caseSensitive=true)
   112 {
   113 	return (strcmp(str1, str2, caseSensitive) == 0);
   114 }
   115 
   116 /**
   117  * Copies one string to another string.
   118  * @note If the destination buffer is too small to hold the source string, the 
   119  *       destination will be truncated.
   120  *
   121  * @param dest			Destination string buffer to copy to.
   122  * @param destlen		Destination buffer length (includes null terminator).
   123  * @param source		Source string buffer to copy from.
   124  * @return				Number of cells written.
   125  */
   126 native strcopy(String:dest[], destLen, const String:source[]);
   127 
   128 /**
   129  * Backwards compatibility stock - use strcopy
   130  * @deprecated			Renamed to strcopy
   131  */
   132 #pragma deprecated Use strcopy() instead
   133 stock StrCopy(String:dest[], destLen, const String:source[])
   134 {
   135 	return strcopy(dest, destLen, source);
   136 }
   137 
   138 /**
   139  * Formats a string according to the SourceMod format rules (see documentation).
   140  *
   141  * @param buffer		Destination string buffer.
   142  * @param maxlength		Maximum length of output string buffer.
   143  * @param format		Formatting rules.
   144  * @param ...			Variable number of format parameters.
   145  * @return				Number of cells written.
   146  */
   147 native Format(String:buffer[], maxlength, const String:format[], any:...);
   148 
   149 /**
   150  * Formats a string according to the SourceMod format rules (see documentation).
   151  * @note This is the same as Format(), except none of the input buffers can 
   152  *       overlap the same memory as the output buffer.  Since this security 
   153  *       check is removed, it is slightly faster.
   154  *
   155  * @param buffer		Destination string buffer.
   156  * @param maxlength		Maximum length of output string buffer.
   157  * @param format		Formatting rules.
   158  * @param ...			Variable number of format parameters.
   159  * @return				Number of cells written.
   160  */
   161 native FormatEx(String:buffer[], maxlength, const String:format[], any:...);
   162 
   163 /**
   164  * Formats a string according to the SourceMod format rules (see documentation).
   165  * @note This is the same as Format(), except it grabs parameters from a 
   166  *       parent parameter stack, rather than a local.  This is useful for 
   167  *       implementing your own variable argument functions.
   168  *
   169  * @param buffer		Destination string buffer.
   170  * @param maxlength		Maximum length of output string buffer.
   171  * @param format		Formatting rules.
   172  * @param varpos		Argument number which contains the '...' symbol.
   173  *						Note: Arguments start at 1.
   174  * @return 				Number of bytes written.
   175  */
   176 native VFormat(String:buffer[], maxlength, const String:format[], varpos);
   177 
   178 /**
   179  * Converts a string to an integer.
   180  *
   181  * @param str			String to convert.
   182  * @param nBase			Numerical base to use.  10 is default.
   183  * @return				Integer conversion of string, or 0 on failure.
   184  */
   185 native StringToInt(const String:str[], nBase=10);
   186 
   187 /**
   188  * Converts a string to an integer with some more options.
   189  *
   190  * @param str			String to convert.
   191  * @param result		Variable to store the result in.
   192  * @param nBase			Numerical base to use.  10 is default.
   193  * @return				Number of characters consumed.
   194  */
   195 native StringToIntEx(const String:str[], &result, nBase=10);
   196 
   197 /**
   198  * Converts an integer to a string.
   199  *
   200  * @param num			Integer to convert.
   201  * @param str			Buffer to store string in.
   202  * @param maxlength		Maximum length of string buffer.
   203  * @return				Number of cells written to buffer.
   204  */
   205 native IntToString(num, String:str[], maxlength);
   206 
   207 /** 
   208  * Converts a string to a floating point number.
   209  *
   210  * @param str			String to convert to a foat.
   211  * @return				Floating point result, or 0.0 on error.
   212  */
   213 native Float:StringToFloat(const String:str[]);
   214 
   215 /** 
   216  * Converts a string to a floating point number with some more options.
   217  *
   218  * @param str			String to convert to a foat.
   219  * @param result		Variable to store result in.
   220  * @return				Number of characters consumed.
   221  */
   222 native StringToFloatEx(const String:str[], &Float:result);
   223 
   224 /**
   225  * Converts a floating point number to a string.
   226  *
   227  * @param num			Floating point number to convert.
   228  * @param str			Buffer to store string in.
   229  * @param maxlength		Maximum length of string buffer.
   230  * @return				Number of cells written to buffer.
   231  */
   232 native FloatToString(Float:num, String:str[], maxlength);
   233 
   234 /**
   235  * Finds the first "argument" in a string; either a set of space
   236  * terminated characters, or a fully quoted string.  After the 
   237  * argument is found, whitespace is read until the next portion
   238  * of the string is reached.  If nothing remains, -1 is returned.
   239  * Otherwise, the index to the first character is returned.
   240  *
   241  * @param source		Source input string.
   242  * @param arg			Stores argument read from string.
   243  * @param argLen		Maximum length of argument buffer.
   244  * @return				Index to next piece of string, or -1 if none.
   245  */
   246 native BreakString(const String:source[], String:arg[], argLen);
   247 
   248 /**
   249  * Backwards compatibility stock - use BreakString
   250  * @deprecated			Renamed to BreakString.
   251  */
   252 #pragma deprecated Use BreakString() instead
   253 stock StrBreak(const String:source[], String:arg[], argLen)
   254 {
   255 	return BreakString(source, arg, argLen);
   256 }
   257 
   258 /**
   259  * Removes whitespace characters from the beginning and end of a string.
   260  *
   261  * @param str			The string to trim.
   262  * @return				Number of bytes written (UTF-8 safe).
   263  */
   264 native TrimString(String:str[]);
   265 
   266 /**
   267  * Returns text in a string up until a certain character sequence is reached.
   268  *
   269  * @param source		Source input string.
   270  * @param split			A string which specifies a search point to break at.
   271  * @param part			Buffer to store string part.
   272  * @param partLen		Maximum length of the string part buffer.
   273  * @return				-1 if no match was found; otherwise, an index into source
   274  *						marking the first index after the searched text.  The
   275  *						index is always relative to the start of the input string.
   276  */
   277 native SplitString(const String:source[], const String:split[], String:part[], partLen);
   278 
   279 /**
   280  * Given a string, replaces all occurrences of a search string with a 
   281  * replacement string.
   282  *
   283  * @param text			String to perform search and replacements on.
   284  * @param maxlength		Maximum length of the string buffer.
   285  * @param search		String to search for.
   286  * @param replace		String to replace the search string with.
   287  * @return				Number of replacements that were performed.
   288  */
   289 native ReplaceString(String:text[], maxlength, const String:search[], const String:replace[]);
   290 
   291 /**
   292  * Given a string, replaces the first occurrence of a search string with a 
   293  * replacement string.
   294  *
   295  * @param text			String to perform search and replacements on.
   296  * @param maxlength		Maximum length of the string buffer.
   297  * @param search		String to search for.
   298  * @param replace		String to replace the search string with.
   299  * @param searchLen		If higher than -1, its value will be used instead of
   300  *						a strlen() call on the search parameter.
   301  * @param replaceLen	If higher than -1, its value will be used instead of
   302  *						a strlen() call on the replace parameter.
   303  * @return				Index into the buffer (relative to the start) from where
   304  *						the last replacement ended, or -1 if no replacements were
   305  *						made.
   306  */
   307 native ReplaceStringEx(String:text[], maxlength, const String:search[], const String:replace[], searchLen=-1, replaceLen=-1);
   308 
   309 /** 
   310  * Returns the number of bytes a character is using.  This is
   311  * for multi-byte characters (UTF-8).  For normal ASCII characters,
   312  * this will return 1.
   313  *
   314  * @param source		Source input string.
   315  * @return				Number of bytes the current character uses.
   316  */
   317 native GetCharBytes(const String:source[]);
   318 
   319 /**
   320  * Returns whether a character is an ASCII alphabet character.
   321  *
   322  * @note Multi-byte characters will always return false.
   323  *
   324  * @param char			Character to test.
   325  * @return				True if character is alphabetical, otherwise false.
   326  */
   327 native bool:IsCharAlpha(chr);
   328 
   329 /**
   330  * Returns whether a character is numeric.
   331  *
   332  * @note Multi-byte characters will always return false.
   333  *
   334  * @param char			Character to test.
   335  * @return				True if character is numeric, otherwise false.
   336  */
   337 native bool:IsCharNumeric(chr);
   338 
   339 /**
   340  * Returns whether a character is whitespace.
   341  *
   342  * @note Multi-byte characters will always return false.
   343  *
   344  * @param char			Character to test.
   345  * @return				True if character is whitespace, otherwise false.
   346  */
   347 native bool:IsCharSpace(chr);
   348 
   349 /**
   350  * Returns if a character is multi-byte or not.
   351  *
   352  * @param char			Character to test.
   353  * @return				0 for a normal 7-bit ASCII character,
   354  *						otherwise number of bytes in multi-byte character.
   355  */
   356 native IsCharMB(chr);
   357 
   358 /**
   359  * Returns whether an alphabetic character is uppercase.
   360  *
   361  * @note Multi-byte characters will always return false.
   362  *
   363  * @param char			Character to test.
   364  * @return				True if character is uppercase, otherwise false.
   365  */
   366 native bool:IsCharUpper(chr);
   367 
   368 /**
   369  * Returns whether an alphabetic character is lowercase.
   370  *
   371  * @note Multi-byte characters will always return false.
   372  *
   373  * @param char			Character to test.
   374  * @return				True if character is lowercase, otherwise false.
   375  */
   376 native bool:IsCharLower(chr);
   377 
   378 /**
   379  * Strips a quote pair off a string if it exists.  That is, the following 
   380  * replace rule is applied once:  $"(.*)"^ -> $\1^
   381  *
   382  * Note that the leading and trailing quotes will only be removed if both 
   383  * exist.  Otherwise, the string is left unmodified.  This function should 
   384  * be considered O(k) (all characters get shifted down).
   385  *
   386  * @param text			String to modify (in place).
   387  * @return				True if string was modified, false if there was no 
   388  *						set of quotes.
   389  */
   390 native bool:StripQuotes(String:text[]);
   391 
   392 /**
   393  * Returns an uppercase character to a lowercase character.
   394  *
   395  * @param chr			Characer to convert.
   396  * @return				Lowercase character on success, 
   397  *						no change on failure.
   398  */
   399 stock CharToUpper(chr)
   400 {
   401 	if (IsCharLower(chr))
   402 	{
   403 		return (chr & ~(1<<5));
   404 	}
   405 	return chr;
   406 }
   407 
   408 /**
   409  * Returns a lowercase character to an uppercase character.
   410  *
   411  * @param chr			Characer to convert.
   412  * @return				Uppercase character on success, 
   413  *						no change on failure.
   414  */
   415 stock CharToLower(chr)
   416 {
   417 	if (IsCharUpper(chr))
   418 	{
   419 		return (chr | (1<<5));
   420 	}
   421 	return chr;
   422 }
   423 
   424 /**
   425  * Finds the first occurrence of a character in a string.
   426  *
   427  * @param str			String.
   428  * @param c				Character to search for.
   429  * @param reverse		False (default) to search forward, true to search 
   430  *						backward.
   431  * @return				The index of the first occurrence of the character 
   432  * 						in the string, or -1 if the character was not found.
   433  */
   434 stock FindCharInString(const String:str[], c, bool:reverse = false)
   435 {
   436 	new i, len
   437 
   438 	len = strlen(str);
   439 	
   440 	if (!reverse)
   441 	{
   442 		for (i = 0; i < len; i++)
   443 		{
   444 			if (str[i] == c)
   445 			{
   446 				return i;
   447 			}
   448 		}
   449 	}
   450 	else
   451 	{
   452 		for (i = len - 1; i >= 0; i--)
   453 		{
   454 			if (str[i] == c)
   455 			{
   456 				return i;
   457 			}
   458 		}
   459 	}
   460 
   461 	return -1;
   462 }
   463 
   464 /**
   465  * Concatenates one string onto another.
   466  *
   467  * @param buffer		String to append to.
   468  * @param maxlength		Maximum length of entire buffer.
   469  * @param source		Source string to concatenate.
   470  * @return				Number of bytes written.
   471  */
   472 stock StrCat(String:buffer[], maxlength, const String:source[])
   473 {
   474 	new len = strlen(buffer);
   475 	if (len >= maxlength)
   476 	{
   477 		return 0;
   478 	}
   479 	
   480 	return Format(buffer[len], maxlength-len, "%s", source);
   481 }
   482 
   483 /**
   484  * Breaks a string into pieces and stores each piece into an array of buffers.
   485  *
   486  * @param text				The string to split.
   487  * @param split				The string to use as a split delimiter.
   488  * @param buffers			An array of string buffers (2D array).
   489  * @param maxStrings		Number of string buffers (first dimension size).
   490  * @param maxStringLength	Maximum length of each string buffer.
   491  * @return					Number of strings retrieved.
   492  */
   493 stock ExplodeString(const String:text[], const String:split[], String:buffers[][], maxStrings, maxStringLength)
   494 {
   495 	new reloc_idx, idx, total;
   496 	
   497 	if (maxStrings < 1 || split[0] == '\0')
   498 	{
   499 		return 0;
   500 	}
   501 	
   502 	while ((idx = SplitString(text[reloc_idx], split, buffers[total], maxStringLength)) != -1)
   503 	{
   504 		reloc_idx += idx;
   505 		if (text[reloc_idx] == '\0')
   506 		{
   507 			break;
   508 		}
   509 		if (++total >= maxStrings)
   510 		{
   511 			return total;
   512 		}
   513 	}
   514 	
   515 	if (text[reloc_idx] != '\0' && total <= maxStrings - 1)
   516 	{
   517 		strcopy(buffers[total++], maxStringLength, text[reloc_idx]);
   518 	}
   519 	
   520 	return total;
   521 }
   522 
   523 /**
   524  * Joins an array of strings into one string, with a "join" string inserted in
   525  * between each given string.  This function complements ExplodeString.
   526  *
   527  * @param strings		An array of strings.
   528  * @param numStrings	Number of strings in the array.
   529  * @param join			The join string to insert between each string.
   530  * @param buffer		Output buffer to write the joined string to.
   531  * @param maxLength		Maximum length of the output buffer.
   532  * @return				Number of bytes written to the output buffer.
   533  */
   534 stock ImplodeStrings(const String:strings[][], numStrings, const String:join[], String:buffer[], maxLength)
   535 {
   536 	new total, length, part_length;
   537 	new join_length = strlen(join);
   538 	for (new i=0; i<numStrings; i++)
   539 	{
   540 		length = strcopy(buffer[total], maxLength-total, strings[i]);
   541 		total += length;
   542 		if (length < part_length)
   543 		{
   544 			break;
   545 		}
   546 		if (i != numStrings - 1)
   547 		{
   548 			length = strcopy(buffer[total], maxLength-total, join);
   549 			total += length;
   550 			if (length < join_length)
   551 			{
   552 				break;
   553 			}
   554 		}
   555 	}
   556 	return total;
   557 }