Fixed a bug where natives with no owner could be bound (crashing).
Fixed a bug where FakeNative entries were leaked.
3 * =============================================================================
4 * SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
5 * =============================================================================
7 * This file is part of the SourceMod/SourcePawn SDK.
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, version 3.0, as published by the
11 * Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License along with
19 * this program. If not, see <http://www.gnu.org/licenses/>.
21 * As a special exception, AlliedModders LLC gives you permission to link the
22 * code of this program (as well as its derivative works) to "Half-Life 2," the
23 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
24 * by the Valve Corporation. You must obey the GNU General Public License in
25 * all respects for all other code used. Additionally, AlliedModders LLC grants
26 * this exception to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
28 * or <http://www.sourcemod.net/license.php>.
33 #if defined _string_included
36 #define _string_included
39 * @global Unless otherwise noted, all string functions which take in a
40 * writable buffer and maximum length should have the null terminator INCLUDED
41 * in the length. This means that this is valid:
42 * strcopy(string, sizeof(string), ...)
46 * Calculates the length of a string.
48 * @param str String to check.
49 * @return Number of valid character bytes in the string.
51 native strlen(const String:str[]);
54 * Tests whether a string is found inside another string.
56 * @param str String to search in.
57 * @param substr Substring to find inside the original string.
58 * @param caseSensitive If true (default), search is case sensitive.
59 * If false, search is case insensitive.
60 * @return -1 on failure (no match found). Any other value
61 * indicates a position in the string where the match starts.
63 native StrContains(const String:str[], const String:substr[], bool:caseSensitive=true);
66 * Compares two strings lexographically.
68 * @param str1 First string (left).
69 * @param str2 Second string (right).
70 * @param caseSensitive If true (default), comparison is case sensitive.
71 * If false, comparison is case insensitive.
72 * @return -1 if str1 < str2
76 native strcmp(const String:str1[], const String:str2[], bool:caseSensitive=true);
79 * Compares two strings parts lexographically.
81 * @param str1 First string (left).
82 * @param str2 Second string (right).
83 * @param num Number of characters to compare.
84 * @param caseSensitive If true (default), comparison is case sensitive.
85 * If false, comparison is case insensitive.
86 * @return -1 if str1 < str2
90 native strncmp(const String:str1[], const String:str2[], num, bool:caseSensitive=true);
93 * Backwards compatible stock - StrCompare is now strcmp
94 * @deprecated Renamed to strcmp
96 #pragma deprecated Use strcmp() instead
97 stock StrCompare(const String:str1[], const String:str2[], bool:caseSensitive=true)
99 return strcmp(str1, str2, caseSensitive);
103 * Returns whether two strings are equal.
105 * @param str1 First string (left).
106 * @param str2 Second string (right).
107 * @param caseSensitive If true (default), comparison is case sensitive.
108 * If false, comparison is case insensitive.
109 * @return True if equal, false otherwise.
111 stock bool:StrEqual(const String:str1[], const String:str2[], bool:caseSensitive=true)
113 return (strcmp(str1, str2, caseSensitive) == 0);
117 * Copies one string to another string.
118 * @note If the destination buffer is too small to hold the source string, the
119 * destination will be truncated.
121 * @param dest Destination string buffer to copy to.
122 * @param destlen Destination buffer length (includes null terminator).
123 * @param source Source string buffer to copy from.
124 * @return Number of cells written.
126 native strcopy(String:dest[], destLen, const String:source[]);
129 * Backwards compatibility stock - use strcopy
130 * @deprecated Renamed to strcopy
132 #pragma deprecated Use strcopy() instead
133 stock StrCopy(String:dest[], destLen, const String:source[])
135 return strcopy(dest, destLen, source);
139 * Formats a string according to the SourceMod format rules (see documentation).
141 * @param buffer Destination string buffer.
142 * @param maxlength Maximum length of output string buffer.
143 * @param format Formatting rules.
144 * @param ... Variable number of format parameters.
145 * @return Number of cells written.
147 native Format(String:buffer[], maxlength, const String:format[], any:...);
150 * Formats a string according to the SourceMod format rules (see documentation).
151 * @note This is the same as Format(), except none of the input buffers can
152 * overlap the same memory as the output buffer. Since this security
153 * check is removed, it is slightly faster.
155 * @param buffer Destination string buffer.
156 * @param maxlength Maximum length of output string buffer.
157 * @param format Formatting rules.
158 * @param ... Variable number of format parameters.
159 * @return Number of cells written.
161 native FormatEx(String:buffer[], maxlength, const String:format[], any:...);
164 * Formats a string according to the SourceMod format rules (see documentation).
165 * @note This is the same as Format(), except it grabs parameters from a
166 * parent parameter stack, rather than a local. This is useful for
167 * implementing your own variable argument functions.
169 * @param buffer Destination string buffer.
170 * @param maxlength Maximum length of output string buffer.
171 * @param format Formatting rules.
172 * @param varpos Argument number which contains the '...' symbol.
173 * Note: Arguments start at 1.
174 * @return Number of bytes written.
176 native VFormat(String:buffer[], maxlength, const String:format[], varpos);
179 * Converts a string to an integer.
181 * @param str String to convert.
182 * @param nBase Numerical base to use. 10 is default.
183 * @return Integer conversion of string, or 0 on failure.
185 native StringToInt(const String:str[], nBase=10);
188 * Converts a string to an integer with some more options.
190 * @param str String to convert.
191 * @param result Variable to store the result in.
192 * @param nBase Numerical base to use. 10 is default.
193 * @return Number of characters consumed.
195 native StringToIntEx(const String:str[], &result, nBase=10);
198 * Converts an integer to a string.
200 * @param num Integer to convert.
201 * @param str Buffer to store string in.
202 * @param maxlength Maximum length of string buffer.
203 * @return Number of cells written to buffer.
205 native IntToString(num, String:str[], maxlength);
208 * Converts a string to a floating point number.
210 * @param str String to convert to a foat.
211 * @return Floating point result, or 0.0 on error.
213 native Float:StringToFloat(const String:str[]);
216 * Converts a string to a floating point number with some more options.
218 * @param str String to convert to a foat.
219 * @param result Variable to store result in.
220 * @return Number of characters consumed.
222 native StringToFloatEx(const String:str[], &Float:result);
225 * Converts a floating point number to a string.
227 * @param num Floating point number to convert.
228 * @param str Buffer to store string in.
229 * @param maxlength Maximum length of string buffer.
230 * @return Number of cells written to buffer.
232 native FloatToString(Float:num, String:str[], maxlength);
235 * Finds the first "argument" in a string; either a set of space
236 * terminated characters, or a fully quoted string. After the
237 * argument is found, whitespace is read until the next portion
238 * of the string is reached. If nothing remains, -1 is returned.
239 * Otherwise, the index to the first character is returned.
241 * @param source Source input string.
242 * @param arg Stores argument read from string.
243 * @param argLen Maximum length of argument buffer.
244 * @return Index to next piece of string, or -1 if none.
246 native BreakString(const String:source[], String:arg[], argLen);
249 * Backwards compatibility stock - use BreakString
250 * @deprecated Renamed to BreakString.
252 #pragma deprecated Use BreakString() instead
253 stock StrBreak(const String:source[], String:arg[], argLen)
255 return BreakString(source, arg, argLen);
259 * Removes whitespace characters from the beginning and end of a string.
261 * @param str The string to trim.
262 * @return Number of bytes written (UTF-8 safe).
264 native TrimString(String:str[]);
267 * Returns text in a string up until a certain character sequence is reached.
269 * @param source Source input string.
270 * @param split A string which specifies a search point to break at.
271 * @param part Buffer to store string part.
272 * @param partLen Maximum length of the string part buffer.
273 * @return -1 if no match was found; otherwise, an index into source
274 * marking the first index after the searched text. The
275 * index is always relative to the start of the input string.
277 native SplitString(const String:source[], const String:split[], String:part[], partLen);
280 * Given a string, replaces all occurrences of a search string with a
281 * replacement string.
283 * @param text String to perform search and replacements on.
284 * @param maxlength Maximum length of the string buffer.
285 * @param search String to search for.
286 * @param replace String to replace the search string with.
287 * @return Number of replacements that were performed.
289 native ReplaceString(String:text[], maxlength, const String:search[], const String:replace[]);
292 * Given a string, replaces the first occurrence of a search string with a
293 * replacement string.
295 * @param text String to perform search and replacements on.
296 * @param maxlength Maximum length of the string buffer.
297 * @param search String to search for.
298 * @param replace String to replace the search string with.
299 * @param searchLen If higher than -1, its value will be used instead of
300 * a strlen() call on the search parameter.
301 * @param replaceLen If higher than -1, its value will be used instead of
302 * a strlen() call on the replace parameter.
303 * @return Index into the buffer (relative to the start) from where
304 * the last replacement ended, or -1 if no replacements were
307 native ReplaceStringEx(String:text[], maxlength, const String:search[], const String:replace[], searchLen=-1, replaceLen=-1);
310 * Returns the number of bytes a character is using. This is
311 * for multi-byte characters (UTF-8). For normal ASCII characters,
312 * this will return 1.
314 * @param source Source input string.
315 * @return Number of bytes the current character uses.
317 native GetCharBytes(const String:source[]);
320 * Returns whether a character is an ASCII alphabet character.
322 * @note Multi-byte characters will always return false.
324 * @param char Character to test.
325 * @return True if character is alphabetical, otherwise false.
327 native bool:IsCharAlpha(chr);
330 * Returns whether a character is numeric.
332 * @note Multi-byte characters will always return false.
334 * @param char Character to test.
335 * @return True if character is numeric, otherwise false.
337 native bool:IsCharNumeric(chr);
340 * Returns whether a character is whitespace.
342 * @note Multi-byte characters will always return false.
344 * @param char Character to test.
345 * @return True if character is whitespace, otherwise false.
347 native bool:IsCharSpace(chr);
350 * Returns if a character is multi-byte or not.
352 * @param char Character to test.
353 * @return 0 for a normal 7-bit ASCII character,
354 * otherwise number of bytes in multi-byte character.
356 native IsCharMB(chr);
359 * Returns whether an alphabetic character is uppercase.
361 * @note Multi-byte characters will always return false.
363 * @param char Character to test.
364 * @return True if character is uppercase, otherwise false.
366 native bool:IsCharUpper(chr);
369 * Returns whether an alphabetic character is lowercase.
371 * @note Multi-byte characters will always return false.
373 * @param char Character to test.
374 * @return True if character is lowercase, otherwise false.
376 native bool:IsCharLower(chr);
379 * Strips a quote pair off a string if it exists. That is, the following
380 * replace rule is applied once: $"(.*)"^ -> $\1^
382 * Note that the leading and trailing quotes will only be removed if both
383 * exist. Otherwise, the string is left unmodified. This function should
384 * be considered O(k) (all characters get shifted down).
386 * @param text String to modify (in place).
387 * @return True if string was modified, false if there was no
390 native bool:StripQuotes(String:text[]);
393 * Returns an uppercase character to a lowercase character.
395 * @param chr Characer to convert.
396 * @return Lowercase character on success,
397 * no change on failure.
399 stock CharToUpper(chr)
401 if (IsCharLower(chr))
403 return (chr & ~(1<<5));
409 * Returns a lowercase character to an uppercase character.
411 * @param chr Characer to convert.
412 * @return Uppercase character on success,
413 * no change on failure.
415 stock CharToLower(chr)
417 if (IsCharUpper(chr))
419 return (chr | (1<<5));
425 * Finds the first occurrence of a character in a string.
428 * @param c Character to search for.
429 * @param reverse False (default) to search forward, true to search
431 * @return The index of the first occurrence of the character
432 * in the string, or -1 if the character was not found.
434 stock FindCharInString(const String:str[], c, bool:reverse = false)
442 for (i = 0; i < len; i++)
452 for (i = len - 1; i >= 0; i--)
465 * Concatenates one string onto another.
467 * @param buffer String to append to.
468 * @param maxlength Maximum length of entire buffer.
469 * @param source Source string to concatenate.
470 * @return Number of bytes written.
472 stock StrCat(String:buffer[], maxlength, const String:source[])
474 new len = strlen(buffer);
475 if (len >= maxlength)
480 return Format(buffer[len], maxlength-len, "%s", source);
484 * Breaks a string into pieces and stores each piece into an array of buffers.
486 * @param text The string to split.
487 * @param split The string to use as a split delimiter.
488 * @param buffers An array of string buffers (2D array).
489 * @param maxStrings Number of string buffers (first dimension size).
490 * @param maxStringLength Maximum length of each string buffer.
491 * @return Number of strings retrieved.
493 stock ExplodeString(const String:text[], const String:split[], String:buffers[][], maxStrings, maxStringLength)
495 new reloc_idx, idx, total;
497 if (maxStrings < 1 || split[0] == '\0')
502 while ((idx = SplitString(text[reloc_idx], split, buffers[total], maxStringLength)) != -1)
505 if (text[reloc_idx] == '\0')
509 if (++total >= maxStrings)
515 if (text[reloc_idx] != '\0' && total <= maxStrings - 1)
517 strcopy(buffers[total++], maxStringLength, text[reloc_idx]);
524 * Joins an array of strings into one string, with a "join" string inserted in
525 * between each given string. This function complements ExplodeString.
527 * @param strings An array of strings.
528 * @param numStrings Number of strings in the array.
529 * @param join The join string to insert between each string.
530 * @param buffer Output buffer to write the joined string to.
531 * @param maxLength Maximum length of the output buffer.
532 * @return Number of bytes written to the output buffer.
534 stock ImplodeStrings(const String:strings[][], numStrings, const String:join[], String:buffer[], maxLength)
536 new total, length, part_length;
537 new join_length = strlen(join);
538 for (new i=0; i<numStrings; i++)
540 length = strcopy(buffer[total], maxLength-total, strings[i]);
542 if (length < part_length)
546 if (i != numStrings - 1)
548 length = strcopy(buffer[total], maxLength-total, join);
550 if (length < join_length)