plugins/include/string.inc
author Scott Ehlert <ds@alliedmods.net>
Sun May 13 07:48:36 2007 +0000 (2007-05-13)
changeset 784 0ff19a8bd981
parent 752 59887ed3c6e0
child 830 97cd070a7ffa
permissions -rw-r--r--
amb283 - Renamed IsPlayerInGame() to IsClientInGame()
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@748
    22
 * @global Unless otherwise noted, all string functions which take in a 
dvander@748
    23
 * writable buffer and maximum length should have the null terminator INCLUDED
dvander@748
    24
 * in the length.  This means that this is valid: 
dvander@748
    25
 * StrCopy(string, sizeof(string), ...)
dvander@233
    26
 */
dvander@631
    27
 
dvander@233
    28
/**
dvander@233
    29
 * Calculates the length of a string.
dvander@233
    30
 *
ds@475
    31
 * @param str			String to check.
ds@475
    32
 * @return				Length of string, in cells (NOT characters).
dvander@233
    33
 */
dvander@233
    34
native strlen(const String:str[]);
dvander@233
    35
dvander@233
    36
/**
dvander@233
    37
 * Tests whether a string is found inside another string.
dvander@233
    38
 *
dvander@233
    39
 * @param str			String to search in.
dvander@233
    40
 * @param substr		Substring to find inside the original string.
dvander@233
    41
 * @param caseSensitive	If true (default), search is case sensitive.
ds@475
    42
 *						If false, search is case insensitive.
ds@475
    43
 * @return				-1 on failure (no match found). Any other value
ds@475
    44
 *						indicates a position in the string where the match starts.
dvander@233
    45
 */
dvander@233
    46
native StrContains(const String:str[], const String:substr[], bool:caseSensitive=true);
dvander@233
    47
dvander@233
    48
/**
dvander@233
    49
 * Compares two strings lexographically.
dvander@233
    50
 *
dvander@233
    51
 * @param str1			First string (left).
dvander@233
    52
 * @param str2			Second string (right).
dvander@233
    53
 * @param caseSensitive	If true (default), comparison is case sensitive.
ds@475
    54
 *						If false, comparison is case insensitive.
ds@475
    55
 * @return				-1 if str1 < str2
ds@475
    56
 *						0 if str1 == str2
ds@475
    57
 *						1 if str1 > str2
dvander@233
    58
 */
dvander@680
    59
native strcmp(const String:str1[], const String:str2[], bool:caseSensitive=true);
dvander@680
    60
dvander@680
    61
/**
dvander@680
    62
 * Compares two strings parts lexographically.
dvander@680
    63
 *
dvander@680
    64
 * @param str1			First string (left).
dvander@680
    65
 * @param str2			Second string (right).
dvander@680
    66
 * @param num			Number of characters to compare.
dvander@680
    67
 * @param caseSensitive	If true (default), comparison is case sensitive.
dvander@680
    68
 *						If false, comparison is case insensitive.
dvander@680
    69
 * @return				-1 if str1 < str2
dvander@680
    70
 *						0 if str1 == str2
dvander@680
    71
 *						1 if str1 > str2
dvander@680
    72
 */
dvander@680
    73
native strncmp(const String:str1[], const String:str2[], num, bool:caseSensitive=true);
dvander@680
    74
dvander@680
    75
/**
dvander@680
    76
 * Backwards compatible stock - StrCompare is now strcmp
dvander@681
    77
 * @deprecated		Renamed to strcmp
dvander@680
    78
 */
dvander@680
    79
stock StrCompare(const String:str1[], const String:str2[], bool:caseSensitive=true)
dvander@680
    80
{
dvander@680
    81
	return strcmp(str1, str2, caseSensitive);
dvander@680
    82
}
dvander@233
    83
dvander@233
    84
/**
dvander@233
    85
 * Returns whether two strings are equal.
dvander@233
    86
 *
dvander@233
    87
 *
dvander@233
    88
 * @param str1			First string (left).
dvander@233
    89
 * @param str2			Second string (right).
dvander@233
    90
 * @param caseSensitive	If true (default), comparison is case sensitive.
dvander@412
    91
 * 						If false, comparison is case insensitive.
dvander@412
    92
 * @return				True if equal, false otherwise.
dvander@233
    93
 */
