plugins/include/string.inc
author Scott Ehlert <ds@alliedmods.net>
Tue May 01 05:29:37 2007 +0000 (2007-05-01)
changeset 714 93b19876407e
parent 681 02f6c1c5fd06
child 748 0433498ab05c
permissions -rw-r--r--
- Removed GetConVarMin/Max() and replaced them with GetConVarBounds()
- Added SetConVarBounds() to set convar constraints after convar has already been created
dvander@233
     1
/**
ds@475
     2
 * vim: set ts=4 :
dvander@374
     3
 * ===============================================================
dvander@374
     4
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
dvander@374
     5
 * ===============================================================
dvander@374
     6
 *
dvander@374
     7
 *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be used 
dvander@374
     8
 * or modified under the Terms and Conditions of its License Agreement, which is found 
dvander@374
     9
 * in LICENSE.txt.  The Terms and Conditions for making SourceMod extensions/plugins 
dvander@374
    10
 * may change at any time.  To view the latest information, see:
dvander@374
    11
 *   http://www.sourcemod.net/license.php
dvander@374
    12
 *
dvander@374
    13
 * Version: $Id$
dvander@233
    14
 */
dvander@374
    15
 
dvander@233
    16
#if defined _string_included
dvander@233
    17
 #endinput
dvander@233
    18
#endif
dvander@233
    19
#define _string_included
dvander@233
    20
dvander@233
    21
/**
dvander@619
    22
 * @global Unless otherwise noted, all string functions which take in a writable buffer 
dvander@619
    23
 * and maximum length should have the null terminator INCLUDED in the length.  This means 
dvander@619
    24
 * that this is valid: StrCopy(string, sizeof(string), ...)
dvander@233
    25
 */
dvander@631
    26
 
dvander@233
    27
/**
dvander@233
    28
 * Calculates the length of a string.
dvander@233
    29
 *
ds@475
    30
 * @param str			String to check.
ds@475
    31
 * @return				Length of string, in cells (NOT characters).
dvander@233
    32
 */
dvander@233
    33
native strlen(const String:str[]);
dvander@233
    34
dvander@233
    35
/**
dvander@233
    36
 * Tests whether a string is found inside another string.
dvander@233
    37
 *
dvander@233
    38
 * @param str			String to search in.
dvander@233
    39
 * @param substr		Substring to find inside the original string.
dvander@233
    40
 * @param caseSensitive	If true (default), search is case sensitive.
ds@475
    41
 *						If false, search is case insensitive.
ds@475
    42
 * @return				-1 on failure (no match found). Any other value
ds@475
    43
 *						indicates a position in the string where the match starts.
dvander@233
    44
 */
dvander@233
    45
native StrContains(const String:str[], const String:substr[], bool:caseSensitive=true);
dvander@233
    46
dvander@233
    47
/**
dvander@233
    48
 * Compares two strings lexographically.
dvander@233
    49
 *
dvander@233
    50
 * @param str1			First string (left).
dvander@233
    51
 * @param str2			Second string (right).
dvander@233
    52
 * @param caseSensitive	If true (default), comparison is case sensitive.
ds@475
    53
 *						If false, comparison is case insensitive.
ds@475
    54
 * @return				-1 if str1 < str2
ds@475
    55
 *						0 if str1 == str2
ds@475
    56
 *						1 if str1 > str2
dvander@233
    57
 */
dvander@680
    58
native strcmp(const String:str1[], const String:str2[], bool:caseSensitive=true);
dvander@680
    59
dvander@680
    60
/**
dvander@680
    61
 * Compares two strings parts lexographically.
dvander@680
    62
 *
dvander@680
    63
 * @param str1			First string (left).
dvander@680
    64
 * @param str2			Second string (right).
dvander@680
    65
 * @param num			Number of characters to compare.
dvander@680
    66
 * @param caseSensitive	If true (default), comparison is case sensitive.
dvander@680
    67
 *						If false, comparison is case insensitive.
dvander@680
    68
 * @return				-1 if str1 < str2
dvander@680
    69
 *						0 if str1 == str2
dvander@680
    70
 *						1 if str1 > str2
dvander@680
    71
 */
