plugins/include/string.inc
author Scott Ehlert <ds@alliedmods.net>
Tue Mar 06 04:50:30 2007 +0000 (2007-03-06)
changeset 577 39a746736a0f
parent 475 bb11d7eaae0f
child 619 6934bf39171e
permissions -rw-r--r--
Creating/Firing/Canceling events now use CStack to avoid the need to use new when possible
     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@
    23  *   Unless otherwise noted, all string functions which take in a writable buffer and maximum length
    24  * should have the null terminator INCLUDED 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 StrCompare(const String:str1[], const String:str2[], bool:caseSensitive=true);
    60 
    61 /**
    62  * Returns whether two strings are equal.
    63  *
    64  *
    65  * @param str1			First string (left).
    66  * @param str2			Second string (right).
    67  * @param caseSensitive	If true (default), comparison is case sensitive.
    68  * 						If false, comparison is case insensitive.
    69  * @return				True if equal, false otherwise.
    70  */
    71 stock bool:StrEqual(const String:str1[], const String:str2[], bool:caseSensitive=true)
    72 {
    73 	return (StrCompare(str1, str2, caseSensitive) == 0);
    74 }
    75 
    76 /**
    77  * Copies one string to another string.
    78  * NOTE: If the destination buffer is too small to hold the source string,
    79  * the destination will be truncated.
    80  *
    81  * @param dest			Destination string buffer to copy to.
    82  * @param destlen		Destination buffer length (includes null terminator).
    83  * @param source		Source string buffer to copy from.
    84  * @return				Number of cells written.
    85  */
    86 native StrCopy(String:dest[], destLen, const String:source[]);
    87 
    88 /**
    89  * Formats a string according to the SourceMod format rules (see documentation).
    90  *
    91  * @param buffer		Destination string buffer.
    92  * @param maxlength		Maximum length of output string buffer.
    93  * @param format		Formatting rules.
    94  * @param ...			Variable number of format parameters.
    95  * @return				Number of cells written.
    96  */
    97 native Format(String:buffer[], maxlength, const String:format[], {Handle,Float,String,_}:...);
    98 
    99 /**
   100  * Formats a string according to the SourceMod format rules (see documentation).
   101  * @note This is the same as Format(), except none of the input buffers can overlap the same
   102  *	    memory as the output buffer.  Since this security check is removed, it is slightly faster.
   103  *
   104  * @param buffer		Destination string buffer.
   105  * @param maxlength		Maximum length of output string buffer.
   106  * @param format		Formatting rules.
   107  * @param ...			Variable number of format parameters.
   108  * @return				Number of cells written.
   109  */
   110 native FormatEx(String:buffer[], maxlength, const String:format[], {Handle,Float,String,_}:...);
   111 
   112 /**
   113  * Formats a string according to the SourceMod format rules (see documentation).
   114  * @note This is the same as Format(), except it grabs parameters from a parent parameter
   115  * stack, rather than a local.  This is useful for implementing your own variable argument
   116  * functions.
   117  *
   118  * @param buffer		Destination string buffer.
   119  * @param maxlength		Maximum length of output string buffer.
   120  * @param format		Formatting rules.
   121  * @param varpos		Argument number which contains the '...' symbol.
   122  *						Note: Arguments start at 1.
   123  * @return 				Number of bytes written.
   124  */
   125 native VFormat(String:buffer[], maxlength, const String:format[], varpos);
   126 
   127 /**
   128  * Converts a string to an integer.
   129  *
   130  * @param str			String to convert.
   131  * @param nBase			Numerical base to use.  10 is default.
   132  * @return				Integer conversion of string, or 0 on failure.
   133  */
   134 native StringToInt(const String:str[], nBase=10);
   135 
   136 /**
   137  * Converts an integer to a string.
   138  *
   139  * @param num			Integer to convert.
   140  * @param str			Buffer to store string in.
   141  * @param maxlength		Maximum length of string buffer.
   142  * @return				Number of cells written to buffer.
   143  */
   144 native IntToString(num, String:str[], maxlength);
   145 
   146 /** 
   147  * Converts a string to a floating point number.
   148  *
   149  * @param str			String to convert to a foat.
   150  * @return				Floating point result, or 0.0 on error.
   151  */
   152 native Float:StringToFloat(const String:str[]);
   153 
   154 /**
   155  * Converts a floating point number to a string.
   156  *
   157  * @param num			Floating point number to convert.
   158  * @param str			Buffer to store string in.
   159  * @param maxlength		Maximum length of string buffer.
   160  * @return				Number of cells written to buffer.
   161  */
   162 native FloatToString(Float:num, String:str[], maxlength);