faluco@234
    94
stock bool:StrEqual(const String:str1[], const String:str2[], bool:caseSensitive=true)
dvander@233
    95
{
faluco@234
    96
	return (StrCompare(str1, str2, caseSensitive) == 0);
dvander@233
    97
}
dvander@233
    98
dvander@233
    99
/**
dvander@233
   100
 * Copies one string to another string.
dvander@619
   101
 * @note If the destination buffer is too small to hold the source string, the 
dvander@619
   102
 *       destination will be truncated.
dvander@233
   103
 *
dvander@233
   104
 * @param dest			Destination string buffer to copy to.
dvander@233
   105
 * @param destlen		Destination buffer length (includes null terminator).
dvander@233
   106
 * @param source		Source string buffer to copy from.
dvander@412
   107
 * @return				Number of cells written.
dvander@233
   108
 */
dvander@681
   109
native strcopy(String:dest[], destLen, const String:source[]);
dvander@681
   110
dvander@681
   111
/**
dvander@681
   112
 * Backwards compatibility stock - use strcopy
dvander@681
   113
 * @deprecated			Renamed to strcopy
dvander@681
   114
 */
dvander@681
   115
stock StrCopy(String:dest[], destLen, const String:source[])
dvander@681
   116
{
dvander@681
   117
	return strcopy(dest, destLen, source);
dvander@681
   118
}
dvander@233
   119
dvander@233
   120
/**
dvander@257
   121
 * Formats a string according to the SourceMod format rules (see documentation).
dvander@257
   122
 *
dvander@257
   123
 * @param buffer		Destination string buffer.
dvander@257
   124
 * @param maxlength		Maximum length of output string buffer.
dvander@257
   125
 * @param format		Formatting rules.
dvander@257
   126
 * @param ...			Variable number of format parameters.
dvander@412
   127
 * @return				Number of cells written.
dvander@257
   128
 */
ds@634
   129
native Format(String:buffer[], maxlength, const String:format[], any:...);
dvander@257
   130
dvander@257
   131
/**
dvander@257
   132
 * Formats a string according to the SourceMod format rules (see documentation).
dvander@748
   133
 * @note This is the same as Format(), except none of the input buffers can 
dvander@748
   134
 *       overlap the same memory as the output buffer.  Since this security 
dvander@748
   135
 *       check is removed, it is slightly faster.
dvander@257
   136
 *
dvander@257
   137
 * @param buffer		Destination string buffer.
dvander@257
   138
 * @param maxlength		Maximum length of output string buffer.
dvander@257
   139
 * @param format		Formatting rules.
dvander@257
   140
 * @param ...			Variable number of format parameters.
dvander@412
   141
 * @return				Number of cells written.
dvander@257
   142
 */
ds@634
   143
native FormatEx(String:buffer[], maxlength, const String:format[], any:...);
dvander@257
   144
dvander@257
   145
/**
dvander@412
   146
 * Formats a string according to the SourceMod format rules (see documentation).
dvander@748
   147
 * @note This is the same as Format(), except it grabs parameters from a 
dvander@748
   148
 *       parent parameter stack, rather than a local.  This is useful for 
dvander@748
   149
 *       implementing your own variable argument functions.
dvander@412
   150
 *
dvander@412
   151
 * @param buffer		Destination string buffer.
dvander@412
   152
 * @param maxlength		Maximum length of output string buffer.
dvander@412
   153
 * @param format		Formatting rules.
dvander@412
   154
 * @param varpos		Argument number which contains the '...' symbol.
dvander@412
   155
 *						Note: Arguments start at 1.
dvander@412
   156
 * @return 				Number of bytes written.
dvander@412
   157
 */
dvander@412
   158
native VFormat(String:buffer[], maxlength, const String:format[], varpos);
dvander@412
   159
dvander@412
   160
