dvander@233: /** ds@475: * vim: set ts=4 : ds@1330: * ============================================================================= ds@1947: * SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved. ds@1330: * ============================================================================= ds@1330: * ds@1330: * This file is part of the SourceMod/SourcePawn SDK. dvander@374: * ds@1330: * This program is free software; you can redistribute it and/or modify it under ds@1330: * the terms of the GNU General Public License, version 3.0, as published by the ds@1330: * Free Software Foundation. ds@1330: * ds@1330: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1330: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1330: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1330: * details. ds@1330: * ds@1330: * You should have received a copy of the GNU General Public License along with ds@1330: * this program. If not, see . ds@1330: * ds@1330: * As a special exception, AlliedModders LLC gives you permission to link the ds@1330: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1330: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1330: * by the Valve Corporation. You must obey the GNU General Public License in ds@1330: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1330: * this exception to all derivative works. AlliedModders LLC defines further ds@1330: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1330: * or . dvander@374: * dvander@374: * Version: $Id$ dvander@233: */ dvander@374: dvander@233: #if defined _string_included dvander@233: #endinput dvander@233: #endif dvander@233: #define _string_included dvander@233: dvander@233: /** dvander@748: * @global Unless otherwise noted, all string functions which take in a dvander@748: * writable buffer and maximum length should have the null terminator INCLUDED dvander@748: * in the length. This means that this is valid: dvander@1010: * strcopy(string, sizeof(string), ...) dvander@233: */ dvander@631: dvander@233: /** dvander@233: * Calculates the length of a string. dvander@233: * ds@475: * @param str String to check. dvander@1652: * @return Number of valid character bytes in the string. dvander@233: */ dvander@233: native strlen(const String:str[]); dvander@233: dvander@233: /** dvander@233: * Tests whether a string is found inside another string. dvander@233: * dvander@233: * @param str String to search in. dvander@233: * @param substr Substring to find inside the original string. dvander@233: * @param caseSensitive If true (default), search is case sensitive. ds@475: * If false, search is case insensitive. ds@475: * @return -1 on failure (no match found). Any other value ds@475: * indicates a position in the string where the match starts. dvander@233: */ dvander@233: native StrContains(const String:str[], const String:substr[], bool:caseSensitive=true); dvander@233: dvander@233: /** dvander@233: * Compares two strings lexographically. dvander@233: * dvander@233: * @param str1 First string (left). dvander@233: * @param str2 Second string (right). dvander@233: * @param caseSensitive If true (default), comparison is case sensitive. ds@475: * If false, comparison is case insensitive. ds@475: * @return -1 if str1 < str2 ds@475: * 0 if str1 == str2 ds@475: * 1 if str1 > str2 dvander@233: */ dvander@680: native strcmp(const String:str1[], const String:str2[], bool:caseSensitive=true); dvander@680: dvander@680: /** dvander@680: * Compares two strings parts lexographically. dvander@680: * dvander@680: * @param str1 First string (left). dvander@680: * @param str2 Second string (right). dvander@680: * @param num Number of characters to compare. dvander@680: * @param caseSensitive If true (default), comparison is case sensitive. dvander@680: * If false, comparison is case insensitive. dvander@680: * @return -1 if str1 < str2 dvander@680: * 0 if str1 == str2 dvander@680: * 1 if str1 > str2 dvander@680: */ dvander@680: native strncmp(const String:str1[], const String:str2[], num, bool:caseSensitive=true); dvander@680: dvander@680: /** dvander@680: * Backwards compatible stock - StrCompare is now strcmp dvander@681: * @deprecated Renamed to strcmp dvander@680: */ ds@1268: #pragma deprecated Use strcmp() instead dvander@680: stock StrCompare(const String:str1[], const String:str2[], bool:caseSensitive=true) dvander@680: { dvander@680: return strcmp(str1, str2, caseSensitive); dvander@680: } dvander@233: dvander@233: /** dvander@233: * Returns whether two strings are equal. dvander@233: * dvander@233: * @param str1 First string (left). dvander@233: * @param str2 Second string (right). dvander@233: * @param caseSensitive If true (default), comparison is case sensitive. dvander@412: * If false, comparison is case insensitive. dvander@412: * @return True if equal, false otherwise. dvander@233: */ faluco@234: stock bool:StrEqual(const String:str1[], const String:str2[], bool:caseSensitive=true) dvander@233: { ds@1268: return (strcmp(str1, str2, caseSensitive) == 0); dvander@233: } dvander@233: dvander@233: /** dvander@233: * Copies one string to another string. dvander@619: * @note If the destination buffer is too small to hold the source string, the dvander@619: * destination will be truncated. dvander@233: * dvander@233: * @param dest Destination string buffer to copy to. dvander@233: * @param destlen Destination buffer length (includes null terminator). dvander@233: * @param source Source string buffer to copy from. dvander@412: * @return Number of cells written. dvander@233: */ dvander@681: native strcopy(String:dest[], destLen, const String:source[]); dvander@681: dvander@681: /** dvander@681: * Backwards compatibility stock - use strcopy dvander@681: * @deprecated Renamed to strcopy dvander@681: */ ds@1268: #pragma deprecated Use strcopy() instead dvander@681: stock StrCopy(String:dest[], destLen, const String:source[]) dvander@681: { dvander@681: return strcopy(dest, destLen, source); dvander@681: } dvander@233: dvander@233: /** dvander@257: * Formats a string according to the SourceMod format rules (see documentation). dvander@257: * dvander@257: * @param buffer Destination string buffer. dvander@257: * @param maxlength Maximum length of output string buffer. dvander@257: * @param format Formatting rules. dvander@257: * @param ... Variable number of format parameters. dvander@412: * @return Number of cells written. dvander@257: */ ds@634: native Format(String:buffer[], maxlength, const String:format[], any:...); dvander@257: dvander@257: /** dvander@257: * Formats a string according to the SourceMod format rules (see documentation). dvander@748: * @note This is the same as Format(), except none of the input buffers can dvander@748: * overlap the same memory as the output buffer. Since this security dvander@748: * check is removed, it is slightly faster. dvander@257: * dvander@257: * @param buffer Destination string buffer. dvander@257: * @param maxlength Maximum length of output string buffer. dvander@257: * @param format Formatting rules. dvander@257: * @param ... Variable number of format parameters. dvander@412: * @return Number of cells written. dvander@257: */ ds@634: native FormatEx(String:buffer[], maxlength, const String:format[], any:...); dvander@257: dvander@257: /** dvander@412: * Formats a string according to the SourceMod format rules (see documentation). dvander@748: * @note This is the same as Format(), except it grabs parameters from a dvander@748: * parent parameter stack, rather than a local. This is useful for dvander@748: * implementing your own variable argument functions. dvander@412: * dvander@412: * @param buffer Destination string buffer. dvander@412: * @param maxlength Maximum length of output string buffer. dvander@412: * @param format Formatting rules. dvander@412: * @param varpos Argument number which contains the '...' symbol. dvander@412: * Note: Arguments start at 1. dvander@412: * @return Number of bytes written. dvander@412: */ dvander@412: native VFormat(String:buffer[], maxlength, const String:format[], varpos); dvander@412: dvander@412: /** dvander@233: * Converts a string to an integer. dvander@233: * dvander@233: * @param str String to convert. dvander@412: * @param nBase Numerical base to use. 10 is default. dvander@412: * @return Integer conversion of string, or 0 on failure. dvander@233: */ dvander@233: native StringToInt(const String:str[], nBase=10); dvander@233: dvander@233: /** dvander@1072: * Converts a string to an integer with some more options. dvander@1072: * dvander@1072: * @param str String to convert. dvander@1072: * @param result Variable to store the result in. dvander@1072: * @param nBase Numerical base to use. 10 is default. dvander@1072: * @return Number of characters consumed. dvander@1072: */ dvander@1072: native StringToIntEx(const String:str[], &result, nBase=10); dvander@1072: dvander@1072: /** dvander@233: * Converts an integer to a string. dvander@233: * dvander@451: * @param num Integer to convert. dvander@233: * @param str Buffer to store string in. dvander@233: * @param maxlength Maximum length of string buffer. dvander@412: * @return Number of cells written to buffer. dvander@233: */ dvander@451: native IntToString(num, String:str[], maxlength); dvander@233: dvander@233: /** dvander@233: * Converts a string to a floating point number. dvander@233: * dvander@233: * @param str String to convert to a foat. dvander@412: * @return Floating point result, or 0.0 on error. dvander@233: */ dvander@233: native Float:StringToFloat(const String:str[]); dvander@233: dvander@1072: /** dvander@1072: * Converts a string to a floating point number with some more options. dvander@1072: * dvander@1072: * @param str String to convert to a foat. dvander@1072: * @param result Variable to store result in. dvander@1072: * @return Number of characters consumed. dvander@1072: */ dvander@1072: native StringToFloatEx(const String:str[], &Float:result); dvander@1072: dvander@233: /** dvander@233: * Converts a floating point number to a string. dvander@233: * ds@454: * @param num Floating point number to convert. dvander@233: * @param str Buffer to store string in. dvander@233: * @param maxlength Maximum length of string buffer. dvander@412: * @return Number of cells written to buffer. dvander@233: */ ds@454: native FloatToString(Float:num, String:str[], maxlength); dvander@631: dvander@631: /** dvander@631: * Finds the first "argument" in a string; either a set of space dvander@631: * terminated characters, or a fully quoted string. After the dvander@631: * argument is found, whitespace is read until the next portion dvander@631: * of the string is reached. If nothing remains, -1 is returned. dvander@631: * Otherwise, the index to the first character is returned. dvander@631: * dvander@631: * @param source Source input string. dvander@631: * @param arg Stores argument read from string. dvander@631: * @param argLen Maximum length of argument buffer. dvander@631: * @return Index to next piece of string, or -1 if none. dvander@631: */ ds@830: native BreakString(const String:source[], String:arg[], argLen); ds@830: ds@830: /** ds@830: * Backwards compatibility stock - use BreakString ds@830: * @deprecated Renamed to BreakString. ds@830: */ ds@1268: #pragma deprecated Use BreakString() instead ds@830: stock StrBreak(const String:source[], String:arg[], argLen) ds@830: { ds@830: return BreakString(source, arg, argLen); ds@830: } dvander@748: dvander@833: /** ds@839: * Removes whitespace characters from the beginning and end of a string. ds@839: * ds@839: * @param str The string to trim. ds@839: * @return Number of bytes written (UTF-8 safe). ds@839: */ ds@839: native TrimString(String:str[]); ds@839: ds@839: /** dvander@834: * Returns text in a string up until a certain character sequence is reached. dvander@834: * dvander@834: * @param source Source input string. ds@839: * @param split A string which specifies a search point to break at. dvander@834: * @param part Buffer to store string part. dvander@834: * @param partLen Maximum length of the string part buffer. dvander@834: * @return -1 if no match was found; otherwise, an index into source dvander@834: * marking the first index after the searched text. The dvander@834: * index is always relative to the start of the input string. dvander@834: */ dvander@834: native SplitString(const String:source[], const String:split[], String:part[], partLen); dvander@834: dvander@834: /** dvander@833: * Given a string, replaces all occurrences of a search string with a dvander@833: * replacement string. dvander@833: * dvander@833: * @param text String to perform search and replacements on. dvander@833: * @param maxlength Maximum length of the string buffer. dvander@833: * @param search String to search for. dvander@833: * @param replace String to replace the search string with. fyrenmoo@2637: * @param caseSensitive If true (default), search is case sensitive. dvander@833: * @return Number of replacements that were performed. dvander@833: */ fyrenmoo@2637: native ReplaceString(String:text[], maxlength, const String:search[], const String:replace[], bool:caseSensitive=true); dvander@833: dvander@833: /** dvander@833: * Given a string, replaces the first occurrence of a search string with a dvander@833: * replacement string. dvander@833: * dvander@833: * @param text String to perform search and replacements on. dvander@833: * @param maxlength Maximum length of the string buffer. dvander@833: * @param search String to search for. dvander@833: * @param replace String to replace the search string with. dvander@833: * @param searchLen If higher than -1, its value will be used instead of dvander@833: * a strlen() call on the search parameter. dvander@833: * @param replaceLen If higher than -1, its value will be used instead of dvander@833: * a strlen() call on the replace parameter. fyrenmoo@2637: * @param caseSensitive If true (default), search is case sensitive. dvander@833: * @return Index into the buffer (relative to the start) from where dvander@833: * the last replacement ended, or -1 if no replacements were dvander@833: * made. dvander@833: */ fyrenmoo@2637: native ReplaceStringEx(String:text[], maxlength, const String:search[], const String:replace[], searchLen=-1, replaceLen=-1, bool:caseSensitive=true); dvander@833: dvander@748: /** dvander@748: * Returns the number of bytes a character is using. This is dvander@748: * for multi-byte characters (UTF-8). For normal ASCII characters, dvander@748: * this will return 1. dvander@748: * dvander@748: * @param source Source input string. dvander@748: * @return Number of bytes the current character uses. dvander@748: */ dvander@748: native GetCharBytes(const String:source[]); dvander@748: dvander@748: /** dvander@748: * Returns whether a character is an ASCII alphabet character. dvander@748: * dvander@748: * @note Multi-byte characters will always return false. dvander@748: * dvander@748: * @param char Character to test. dvander@748: * @return True if character is alphabetical, otherwise false. dvander@748: */ dvander@748: native bool:IsCharAlpha(chr); dvander@748: dvander@748: /** dvander@748: * Returns whether a character is numeric. dvander@748: * dvander@748: * @note Multi-byte characters will always return false. dvander@748: * dvander@748: * @param char Character to test. dvander@748: * @return True if character is numeric, otherwise false. dvander@748: */ dvander@748: native bool:IsCharNumeric(chr); dvander@748: dvander@748: /** dvander@748: * Returns whether a character is whitespace. dvander@748: * dvander@748: * @note Multi-byte characters will always return false. dvander@748: * dvander@748: * @param char Character to test. dvander@748: * @return True if character is whitespace, otherwise false. dvander@748: */ dvander@748: native bool:IsCharSpace(chr); dvander@748: dvander@748: /** dvander@748: * Returns if a character is multi-byte or not. dvander@748: * dvander@748: * @param char Character to test. dvander@748: * @return 0 for a normal 7-bit ASCII character, dvander@748: * otherwise number of bytes in multi-byte character. dvander@748: */ dvander@748: native IsCharMB(chr); dvander@748: dvander@748: /** dvander@748: * Returns whether an alphabetic character is uppercase. dvander@748: * dvander@748: * @note Multi-byte characters will always return false. dvander@748: * dvander@748: * @param char Character to test. dvander@748: * @return True if character is uppercase, otherwise false. dvander@748: */ dvander@748: native bool:IsCharUpper(chr); dvander@748: dvander@748: /** dvander@748: * Returns whether an alphabetic character is lowercase. dvander@748: * dvander@748: * @note Multi-byte characters will always return false. dvander@748: * dvander@748: * @param char Character to test. dvander@748: * @return True if character is lowercase, otherwise false. dvander@748: */ dvander@748: native bool:IsCharLower(chr); dvander@748: dvander@748: /** dvander@2004: * Strips a quote pair off a string if it exists. That is, the following dvander@2004: * replace rule is applied once: $"(.*)"^ -> $\1^ dvander@2004: * dvander@2004: * Note that the leading and trailing quotes will only be removed if both dvander@2004: * exist. Otherwise, the string is left unmodified. This function should dvander@2004: * be considered O(k) (all characters get shifted down). dvander@2004: * dvander@2004: * @param text String to modify (in place). dvander@2004: * @return True if string was modified, false if there was no dvander@2004: * set of quotes. dvander@2004: */ dvander@2004: native bool:StripQuotes(String:text[]); dvander@2004: dvander@2004: /** dvander@748: * Returns an uppercase character to a lowercase character. dvander@748: * dvander@748: * @param chr Characer to convert. dvander@748: * @return Lowercase character on success, dvander@748: * no change on failure. dvander@748: */ dvander@748: stock CharToUpper(chr) dvander@748: { dvander@748: if (IsCharLower(chr)) dvander@748: { dvander@748: return (chr & ~(1<<5)); dvander@748: } dvander@748: return chr; dvander@748: } dvander@748: dvander@748: /** dvander@748: * Returns a lowercase character to an uppercase character. dvander@748: * dvander@748: * @param chr Characer to convert. dvander@748: * @return Uppercase character on success, dvander@748: * no change on failure. dvander@748: */ dvander@748: stock CharToLower(chr) dvander@748: { dvander@748: if (IsCharUpper(chr)) dvander@748: { dvander@748: return (chr | (1<<5)); dvander@748: } dvander@748: return chr; dvander@748: } dvander@748: dvander@749: /** dvander@2070: * Finds the first occurrence of a character in a string. dvander@2070: * dvander@2070: * @param str String. dvander@2070: * @param c Character to search for. dvander@2070: * @param reverse False (default) to search forward, true to search dvander@2070: * backward. dvander@2070: * @return The index of the first occurrence of the character dvander@2070: * in the string, or -1 if the character was not found. dvander@2070: */ dvander@2070: stock FindCharInString(const String:str[], c, bool:reverse = false) dvander@2070: { dvander@2070: new i, len dvander@2070: dvander@2070: len = strlen(str); dvander@2070: dvander@2070: if (!reverse) dvander@2070: { dvander@2070: for (i = 0; i < len; i++) dvander@2070: { dvander@2070: if (str[i] == c) dvander@2070: { dvander@2070: return i; dvander@2070: } dvander@2070: } dvander@2070: } dvander@2070: else dvander@2070: { dvander@2070: for (i = len - 1; i >= 0; i--) dvander@2070: { dvander@2070: if (str[i] == c) dvander@2070: { dvander@2070: return i; dvander@2070: } dvander@2070: } dvander@2070: } dvander@2070: dvander@2070: return -1; dvander@2070: } dvander@2070: dvander@2070: /** dvander@749: * Concatenates one string onto another. dvander@749: * dvander@749: * @param buffer String to append to. dvander@749: * @param maxlength Maximum length of entire buffer. dvander@749: * @param source Source string to concatenate. dvander@749: * @return Number of bytes written. dvander@749: */ dvander@749: stock StrCat(String:buffer[], maxlength, const String:source[]) dvander@749: { teame06@1254: new len = strlen(buffer); dvander@749: if (len >= maxlength) dvander@749: { dvander@749: return 0; dvander@749: } dvander@749: dvander@749: return Format(buffer[len], maxlength-len, "%s", source); dvander@749: } ds@830: ds@830: /** dvander@834: * Breaks a string into pieces and stores each piece into an array of buffers. dvander@834: * dvander@834: * @param text The string to split. dvander@834: * @param split The string to use as a split delimiter. dvander@834: * @param buffers An array of string buffers (2D array). dvander@834: * @param maxStrings Number of string buffers (first dimension size). dvander@834: * @param maxStringLength Maximum length of each string buffer. dvander@834: * @return Number of strings retrieved. dvander@834: */ dvander@834: stock ExplodeString(const String:text[], const String:split[], String:buffers[][], maxStrings, maxStringLength) dvander@834: { dvander@834: new reloc_idx, idx, total; dvander@834: dvander@834: if (maxStrings < 1 || split[0] == '\0') dvander@834: { dvander@834: return 0; dvander@834: } dvander@834: dvander@834: while ((idx = SplitString(text[reloc_idx], split, buffers[total], maxStringLength)) != -1) dvander@834: { dvander@834: reloc_idx += idx; dvander@834: if (text[reloc_idx] == '\0') dvander@834: { dvander@834: break; dvander@834: } dvander@834: if (++total >= maxStrings) dvander@834: { dvander@834: return total; dvander@834: } dvander@834: } dvander@834: dvander@834: if (text[reloc_idx] != '\0' && total <= maxStrings - 1) dvander@834: { dvander@834: strcopy(buffers[total++], maxStringLength, text[reloc_idx]); dvander@834: } dvander@834: dvander@834: return total; dvander@834: } dvander@834: dvander@834: /** dvander@834: * Joins an array of strings into one string, with a "join" string inserted in dvander@834: * between each given string. This function complements ExplodeString. dvander@834: * dvander@834: * @param strings An array of strings. dvander@834: * @param numStrings Number of strings in the array. dvander@834: * @param join The join string to insert between each string. dvander@834: * @param buffer Output buffer to write the joined string to. dvander@834: * @param maxLength Maximum length of the output buffer. dvander@834: * @return Number of bytes written to the output buffer. dvander@834: */ dvander@834: stock ImplodeStrings(const String:strings[][], numStrings, const String:join[], String:buffer[], maxLength) dvander@834: { dvander@834: new total, length, part_length; dvander@834: new join_length = strlen(join); dvander@834: for (new i=0; i