plugins/include/string.inc
author David Anderson <dvander@alliedmods.net>
Sun May 06 23:12:54 2007 +0000 (2007-05-06)
changeset 748 0433498ab05c
parent 681 02f6c1c5fd06
child 749 48253e56579d
permissions -rw-r--r--
more string natives
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be used 
     8  * or modified under the Terms and Conditions of its License Agreement, which is found 
     9  * in LICENSE.txt.  The Terms and Conditions for making SourceMod extensions/plugins 
    10  * may change at any time.  To view the latest information, see:
    11  *   http://www.sourcemod.net/license.php
    12  *
    13  * Version: $Id$
    14  */
    15  
    16 #if defined _string_included
    17  #endinput
    18 #endif
    19 #define _string_included
    20 
    21 /**
    22  * @global Unless otherwise noted, all string functions which take in a 
    23  * writable buffer and maximum length should have the null terminator INCLUDED
    24  * in the length.  This means that this is valid: 
    25  * StrCopy(string, sizeof(string), ...)
    26  */
    27  
    28 /**
    29  * Calculates the length of a string.
    30  *
    31  * @param str			String to check.
    32  * @return				Length of string, in cells (NOT characters).
    33  */
    34 native strlen(const String:str[]);
    35 
    36 /**
    37  * Tests whether a string is found inside another string.
    38  *
    39  * @param str			String to search in.
    40  * @param substr		Substring to find inside the original string.
    41  * @param caseSensitive	If true (default), search is case sensitive.
    42  *						If false, search is case insensitive.
    43  * @return				-1 on failure (no match found). Any other value
    44  *						indicates a position in the string where the match starts.
    45  */
    46 native StrContains(const String:str[], const String:substr[], bool:caseSensitive=true);
    47 
    48 /**
    49  * Compares two strings lexographically.
    50  *
    51  * @param str1			First string (left).
    52  * @param str2			Second string (right).
    53  * @param caseSensitive	If true (default), comparison is case sensitive.
    54  *						If false, comparison is case insensitive.
    55  * @return				-1 if str1 < str2
    56  *						0 if str1 == str2
    57  *						1 if str1 > str2
    58  */
    59 native strcmp(const String:str1[], const String:str2[], bool:caseSensitive=true);
    60 
    61 /**
    62  * Compares two strings parts lexographically.
    63  *
    64  * @param str1			First string (left).
    65  * @param str2			Second string (right).
    66  * @param num			Number of characters to compare.
    67  * @param caseSensitive	If true (default), comparison is case sensitive.
    68  *						If false, comparison is case insensitive.
    69  * @return				-1 if str1 < str2
    70  *						0 if str1 == str2
    71  *						1 if str1 > str2
    72  */
    73 native strncmp(const String:str1[], const String:str2[], num, bool:caseSensitive=true);
    74 
    75 /**
    76  * Backwards compatible stock - StrCompare is now strcmp
    77  * @deprecated		Renamed to strcmp
    78  */
    79 stock StrCompare(const String:str1[], const String:str2[], bool:caseSensitive=true)
    80 {
    81 	return strcmp(str1, str2, caseSensitive);
    82 }
    83 
    84 /**
    85  * Returns whether two strings are equal.
    86  *
    87  *
    88  * @param str1			First string (left).
    89  * @param str2			Second string (right).
    90  * @param caseSensitive	If true (default), comparison is case sensitive.
    91  * 						If false, comparison is case insensitive.
    92  * @return				True if equal, false otherwise.
    93  */
    94 stock bool:StrEqual(const String:str1[], const String:str2[], bool:caseSensitive=true)
    95 {
    96 	return (StrCompare(str1, str2, caseSensitive) == 0);
    97 }
    98 
    99 /**
   100  * Copies one string to another string.
   101  * @note If the destination buffer is too small to hold the source string, the 
   102  *       destination will be truncated.
   103  *
   104  * @param dest			Destination string buffer to copy to.
   105  * @param destlen		Destination buffer length (includes null terminator).
   106  * @param source		Source string buffer to copy from.
   107  * @return				Number of cells written.
   108  */
   109 native strcopy(String:dest[], destLen, const String:source[]);
   110 
   111 /**
   112  * Backwards compatibility stock - use strcopy
   113  * @deprecated			Renamed to strcopy
   114  */
   115 stock StrCopy(String:dest[], destLen, const String:source[])
   116 {
   117 	return strcopy(dest, destLen, source);
   118 }
   119 
   120 /**
   121  * Formats a string according to the SourceMod format rules (see documentation).
   122  *
   123  * @param buffer		Destination string buffer.
   124  * @param maxlength		Maximum length of output string buffer.
   125  * @param format		Formatting rules.
   126  * @param ...			Variable number of format parameters.
   127  * @return				Number of cells written.
   128  */
   129 native Format(String:buffer[], maxlength, const String:format[], any:...);
   130 
   131 /**
   132  * Formats a string according to the SourceMod format rules (see documentation).
   133  * @note This is the same as Format(), except none of the input buffers can 
   134  *       overlap the same memory as the output buffer.  Since this security 
   135  *       check is removed, it is slightly faster.
   136  *
   137  * @param buffer		Destination string buffer.
   138  * @param maxlength		Maximum length of output string buffer.
   139  * @param format		Formatting rules.
   140  * @param ...			Variable number of format parameters.
   141  * @return				Number of cells written.
   142  */
   143 native FormatEx(String:buffer[], maxlength, const String:format[], any:...);
   144 
   145 /**
   146  * Formats a string according to the SourceMod format rules (see documentation).
   147  * @note This is the same as Format(), except it grabs parameters from a 
   148  *       parent parameter stack, rather than a local.  This is useful for 
   149  *       implementing your own variable argument functions.
   150  *
   151  * @param buffer		Destination string buffer.
   152  * @param maxlength		Maximum length of output string buffer.
   153  * @param format		Formatting rules.
   154  * @param varpos		Argument number which contains the '...' symbol.
   155  *						Note: Arguments start at 1.
   156  * @return 				Number of bytes written.
   157  */
   158 native VFormat(String:buffer[], maxlength, const String:format[], varpos);
   159 
   160 /**
   161  * Converts a string to an integer.
   162  *
   163  * @param str			String to convert.
   164  * @param nBase			Numerical base to use.  10 is default.
   165  * @return				Integer conversion of string, or 0 on failure.
   166  */
   167 native StringToInt(const String:str[], nBase=10);
   168 
   169 /**
   170  * Converts an integer to a string.
   171  *
   172  * @param num			Integer to convert.
   173  * @param str			Buffer to store string in.
   174  * @param maxlength		Maximum length of string buffer.
   175  * @return				Number of cells written to buffer.
   176  */
   177 native IntToString(num, String:str[], maxlength);
   178 
   179 /** 
   180  * Converts a string to a floating point number.
   181  *
   182  * @param str			String to convert to a foat.
   183  * @return				Floating point result, or 0.0 on error.
   184  */
   185 native Float:StringToFloat(const String:str[]);
   186 
   187 /**
   188  * Converts a floating point number to a string.
   189  *
   190  * @param num			Floating point number to convert.
   191  * @param str			Buffer to store string in.
   192  * @param maxlength		Maximum length of string buffer.
   193  * @return				Number of cells written to buffer.
   194  */
   195 native FloatToString(Float:num, String:str[], maxlength);
   196 
   197 /**
   198  * Finds the first "argument" in a string; either a set of space
   199  * terminated characters, or a fully quoted string.  After the 
   200  * argument is found, whitespace is read until the next portion
   201  * of the string is reached.  If nothing remains, -1 is returned.
   202  * Otherwise, the index to the first character is returned.
   203  *
   204  * @param source		Source input string.
   205  * @param arg			Stores argument read from string.
   206  * @param argLen		Maximum length of argument buffer.
   207  * @return				Index to next piece of string, or -1 if none.
   208  */
   209 native StrBreak(const String:source[], String:arg[], argLen);
   210 
   211 /** 
   212  * Returns the number of bytes a character is using.  This is
   213  * for multi-byte characters (UTF-8).  For normal ASCII characters,
   214  * this will return 1.
   215  *
   216  * @param source		Source input string.
   217  * @return				Number of bytes the current character uses.
   218  */
   219 native GetCharBytes(const String:source[]);
   220 
   221 /**
   222  * Returns whether a character is an ASCII alphabet character.
   223  *
   224  * @note Multi-byte characters will always return false.
   225  *
   226  * @param char			Character to test.
   227  * @return				True if character is alphabetical, otherwise false.
   228  */
   229 native bool:IsCharAlpha(chr);
   230 
   231 /**
   232  * Returns whether a character is numeric.
   233  *
   234  * @note Multi-byte characters will always return false.
   235  *
   236  * @param char			Character to test.
   237  * @return				True if character is numeric, otherwise false.
   238  */
   239 native bool:IsCharNumeric(chr);
   240 
   241 /**
   242  * Returns whether a character is whitespace.
   243  *
   244  * @note Multi-byte characters will always return false.
   245  *
   246  * @param char			Character to test.
   247  * @return				True if character is whitespace, otherwise false.
   248  */
   249 native bool:IsCharSpace(chr);
   250 
   251 /**
   252  * Returns if a character is multi-byte or not.
   253  *
   254  * @param char			Character to test.
   255  * @return				0 for a normal 7-bit ASCII character,
   256  *						otherwise number of bytes in multi-byte character.
   257  */
   258 native IsCharMB(chr);
   259 
   260 /**
   261  * Returns whether an alphabetic character is uppercase.
   262  *
   263  * @note Multi-byte characters will always return false.
   264  *
   265  * @param char			Character to test.
   266  * @return				True if character is uppercase, otherwise false.
   267  */
   268 native bool:IsCharUpper(chr);
   269 
   270 /**
   271  * Returns whether an alphabetic character is lowercase.
   272  *
   273  * @note Multi-byte characters will always return false.
   274  *
   275  * @param char			Character to test.
   276  * @return				True if character is lowercase, otherwise false.
   277  */
   278 native bool:IsCharLower(chr);
   279 
   280 /**
   281  * Returns an uppercase character to a lowercase character.
   282  *
   283  * @param chr			Characer to convert.
   284  * @return				Lowercase character on success, 
   285  *						no change on failure.
   286  */
   287 stock CharToUpper(chr)
   288 {
   289 	if (IsCharLower(chr))
   290 	{
   291 		return (chr & ~(1<<5));
   292 	}
   293 	return chr;
   294 }
   295 
   296 /**
   297  * Returns a lowercase character to an uppercase character.
   298  *
   299  * @param chr			Characer to convert.
   300  * @return				Uppercase character on success, 
   301  *						no change on failure.
   302  */
   303 stock CharToLower(chr)
   304 {
   305 	if (IsCharUpper(chr))
   306 	{
   307 		return (chr | (1<<5));
   308 	}
   309 	return chr;
   310 }
   311 
   312