3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
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
16 #if defined _string_included
19 #define _string_included
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), ...)
29 * Calculates the length of a string.
31 * @param str String to check.
32 * @return Length of string, in cells (NOT characters).
34 native strlen(const String:str[]);
37 * Tests whether a string is found inside another string.
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.
46 native StrContains(const String:str[], const String:substr[], bool:caseSensitive=true);
49 * Compares two strings lexographically.
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
59 native strcmp(const String:str1[], const String:str2[], bool:caseSensitive=true);
62 * Compares two strings parts lexographically.
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
73 native strncmp(const String:str1[], const String:str2[], num, bool:caseSensitive=true);
76 * Backwards compatible stock - StrCompare is now strcmp
77 * @deprecated Renamed to strcmp
79 stock StrCompare(const String:str1[], const String:str2[], bool:caseSensitive=true)
81 return strcmp(str1, str2, caseSensitive);
85 * Returns whether two strings are equal.
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.
94 stock bool:StrEqual(const String:str1[], const String:str2[], bool:caseSensitive=true)
96 return (StrCompare(str1, str2, caseSensitive) == 0);
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.
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.
109 native strcopy(String:dest[], destLen, const String:source[]);
112 * Backwards compatibility stock - use strcopy
113 * @deprecated Renamed to strcopy
115 stock StrCopy(String:dest[], destLen, const String:source[])
117 return strcopy(dest, destLen, source);
121 * Formats a string according to the SourceMod format rules (see documentation).
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.
129 native Format(String:buffer[], maxlength, const String:format[], any:...);
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.
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.
143 native FormatEx(String:buffer[], maxlength, const String:format[], any:...);
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.
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.
158 native VFormat(String:buffer[], maxlength, const String:format[], varpos);
161 * Converts a string to an integer.
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.
167 native StringToInt(const String:str[], nBase=10);
170 * Converts an integer to a string.
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.
177 native IntToString(num, String:str[], maxlength);
180 * Converts a string to a floating point number.
182 * @param str String to convert to a foat.
183 * @return Floating point result, or 0.0 on error.
185 native Float:StringToFloat(const String:str[]);
188 * Converts a floating point number to a string.
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.
195 native FloatToString(Float:num, String:str[], maxlength);
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.
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.
209 native StrBreak(const String:source[], String:arg[], argLen);
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.
216 * @param source Source input string.
217 * @return Number of bytes the current character uses.
219 native GetCharBytes(const String:source[]);
222 * Returns whether a character is an ASCII alphabet character.
224 * @note Multi-byte characters will always return false.
226 * @param char Character to test.
227 * @return True if character is alphabetical, otherwise false.
229 native bool:IsCharAlpha(chr);
232 * Returns whether a character is numeric.
234 * @note Multi-byte characters will always return false.
236 * @param char Character to test.
237 * @return True if character is numeric, otherwise false.
239 native bool:IsCharNumeric(chr);
242 * Returns whether a character is whitespace.
244 * @note Multi-byte characters will always return false.
246 * @param char Character to test.
247 * @return True if character is whitespace, otherwise false.
249 native bool:IsCharSpace(chr);
252 * Returns if a character is multi-byte or not.
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.
258 native IsCharMB(chr);
261 * Returns whether an alphabetic character is uppercase.
263 * @note Multi-byte characters will always return false.
265 * @param char Character to test.
266 * @return True if character is uppercase, otherwise false.
268 native bool:IsCharUpper(chr);
271 * Returns whether an alphabetic character is lowercase.
273 * @note Multi-byte characters will always return false.
275 * @param char Character to test.
276 * @return True if character is lowercase, otherwise false.
278 native bool:IsCharLower(chr);
281 * Returns an uppercase character to a lowercase character.
283 * @param chr Characer to convert.
284 * @return Lowercase character on success,
285 * no change on failure.
287 stock CharToUpper(chr)
289 if (IsCharLower(chr))
291 return (chr & ~(1<<5));
297 * Returns a lowercase character to an uppercase character.
299 * @param chr Characer to convert.
300 * @return Uppercase character on success,
301 * no change on failure.
303 stock CharToLower(chr)
305 if (IsCharUpper(chr))
307 return (chr | (1<<5));
313 * Concatenates one string onto another.
315 * @param buffer String to append to.
316 * @param maxlength Maximum length of entire buffer.
317 * @param source Source string to concatenate.
318 * @return Number of bytes written.
320 stock StrCat(String:buffer[], maxlength, const String:source[])
322 new len = strlen(source);
323 if (len >= maxlength)
328 return Format(buffer[len], maxlength-len, "%s", source);