plugins/include/string.inc
author Scott Ehlert <ds@alliedmods.net>
Fri Mar 16 20:39:32 2007 +0000 (2007-03-16)
changeset 634 85108309e453
parent 631 76dc2aa558e5
child 680 b5f7893fd4f6
permissions -rw-r--r--
Changed some tags to "any" where appropriate (did I miss anything?)
Plus some other strange things...
     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 writable buffer 
    23  * and maximum length should have the null terminator INCLUDED in the length.  This means 
    24  * that this is valid: StrCopy(string, sizeof(string), ...)
    25  */
    26  
    27 /**
    28  * Calculates the length of a string.
    29  *
    30  * @param str			String to check.
    31  * @return				Length of string, in cells (NOT characters).
    32  */
    33 native strlen(const String:str[]);
    34 
    35 /**
    36  * Tests whether a string is found inside another string.
    37  *
    38  * @param str			String to search in.
    39  * @param substr		Substring to find inside the original string.
    40  * @param caseSensitive	If true (default), search is case sensitive.
    41  *						If false, search is case insensitive.
    42  * @return				-1 on failure (no match found). Any other value
    43  *						indicates a position in the string where the match starts.
    44  */
    45 native StrContains(const String:str[], const String:substr[], bool:caseSensitive=true);
    46 
    47 /**
    48  * Compares two strings lexographically.
    49  *
    50  * @param str1			First string (left).
    51  * @param str2			Second string (right).
    52  * @param caseSensitive	If true (default), comparison is case sensitive.
    53  *						If false, comparison is case insensitive.
    54  * @return				-1 if str1 < str2
    55  *						0 if str1 == str2
    56  *						1 if str1 > str2
    57  */
    58 native StrCompare(const String:str1[], const String:str2[], bool:caseSensitive=true);
    59 
    60 /**
    61  * Returns whether two strings are equal.
    62  *
    63  *
    64  * @param str1			First string (left).
    65  * @param str2			Second string (right).
    66  * @param caseSensitive	If true (default), comparison is case sensitive.
    67  * 						If false, comparison is case insensitive.
    68  * @return				True if equal, false otherwise.
    69  */
    70 stock bool:StrEqual(const String:str1[], const String:str2[], bool:caseSensitive=true)
    71 {
    72 	return (StrCompare(str1, str2, caseSensitive) == 0);
    73 }
    74 
    75 /**
    76  * Copies one string to another string.
    77  * @note If the destination buffer is too small to hold the source string, the 
    78  *       destination will be truncated.
    79  *
    80  * @param dest			Destination string buffer to copy to.
    81  * @param destlen		Destination buffer length (includes null terminator).
    82  * @param source		Source string buffer to copy from.
    83  * @return				Number of cells written.
    84  */
    85 native StrCopy(String:dest[], destLen, const String:source[]);
    86 
    87 /**
    88  * Formats a string according to the SourceMod format rules (see documentation).
    89  *
    90  * @param buffer		Destination string buffer.
    91  * @param maxlength		Maximum length of output string buffer.
    92  * @param format		Formatting rules.
    93  * @param ...			Variable number of format parameters.
    94  * @return				Number of cells written.
    95  */
    96 native Format(String:buffer[], maxlength, const String:format[], any:...);
    97 
    98 /**
    99  * Formats a string according to the SourceMod format rules (see documentation).
   100  * @note This is the same as Format(), except none of the input buffers can overlap the same
   101  *	     memory as the output buffer.  Since this security check is removed, it is slightly faster.
   102  *
   103  * @param buffer		Destination string buffer.
   104  * @param maxlength		Maximum length of output string buffer.
   105  * @param format		Formatting rules.
   106  * @param ...			Variable number of format parameters.
   107  * @return				Number of cells written.
   108  */
   109 native FormatEx(String:buffer[], maxlength, const String:format[], any:...);
   110 
   111 /**
   112  * Formats a string according to the SourceMod format rules (see documentation).
   113  * @note This is the same as Format(), except it grabs parameters from a parent parameter
   114  *       stack, rather than a local.  This is useful for implementing your own variable 
   115  *       argument functions.
   116  *
   117  * @param buffer		Destination string buffer.
   118  * @param maxlength		Maximum length of output string buffer.
   119  * @param format		Formatting rules.
   120  * @param varpos		Argument number which contains the '...' symbol.
   121  *						Note: Arguments start at 1.
   122  * @return 				Number of bytes written.
   123  */
   124 native VFormat(String:buffer[], maxlength, const String:format[], varpos);
   125 
   126 /**
   127  * Converts a string to an integer.
   128  *
   129  * @param str			String to convert.
   130  * @param nBase			Numerical base to use.  10 is default.
   131  * @return				Integer conversion of string, or 0 on failure.
   132  */
   133 native StringToInt(const String:str[], nBase=10);
   134 
   135 /**
   136  * Converts an integer to a string.
   137  *
   138  * @param num			Integer to convert.
   139  * @param str			Buffer to store string in.
   140  * @param maxlength		Maximum length of string buffer.
   141  * @return				Number of cells written to buffer.
   142  */
   143 native IntToString(num, String:str[], maxlength);
   144 
   145 /** 
   146  * Converts a string to a floating point number.
   147  *
   148  * @param str			String to convert to a foat.
   149  * @return				Floating point result, or 0.0 on error.
   150  */
   151 native Float:StringToFloat(const String:str[]);
   152 
   153 /**
   154  * Converts a floating point number to a string.
   155  *
   156  * @param num			Floating point number to convert.
   157  * @param str			Buffer to store string in.
   158  * @param maxlength		Maximum length of string buffer.
   159  * @return				Number of cells written to buffer.
   160  */
   161 native FloatToString(Float:num, String:str[], maxlength);
   162 
   163 /**
   164  * Finds the first "argument" in a string; either a set of space
   165  * terminated characters, or a fully quoted string.  After the 
   166  * argument is found, whitespace is read until the next portion
   167  * of the string is reached.  If nothing remains, -1 is returned.
   168  * Otherwise, the index to the first character is returned.
   169  *
   170  * @param source		Source input string.
   171  * @param arg			Stores argument read from string.
   172  * @param argLen		Maximum length of argument buffer.
   173  * @return				Index to next piece of string, or -1 if none.
   174  */
   175 native StrBreak(const String:source[], String:arg[], argLen);