plugins/include/string.inc
author David Anderson <dvander@alliedmods.net>
Wed Apr 11 16:24:50 2007 +0000 (2007-04-11)
changeset 680 b5f7893fd4f6
parent 634 85108309e453
child 681 02f6c1c5fd06
permissions -rw-r--r--
added strncmp
renamed StrCompare to strcmp
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@680
    76
 */
dvander@680
    77
stock StrCompare(const String:str1[], const String:str2[], bool:caseSensitive=true)
dvander@680
    78
{
dvander@680
    79
	return strcmp(str1, str2, caseSensitive);
dvander@680
    80
}
dvander@233
    81
dvander@233
    82
/**
dvander@233
    83
 * Returns whether two strings are equal.
dvander@233
    84
 *
dvander@233
    85
 *
dvander@233
    86
 * @param str1			First string (left).
dvander@233
    87
 * @param str2			Second string (right).
dvander@233
    88
 * @param caseSensitive	If true (default), comparison is case sensitive.
dvander@412
    89
 * 						If false, comparison is case insensitive.
dvander@412
    90
 * @return				True if equal, false otherwise.
dvander@233
    91
 */
faluco@234
    92
stock bool:StrEqual(const String:str1[], const String:str2[], bool:caseSensitive=true)
dvander@233
    93
{
faluco@234
    94
	return (StrCompare(str1, str2, caseSensitive) == 0);
dvander@233
    95
}
dvander@233
    96
dvander@233
    97
/**
dvander@233
    98
 * Copies one string to another string.
dvander@619
    99
 * @note If the destination buffer is too small to hold the source string, the 
dvander@619
   100
 *       destination will be truncated.
dvander@233
   101
 *
dvander@233
   102
 * @param dest			Destination string buffer to copy to.
dvander@233
   103
 * @param destlen		Destination buffer length (includes null terminator).
dvander@233
   104
 * @param source		Source string buffer to copy from.
dvander@412
   105
 * @return				Number of cells written.
dvander@233
   106
 */
dvander@233
   107
native StrCopy(String:dest[], destLen, const String:source[]);
dvander@233
   108
dvander@233
   109
/**
dvander@257
   110
 * Formats a string according to the SourceMod format rules (see documentation).
dvander@257
   111
 *
dvander@257
   112
 * @param buffer		Destination string buffer.
dvander@257
   113
 * @param maxlength		Maximum length of output string buffer.
dvander@257
   114
 * @param format		Formatting rules.
dvander@257
   115
 * @param ...			Variable number of format parameters.
dvander@412
   116
 * @return				Number of cells written.
dvander@257
   117
 */
ds@634
   118
native Format(String:buffer[], maxlength, const String:format[], any:...);
dvander@257
   119
dvander@257
   120
/**
dvander@257
   121
 * Formats a string according to the SourceMod format rules (see documentation).
dvander@257
   122
 * @note This is the same as Format(), except none of the input buffers can overlap the same
dvander@619
   123
 *	     memory as the output buffer.  Since this security check is removed, it is slightly faster.
dvander@257
   124
 *
dvander@257
   125
 * @param buffer		Destination string buffer.
dvander@257
   126
 * @param maxlength		Maximum length of output string buffer.
dvander@257
   127
 * @param format		Formatting rules.
dvander@257
   128
 * @param ...			Variable number of format parameters.
dvander@412
   129
 * @return				Number of cells written.
dvander@257
   130
 */
ds@634
   131
native FormatEx(String:buffer[], maxlength, const String:format[], any:...);
dvander@257
   132
dvander@257
   133
/**
dvander@412
   134
 * Formats a string according to the SourceMod format rules (see documentation).
dvander@412
   135
 * @note This is the same as Format(), except it grabs parameters from a parent parameter
dvander@619
   136
 *       stack, rather than a local.  This is useful for implementing your own variable 
dvander@619
   137
 *       argument functions.
dvander@412
   138
 *
dvander@412
   139
 * @param buffer		Destination string buffer.
dvander@412
   140
 * @param maxlength		Maximum length of output string buffer.
dvander@412
   141
 * @param format		Formatting rules.
dvander@412
   142
 * @param varpos		Argument number which contains the '...' symbol.
dvander@412
   143
 *						Note: Arguments start at 1.
dvander@412
   144
 * @return 				Number of bytes written.
dvander@412
   145
 */
dvander@412
   146
native VFormat(String:buffer[], maxlength, const String:format[], varpos);
dvander@412
   147
dvander@412
   148
/**
dvander@233
   149
 * Converts a string to an integer.
dvander@233
   150
 *
dvander@233
   151
 * @param str			String to convert.
dvander@412
   152
 * @param nBase			Numerical base to use.  10 is default.
dvander@412
   153
 * @return				Integer conversion of string, or 0 on failure.
dvander@233
   154
 */
dvander@233
   155
native StringToInt(const String:str[], nBase=10);
dvander@233
   156
dvander@233
   157
/**
dvander@233
   158
 * Converts an integer to a string.
dvander@233
   159
 *
dvander@451
   160
 * @param num			Integer to convert.
dvander@233
   161
 * @param str			Buffer to store string in.
dvander@233
   162
 * @param maxlength		Maximum length of string buffer.
dvander@412
   163
 * @return				Number of cells written to buffer.
dvander@233
   164
 */
dvander@451
   165
native IntToString(num, String:str[], maxlength);
dvander@233
   166
dvander@233
   167
/** 
dvander@233
   168
 * Converts a string to a floating point number.
dvander@233
   169
 *
dvander@233
   170
 * @param str			String to convert to a foat.
dvander@412
   171
 * @return				Floating point result, or 0.0 on error.
dvander@233
   172
 */
dvander@233
   173
native Float:StringToFloat(const String:str[]);
dvander@233
   174
dvander@233
   175
/**
dvander@233
   176
 * Converts a floating point number to a string.
dvander@233
   177
 *
ds@454
   178
 * @param num			Floating point number to convert.
dvander@233
   179
 * @param str			Buffer to store string in.
dvander@233
   180
 * @param maxlength		Maximum length of string buffer.
dvander@412
   181
 * @return				Number of cells written to buffer.
dvander@233
   182
 */
ds@454
   183
native FloatToString(Float:num, String:str[], maxlength);
dvander@631
   184
dvander@631
   185
/**
dvander@631
   186
 * Finds the first "argument" in a string; either a set of space
dvander@631
   187
 * terminated characters, or a fully quoted string.  After the 
dvander@631
   188
 * argument is found, whitespace is read until the next portion
dvander@631
   189
 * of the string is reached.  If nothing remains, -1 is returned.
dvander@631
   190
 * Otherwise, the index to the first character is returned.
dvander@631
   191
 *
dvander@631
   192
 * @param source		Source input string.
dvander@631
   193
 * @param arg			Stores argument read from string.
dvander@631
   194
 * @param argLen		Maximum length of argument buffer.
dvander@631
   195
 * @return				Index to next piece of string, or -1 if none.
dvander@631
   196
 */
ds@634
   197
native StrBreak(const String:source[], String:arg[], argLen);