dvander@680
    72
native strncmp(const String:str1[], const String:str2[], num, bool:caseSensitive=true);
dvander@680
    73
dvander@680
    74
/**
dvander@680
    75
 * Backwards compatible stock - StrCompare is now strcmp
dvander@681
    76
 * @deprecated		Renamed to strcmp
dvander@680
    77
 */
dvander@680
    78
stock StrCompare(const String:str1[], const String:str2[], bool:caseSensitive=true)
dvander@680
    79
{
dvander@680
    80
	return strcmp(str1, str2, caseSensitive);
dvander@680
    81
}
dvander@233
    82
dvander@233
    83
/**
dvander@233
    84
 * Returns whether two strings are equal.
dvander@233
    85
 *
dvander@233
    86
 *
dvander@233
    87
 * @param str1			First string (left).
dvander@233
    88
 * @param str2			Second string (right).
dvander@233
    89
 * @param caseSensitive	If true (default), comparison is case sensitive.
dvander@412
    90
 * 						If false, comparison is case insensitive.
dvander@412
    91
 * @return				True if equal, false otherwise.
dvander@233
    92
 */
faluco@234
    93
stock bool:StrEqual(const String:str1[], const String:str2[], bool:caseSensitive=true)
dvander@233
    94
{
faluco@234
    95
	return (StrCompare(str1, str2, caseSensitive) == 0);
dvander@233
    96
}
dvander@233
    97
dvander@233
    98
/**
dvander@233
    99
 * Copies one string to another string.
dvander@619
   100
 * @note If the destination buffer is too small to hold the source string, the 
dvander@619
   101
 *       destination will be truncated.
dvander@233
   102
 *
dvander@233
   103
 * @param dest			Destination string buffer to copy to.
dvander@233
   104
 * @param destlen		Destination buffer length (includes null terminator).
dvander@233
   105
 * @param source		Source string buffer to copy from.
dvander@412
   106
 * @return				Number of cells written.
dvander@233
   107
 */
dvander@681
   108
native strcopy(String:dest[], destLen, const String:source[]);
dvander@681
   109
dvander@681
   110
/**
dvander@681
   111
 * Backwards compatibility stock - use strcopy
dvander@681
   112
 * @deprecated			Renamed to strcopy
dvander@681
   113
 */
dvander@681
   114
stock StrCopy(String:dest[], destLen, const String:source[])
dvander@681
   115
{
dvander@681
   116
	return strcopy(dest, destLen, source);
dvander@681
   117
}
dvander@233
   118
dvander@233
   119
/**
dvander@257
   120
 * Formats a string according to the SourceMod format rules (see documentation).
dvander@257
   121
 *
dvander@257
   122
 * @param buffer		Destination string buffer.
dvander@257
   123
 * @param maxlength		Maximum length of output string buffer.
dvander@257
   124
 * @param format		Formatting rules.
dvander@257
   125
 * @param ...			Variable number of format parameters.
dvander@412
   126
 * @return				Number of cells written.
dvander@257
   127
 */
ds@634
   128
native Format(String:buffer[], maxlength, const String:format[], any:...);
dvander@257
   129
dvander@257
   130
/**
dvander@257
   131
 * Formats a string according to the SourceMod format rules (see documentation).
dvander@257
   132
 * @note This is the same as Format(), except none of the input buffers can overlap the same
dvander@619
   133
 *	     memory as the output buffer.  Since this security check is removed, it is slightly faster.
dvander@257
   134
 *
dvander@257
   135
 * @param buffer		Destination string buffer.
dvander@257
   136
 * @param maxlength		Maximum length of output string buffer.
dvander@257
   137
 * @param format		Formatting rules.
dvander@257
   138
 * @param ...			Variable number of format parameters.
dvander@412
   139
 * @return				Number of cells written.
dvander@257
   140
 */
