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.
87 * @param str1 First string (left).
88 * @param str2 Second string (right).
89 * @param caseSensitive If true (default), comparison is case sensitive.
90 * If false, comparison is case insensitive.
91 * @return True if equal, false otherwise.
93 stock bool:StrEqual(const String:str1[], const String:str2[], bool:caseSensitive=true)
95 return (StrCompare(str1, str2, caseSensitive) == 0);
99 * Copies one string to another string.
100 * @note If the destination buffer is too small to hold the source string, the
101 * destination will be truncated.
103 * @param dest Destination string buffer to copy to.
104 * @param destlen Destination buffer length (includes null terminator).
105 * @param source Source string buffer to copy from.
106 * @return Number of cells written.
108 native strcopy(String:dest[], destLen, const String:source[]);
111 * Backwards compatibility stock - use strcopy
112 * @deprecated Renamed to strcopy
114 stock StrCopy(String:dest[], destLen, const String:source[])
116 return strcopy(dest, destLen, source);
120 * Formats a string according to the SourceMod format rules (see documentation).
122 * @param buffer Destination string buffer.
123 * @param maxlength Maximum length of output string buffer.
124 * @param format Formatting rules.
125 * @param ... Variable number of format parameters.
126 * @return Number of cells written.
128 native Format(String:buffer[], maxlength, const String:format[], any:...);
131 * Formats a string according to the SourceMod format rules (see documentation).
132 * @note This is the same as Format(), except none of the input buffers can
133 * overlap the same memory as the output buffer. Since this security
134 * check is removed, it is slightly faster.
136 * @param buffer Destination string buffer.
137 * @param maxlength Maximum length of output string buffer.
138 * @param format Formatting rules.
139 * @param ... Variable number of format parameters.
140 * @return Number of cells written.
142 native FormatEx(String:buffer[], maxlength, const String:format[], any:...);
145 * Formats a string according to the SourceMod format rules (see documentation).
146 * @note This is the same as Format(), except it grabs parameters from a
147 * parent parameter stack, rather than a local. This is useful for
148 * implementing your own variable argument functions.
150 * @param buffer Destination string buffer.
151 * @param maxlength Maximum length of output string buffer.
152 * @param format Formatting rules.
153 * @param varpos Argument number which contains the '...' symbol.
154 * Note: Arguments start at 1.
155 * @return Number of bytes written.
157 native VFormat(String:buffer[], maxlength, const String:format[], varpos);
160 * Converts a string to an integer.
162 * @param str String to convert.
163 * @param nBase Numerical base to use. 10 is default.
164 * @return Integer conversion of string, or 0 on failure.
166 native StringToInt(const String:str[], nBase=10);
169 * Converts a string to an integer with some more options.
171 * @param str String to convert.
172 * @param result Variable to store the result in.
173 * @param nBase Numerical base to use. 10 is default.
174 * @return Number of characters consumed.
176 native StringToIntEx(const String:str[], &result, nBase=10);
179 * Converts an integer to a string.
181 * @param num Integer to convert.
182 * @param str Buffer to store string in.
183 * @param maxlength Maximum length of string buffer.
184 * @return Number of cells written to buffer.
186 native IntToString(num, String:str[], maxlength);
189 * Converts a string to a floating point number.
191 * @param str String to convert to a foat.
192 * @return Floating point result, or 0.0 on error.
194 native Float:StringToFloat(const String:str[]);
197 * Converts a string to a floating point number with some more options.
199 * @param str String to convert to a foat.
200 * @param result Variable to store result in.
201 * @return Number of characters consumed.
203 native StringToFloatEx(const String:str[], &Float:result);
206 * Converts a floating point number to a string.
208 * @param num Floating point number to convert.
209 * @param str Buffer to store string in.
210 * @param maxlength Maximum length of string buffer.
211 * @return Number of cells written to buffer.
213 native FloatToString(Float:num, String:str[], maxlength);
216 * Finds the first "argument" in a string; either a set of space
217 * terminated characters, or a fully quoted string. After the
218 * argument is found, whitespace is read until the next portion
219 * of the string is reached. If nothing remains, -1 is returned.
220 * Otherwise, the index to the first character is returned.
222 * @param source Source input string.
223 * @param arg Stores argument read from string.
224 * @param argLen Maximum length of argument buffer.
225 * @return Index to next piece of string, or -1 if none.
227 native BreakString(const String:source[], String:arg[], argLen);
230 * Backwards compatibility stock - use BreakString
231 * @deprecated Renamed to BreakString.
233 stock StrBreak(const String:source[], String:arg[], argLen)
235 return BreakString(source, arg, argLen);
239 * Removes whitespace characters from the beginning and end of a string.
241 * @param str The string to trim.
242 * @return Number of bytes written (UTF-8 safe).
244 native TrimString(String:str[]);
247 * Returns text in a string up until a certain character sequence is reached.
249 * @param source Source input string.
250 * @param split A string which specifies a search point to break at.
251 * @param part Buffer to store string part.
252 * @param partLen Maximum length of the string part buffer.
253 * @return -1 if no match was found; otherwise, an index into source
254 * marking the first index after the searched text. The
255 * index is always relative to the start of the input string.
257 native SplitString(const String:source[], const String:split[], String:part[], partLen);
260 * Given a string, replaces all occurrences of a search string with a
261 * replacement string.
263 * @param text String to perform search and replacements on.
264 * @param maxlength Maximum length of the string buffer.
265 * @param search String to search for.
266 * @param replace String to replace the search string with.
267 * @return Number of replacements that were performed.
269 native ReplaceString(String:text[], maxlength, const String:search[], const String:replace[]);
272 * Given a string, replaces the first occurrence of a search string with a
273 * replacement string.
275 * @param text String to perform search and replacements on.
276 * @param maxlength Maximum length of the string buffer.
277 * @param search String to search for.
278 * @param replace String to replace the search string with.
279 * @param searchLen If higher than -1, its value will be used instead of
280 * a strlen() call on the search parameter.
281 * @param replaceLen If higher than -1, its value will be used instead of
282 * a strlen() call on the replace parameter.
283 * @return Index into the buffer (relative to the start) from where
284 * the last replacement ended, or -1 if no replacements were
287 native ReplaceStringEx(String:text[], maxlength, const String:search[], const String:replace[], searchLen=-1, replaceLen=-1);
290 * Returns the number of bytes a character is using. This is
291 * for multi-byte characters (UTF-8). For normal ASCII characters,
292 * this will return 1.
294 * @param source Source input string.
295 * @return Number of bytes the current character uses.
297 native GetCharBytes(const String:source[]);
300 * Returns whether a character is an ASCII alphabet character.
302 * @note Multi-byte characters will always return false.
304 * @param char Character to test.
305 * @return True if character is alphabetical, otherwise false.
307 native bool:IsCharAlpha(chr);
310 * Returns whether a character is numeric.
312 * @note Multi-byte characters will always return false.
314 * @param char Character to test.
315 * @return True if character is numeric, otherwise false.
317 native bool:IsCharNumeric(chr);
320 * Returns whether a character is whitespace.
322 * @note Multi-byte characters will always return false.
324 * @param char Character to test.
325 * @return True if character is whitespace, otherwise false.
327 native bool:IsCharSpace(chr);
330 * Returns if a character is multi-byte or not.
332 * @param char Character to test.
333 * @return 0 for a normal 7-bit ASCII character,
334 * otherwise number of bytes in multi-byte character.
336 native IsCharMB(chr);
339 * Returns whether an alphabetic character is uppercase.
341 * @note Multi-byte characters will always return false.
343 * @param char Character to test.
344 * @return True if character is uppercase, otherwise false.
346 native bool:IsCharUpper(chr);
349 * Returns whether an alphabetic character is lowercase.
351 * @note Multi-byte characters will always return false.
353 * @param char Character to test.
354 * @return True if character is lowercase, otherwise false.
356 native bool:IsCharLower(chr);
359 * Returns an uppercase character to a lowercase character.
361 * @param chr Characer to convert.
362 * @return Lowercase character on success,
363 * no change on failure.
365 stock CharToUpper(chr)
367 if (IsCharLower(chr))
369 return (chr & ~(1<<5));
375 * Returns a lowercase character to an uppercase character.
377 * @param chr Characer to convert.
378 * @return Uppercase character on success,
379 * no change on failure.
381 stock CharToLower(chr)
383 if (IsCharUpper(chr))
385 return (chr | (1<<5));
391 * Concatenates one string onto another.
393 * @param buffer String to append to.
394 * @param maxlength Maximum length of entire buffer.
395 * @param source Source string to concatenate.
396 * @return Number of bytes written.
398 stock StrCat(String:buffer[], maxlength, const String:source[])
400 new len = strlen(source);
401 if (len >= maxlength)
406 return Format(buffer[len], maxlength-len, "%s", source);
410 * Breaks a string into pieces and stores each piece into an array of buffers.
412 * @param text The string to split.
413 * @param split The string to use as a split delimiter.
414 * @param buffers An array of string buffers (2D array).
415 * @param maxStrings Number of string buffers (first dimension size).
416 * @param maxStringLength Maximum length of each string buffer.
417 * @return Number of strings retrieved.
419 stock ExplodeString(const String:text[], const String:split[], String:buffers[][], maxStrings, maxStringLength)
421 new reloc_idx, idx, total;
423 if (maxStrings < 1 || split[0] == '\0')
428 while ((idx = SplitString(text[reloc_idx], split, buffers[total], maxStringLength)) != -1)
431 if (text[reloc_idx] == '\0')
435 if (++total >= maxStrings)
441 if (text[reloc_idx] != '\0' && total <= maxStrings - 1)
443 strcopy(buffers[total++], maxStringLength, text[reloc_idx]);
450 * Joins an array of strings into one string, with a "join" string inserted in
451 * between each given string. This function complements ExplodeString.
453 * @param strings An array of strings.
454 * @param numStrings Number of strings in the array.
455 * @param join The join string to insert between each string.
456 * @param buffer Output buffer to write the joined string to.
457 * @param maxLength Maximum length of the output buffer.
458 * @return Number of bytes written to the output buffer.
460 stock ImplodeStrings(const String:strings[][], numStrings, const String:join[], String:buffer[], maxLength)
462 new total, length, part_length;
463 new join_length = strlen(join);
464 for (new i=0; i<numStrings; i++)
466 length = strcopy(buffer[total], maxLength-total, strings[i]);
468 if (length < part_length)
472 if (i != numStrings - 1)
474 length = strcopy(buffer[total], maxLength-total, join);
476 if (length < join_length)