/**
dvander@233
   161
 * Converts a string to an integer.
dvander@233
   162
 *
dvander@233
   163
 * @param str			String to convert.
dvander@412
   164
 * @param nBase			Numerical base to use.  10 is default.
dvander@412
   165
 * @return				Integer conversion of string, or 0 on failure.
dvander@233
   166
 */
dvander@233
   167
native StringToInt(const String:str[], nBase=10);
dvander@233
   168
dvander@233
   169
/**
dvander@233
   170
 * Converts an integer to a string.
dvander@233
   171
 *
dvander@451
   172
 * @param num			Integer to convert.
dvander@233
   173
 * @param str			Buffer to store string in.
dvander@233
   174
 * @param maxlength		Maximum length of string buffer.
dvander@412
   175
 * @return				Number of cells written to buffer.
dvander@233
   176
 */
dvander@451
   177
native IntToString(num, String:str[], maxlength);
dvander@233
   178
dvander@233
   179
/** 
dvander@233
   180
 * Converts a string to a floating point number.
dvander@233
   181
 *
dvander@233
   182
 * @param str			String to convert to a foat.
dvander@412
   183
 * @return				Floating point result, or 0.0 on error.
dvander@233
   184
 */
dvander@233
   185
native Float:StringToFloat(const String:str[]);
dvander@233
   186
dvander@233
   187
/**
dvander@233
   188
 * Converts a floating point number to a string.
dvander@233
   189
 *
ds@454
   190
 * @param num			Floating point number to convert.
dvander@233
   191
 * @param str			Buffer to store string in.
dvander@233
   192
 * @param maxlength		Maximum length of string buffer.
dvander@412
   193
 * @return				Number of cells written to buffer.
dvander@233
   194
 */
ds@454
   195
native FloatToString(Float:num, String:str[], maxlength);
dvander@631
   196
dvander@631
   197
/**
dvander@631
   198
 * Finds the first "argument" in a string; either a set of space
dvander@631
   199
 * terminated characters, or a fully quoted string.  After the 
dvander@631
   200
 * argument is found, whitespace is read until the next portion
dvander@631
   201
 * of the string is reached.  If nothing remains, -1 is returned.
dvander@631
   202
 * Otherwise, the index to the first character is returned.
dvander@631
   203
 *
dvander@631
   204
 * @param source		Source input string.
dvander@631
   205
 * @param arg			Stores argument read from string.
dvander@631
   206
 * @param argLen		Maximum length of argument buffer.
dvander@631
   207
 * @return				Index to next piece of string, or -1 if none.
dvander@631
   208
 */
ds@634
   209
native StrBreak(const String:source[], String:arg[], argLen);
dvander@748
   210
dvander@748
   211
/** 
dvander@748
   212
 * Returns the number of bytes a character is using.  This is
dvander@748
   213
 * for multi-byte characters (UTF-8).  For normal ASCII characters,
dvander@748
   214
 * this will return 1.
dvander@748
   215
 *
dvander@748
   216
 * @param source		Source input string.
dvander@748
   217
 * @return				Number of bytes the current character uses.
dvander@748
   218
 */
dvander@748
   219
native GetCharBytes(const String:source[]);
dvander@748
   220
dvander@748
   221
/**
dvander@748
   222
 * Returns whether a character is an ASCII alphabet character.
dvander@748
   223
 *
dvander@748
   224
 * @note Multi-byte characters will always return false.
dvander@748
   225
 *
dvander@748
   226
 * @param char			Character to test.
dvander@748
   227
 * @return				True if character is alphabetical, otherwise false.
dvander@748
   228
 */
dvander@748
   229
native bool:IsCharAlpha(chr);
dvander@748
   230
dvander@748
   231
/**
dvander@748
   232
 * Returns whether a character is numeric.
dvander@748
   233
 *
dvander@748
   234
 * @note Multi-byte characters will always return false.
dvander@748
   235
 *
dvander@748
   236
 * @param char			Character to test.
dvander@748
   237
 * @return				True if character is numeric, otherwise false.
dvander@748
   238
 */
dvander@748
   239
native bool:IsCharNumeric(chr);
dvander@748
   240
dvander@748
   241