ds@634
   141
native FormatEx(String:buffer[], maxlength, const String:format[], any:...);
dvander@257
   142
dvander@257
   143
/**
dvander@412
   144
 * Formats a string according to the SourceMod format rules (see documentation).
dvander@412
   145
 * @note This is the same as Format(), except it grabs parameters from a parent parameter
dvander@619
   146
 *       stack, rather than a local.  This is useful for implementing your own variable 
dvander@619
   147
 *       argument functions.
dvander@412
   148
 *
dvander@412
   149
 * @param buffer		Destination string buffer.
dvander@412
   150
 * @param maxlength		Maximum length of output string buffer.
dvander@412
   151
 * @param format		Formatting rules.
dvander@412
   152
 * @param varpos		Argument number which contains the '...' symbol.
dvander@412
   153
 *						Note: Arguments start at 1.
dvander@412
   154
 * @return 				Number of bytes written.
dvander@412
   155
 */
dvander@412
   156
native VFormat(String:buffer[], maxlength, const String:format[], varpos);
dvander@412
   157
dvander@412
   158
/**
dvander@233
   159
 * Converts a string to an integer.
dvander@233
   160
 *
dvander@233
   161
 * @param str			String to convert.
dvander@412
   162
 * @param nBase			Numerical base to use.  10 is default.
dvander@412
   163
 * @return				Integer conversion of string, or 0 on failure.
dvander@233
   164
 */
dvander@233
   165
native StringToInt(const String:str[], nBase=10);
dvander@233
   166
dvander@233
   167
/**
dvander@233
   168
 * Converts an integer to a string.
dvander@233
   169
 *
dvander@451
   170
 * @param num			Integer to convert.
dvander@233
   171
 * @param str			Buffer to store string in.
dvander@233
   172
 * @param maxlength		Maximum length of string buffer.
dvander@412
   173
 * @return				Number of cells written to buffer.
dvander@233
   174
 */
dvander@451
   175
native IntToString(num, String:str[], maxlength);
dvander@233
   176
dvander@233
   177
/** 
dvander@233
   178
 * Converts a string to a floating point number.
dvander@233
   179
 *
dvander@233
   180
 * @param str			String to convert to a foat.
dvander@412
   181
 * @return				Floating point result, or 0.0 on error.
dvander@233
   182
 */
dvander@233
   183
native Float:StringToFloat(const String:str[]);
dvander@233
   184
dvander@233
   185
/**
dvander@233
   186
 * Converts a floating point number to a string.
dvander@233
   187
 *
ds@454
   188
 * @param num			Floating point number to convert.
dvander@233
   189
 * @param str			Buffer to store string in.
dvander@233
   190
 * @param maxlength		Maximum length of string buffer.
dvander@412
   191
 * @return				Number of cells written to buffer.
dvander@233
   192
 */
ds@454
   193
native FloatToString(Float:num, String:str[], maxlength);
dvander@631
   194
dvander@631
   195
/**
dvander@631
   196
 * Finds the first "argument" in a string; either a set of space
dvander@631
   197
 * terminated characters, or a fully quoted string.  After the 
dvander@631
   198
 * argument is found, whitespace is read until the next portion
dvander@631
   199
 * of the string is reached.  If nothing remains, -1 is returned.
dvander@631
   200
 * Otherwise, the index to the first character is returned.
dvander@631
   201
 *
dvander@631
   202
 * @param source		Source input string.
dvander@631
   203
 * @param arg			Stores argument read from string.
dvander@631
   204
 * @param argLen		Maximum length of argument buffer.
dvander@631
   205
 * @return				Index to next piece of string, or -1 if none.
dvander@631
   206
 */
ds@634
   207
native StrBreak(const String:source[], String:arg[], argLen);