/**
dvander@748
   242
 * Returns whether a character is whitespace.
dvander@748
   243
 *
dvander@748
   244
 * @note Multi-byte characters will always return false.
dvander@748
   245
 *
dvander@748
   246
 * @param char			Character to test.
dvander@748
   247
 * @return				True if character is whitespace, otherwise false.
dvander@748
   248
 */
dvander@748
   249
native bool:IsCharSpace(chr);
dvander@748
   250
dvander@748
   251
/**
dvander@748
   252
 * Returns if a character is multi-byte or not.
dvander@748
   253
 *
dvander@748
   254
 * @param char			Character to test.
dvander@748
   255
 * @return				0 for a normal 7-bit ASCII character,
dvander@748
   256
 *						otherwise number of bytes in multi-byte character.
dvander@748
   257
 */
dvander@748
   258
native IsCharMB(chr);
dvander@748
   259
dvander@748
   260
/**
dvander@748
   261
 * Returns whether an alphabetic character is uppercase.
dvander@748
   262
 *
dvander@748
   263
 * @note Multi-byte characters will always return false.
dvander@748
   264
 *
dvander@748
   265
 * @param char			Character to test.
dvander@748
   266
 * @return				True if character is uppercase, otherwise false.
dvander@748
   267
 */
dvander@748
   268
native bool:IsCharUpper(chr);
dvander@748
   269
dvander@748
   270
/**
dvander@748
   271
 * Returns whether an alphabetic character is lowercase.
dvander@748
   272
 *
dvander@748
   273
 * @note Multi-byte characters will always return false.
dvander@748
   274
 *
dvander@748
   275
 * @param char			Character to test.
dvander@748
   276
 * @return				True if character is lowercase, otherwise false.
dvander@748
   277
 */
dvander@748
   278
native bool:IsCharLower(chr);
dvander@748
   279
dvander@748
   280
/**
dvander@748
   281
 * Returns an uppercase character to a lowercase character.
dvander@748
   282
 *
dvander@748
   283
 * @param chr			Characer to convert.
dvander@748
   284
 * @return				Lowercase character on success, 
dvander@748
   285
 *						no change on failure.
dvander@748
   286
 */
dvander@748
   287
stock CharToUpper(chr)
dvander@748
   288
{
dvander@748
   289
	if (IsCharLower(chr))
dvander@748
   290
	{
dvander@748
   291
		return (chr & ~(1<<5));
dvander@748
   292
	}
dvander@748
   293
	return chr;
dvander@748
   294
}
dvander@748
   295
dvander@748
   296
/**
dvander@748
   297
 * Returns a lowercase character to an uppercase character.
dvander@748
   298
 *
dvander@748
   299
 * @param chr			Characer to convert.
dvander@748
   300
 * @return				Uppercase character on success, 
dvander@748
   301
 *						no change on failure.
dvander@748
   302
 */
dvander@748
   303
stock CharToLower(chr)
dvander@748
   304
{
dvander@748
   305
	if (IsCharUpper(chr))
dvander@748
   306
	{
dvander@748
   307
		return (chr | (1<<5));
dvander@748
   308
	}
dvander@748
   309
	return chr;
dvander@748
   310
}
dvander@748
   311
dvander@749
   312
/**
dvander@749
   313
 * Concatenates one string onto another.
dvander@749
   314
 *
dvander@749
   315
 * @param buffer		String to append to.
dvander@749
   316
 * @param maxlength		Maximum length of entire buffer.
dvander@749
   317
 * @param source		Source string to concatenate.
dvander@749
   318
 * @return				Number of bytes written.
dvander@749
   319
 */
dvander@749
   320
stock StrCat(String:buffer[], maxlength, const String:source[])
dvander@749
   321
{
dvander@749
   322
	new len = strlen(source);
dvander@749
   323
	if (len >= maxlength)
dvander@749
   324
	{
dvander@749
   325
		return 0;
dvander@749
   326
	}
dvander@749
   327
	
dvander@749
   328
	return Format(buffer[len], maxlength-len, "%s", source);
dvander@749
   329
}