plugins/include/string.inc
author David Anderson <dvander@alliedmods.net>
Tue May 22 20:50:48 2007 +0000 (2007-05-22)
changeset 833 732bedfcbe3e
parent 830 97cd070a7ffa
child 834 d6fac170d744
permissions -rw-r--r--
added ReplaceString and ReplaceStringEx (phew!)
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
 * @param str1			First string (left).
dvander@233
    88
 * @param str2			Second string (right).
dvander@233
    89
 * @param caseSensitive	If true (default), comparison is case sensitive.
dvander@412
    90
 * 						If false, comparison is case insensitive.
dvander@412
    91
 * @return				True if equal, false otherwise.
dvander@233
    92
 */
faluco@234
    93
stock bool:StrEqual(const String:str1[], const String:str2[], bool:caseSensitive=true)
dvander@233
    94
{
faluco@234
    95
	return (StrCompare(str1, str2, caseSensitive) == 0);
dvander@233
    96
}
dvander@233
    97
dvander@233
    98
/**
dvander@233
    99
 * Copies one string to another string.
dvander@619
   100
 * @note If the destination buffer is too small to hold the source string, the 
dvander@619
   101
 *       destination will be truncated.
dvander@233
   102
 *
dvander@233
   103
 * @param dest			Destination string buffer to copy to.
dvander@233
   104
 * @param destlen		Destination buffer length (includes null terminator).
dvander@233
   105
 * @param source		Source string buffer to copy from.
dvander@412
   106
 * @return				Number of cells written.
dvander@233
   107
 */
dvander@681
   108
native strcopy(String:dest[], destLen, const String:source[]);
dvander@681
   109
dvander@681
   110
/**
dvander@681
   111
 * Backwards compatibility stock - use strcopy
dvander@681
   112
 * @deprecated			Renamed to strcopy
dvander@681
   113
 */
dvander@681
   114
stock StrCopy(String:dest[], destLen, const String:source[])
dvander@681
   115
{
dvander@681
   116
	return strcopy(dest, destLen, source);
dvander@681
   117
}
dvander@233
   118
dvander@233
   119
/**
dvander@257
   120
 * Formats a string according to the SourceMod format rules (see documentation).
dvander@257
   121
 *
dvander@257
   122
 * @param buffer		Destination string buffer.
dvander@257
   123
 * @param maxlength		Maximum length of output string buffer.
dvander@257
   124
 * @param format		Formatting rules.
dvander@257
   125
 * @param ...			Variable number of format parameters.
dvander@412
   126
 * @return				Number of cells written.
dvander@257
   127
 */
ds@634
   128
native Format(String:buffer[], maxlength, const String:format[], any:...);
dvander@257
   129
dvander@257
   130
/**
dvander@257
   131
 * Formats a string according to the SourceMod format rules (see documentation).
dvander@748
   132
 * @note This is the same as Format(), except none of the input buffers can 
dvander@748
   133
 *       overlap the same memory as the output buffer.  Since this security 
dvander@748
   134
 *       check is removed, it is slightly faster.
dvander@257
   135
 *
dvander@257
   136
 * @param buffer		Destination string buffer.
dvander@257
   137
 * @param maxlength		Maximum length of output string buffer.
dvander@257
   138
 * @param format		Formatting rules.
dvander@257
   139
 * @param ...			Variable number of format parameters.
dvander@412
   140
 * @return				Number of cells written.
dvander@257
   141
 */
ds@634
   142
native FormatEx(String:buffer[], maxlength, const String:format[], any:...);
dvander@257
   143
dvander@257
   144
/**
dvander@412
   145
 * Formats a string according to the SourceMod format rules (see documentation).
dvander@748
   146
 * @note This is the same as Format(), except it grabs parameters from a 
dvander@748
   147
 *       parent parameter stack, rather than a local.  This is useful for 
dvander@748
   148
 *       implementing your own variable argument functions.
dvander@412
   149
 *
dvander@412
   150
 * @param buffer		Destination string buffer.
dvander@412
   151
 * @param maxlength		Maximum length of output string buffer.
dvander@412
   152
 * @param format		Formatting rules.
dvander@412
   153
 * @param varpos		Argument number which contains the '...' symbol.
dvander@412
   154
 *						Note: Arguments start at 1.
dvander@412
   155
 * @return 				Number of bytes written.
dvander@412
   156
 */
dvander@412
   157
native VFormat(String:buffer[], maxlength, const String:format[], varpos);
dvander@412
   158
dvander@412
   159
/**
dvander@233
   160
 * Converts a string to an integer.
dvander@233
   161
 *
dvander@233
   162
 * @param str			String to convert.
dvander@412
   163
 * @param nBase			Numerical base to use.  10 is default.
dvander@412
   164
 * @return				Integer conversion of string, or 0 on failure.
dvander@233
   165
 */
dvander@233
   166
native StringToInt(const String:str[], nBase=10);
dvander@233
   167
dvander@233
   168
/**
dvander@233
   169
 * Converts an integer to a string.
dvander@233
   170
 *
dvander@451
   171
 * @param num			Integer to convert.
dvander@233
   172
 * @param str			Buffer to store string in.
dvander@233
   173
 * @param maxlength		Maximum length of string buffer.
dvander@412
   174
 * @return				Number of cells written to buffer.
dvander@233
   175
 */
dvander@451
   176
native IntToString(num, String:str[], maxlength);
dvander@233
   177
dvander@233
   178
/** 
dvander@233
   179
 * Converts a string to a floating point number.
dvander@233
   180
 *
dvander@233
   181
 * @param str			String to convert to a foat.
dvander@412
   182
 * @return				Floating point result, or 0.0 on error.
dvander@233
   183
 */
dvander@233
   184
native Float:StringToFloat(const String:str[]);
dvander@233
   185
dvander@233
   186
/**
dvander@233
   187
 * Converts a floating point number to a string.
dvander@233
   188
 *
ds@454
   189
 * @param num			Floating point number to convert.
dvander@233
   190
 * @param str			Buffer to store string in.
dvander@233
   191
 * @param maxlength		Maximum length of string buffer.
dvander@412
   192
 * @return				Number of cells written to buffer.
dvander@233
   193
 */
ds@454
   194
native FloatToString(Float:num, String:str[], maxlength);
dvander@631
   195
dvander@631
   196
/**
dvander@631
   197
 * Finds the first "argument" in a string; either a set of space
dvander@631
   198
 * terminated characters, or a fully quoted string.  After the 
dvander@631
   199
 * argument is found, whitespace is read until the next portion
dvander@631
   200
 * of the string is reached.  If nothing remains, -1 is returned.
dvander@631
   201
 * Otherwise, the index to the first character is returned.
dvander@631
   202
 *
dvander@631
   203
 * @param source		Source input string.
dvander@631
   204
 * @param arg			Stores argument read from string.
dvander@631
   205
 * @param argLen		Maximum length of argument buffer.
dvander@631
   206
 * @return				Index to next piece of string, or -1 if none.
dvander@631
   207
 */
ds@830
   208
native BreakString(const String:source[], String:arg[], argLen);
ds@830
   209
ds@830
   210
/**
ds@830
   211
 * Backwards compatibility stock - use BreakString
ds@830
   212
 * @deprecated			Renamed to BreakString.
ds@830
   213
 */
ds@830
   214
stock StrBreak(const String:source[], String:arg[], argLen)
ds@830
   215
{
ds@830
   216
	return BreakString(source, arg, argLen);
ds@830
   217
}
dvander@748
   218
dvander@833
   219
/**
dvander@833
   220
 * Given a string, replaces all occurrences of a search string with a 
dvander@833
   221
 * replacement string.
dvander@833
   222
 *
dvander@833
   223
 * @param text			String to perform search and replacements on.
dvander@833
   224
 * @param maxlength		Maximum length of the string buffer.
dvander@833
   225
 * @param search		String to search for.
dvander@833
   226
 * @param replace		String to replace the search string with.
dvander@833
   227
 * @return				Number of replacements that were performed.
dvander@833
   228
 */
dvander@833
   229
native ReplaceString(String:text[], maxlength, const String:search[], const String:replace[]);
dvander@833
   230
dvander@833
   231
/**
dvander@833
   232
 * Given a string, replaces the first occurrence of a search string with a 
dvander@833
   233
 * replacement string.
dvander@833
   234
 *
dvander@833
   235
 * @param text			String to perform search and replacements on.
dvander@833
   236
 * @param maxlength		Maximum length of the string buffer.
dvander@833
   237
 * @param search		String to search for.
dvander@833
   238
 * @param replace		String to replace the search string with.
dvander@833
   239
 * @param searchLen		If higher than -1, its value will be used instead of
dvander@833
   240
 *						a strlen() call on the search parameter.
dvander@833
   241
 * @param replaceLen	If higher than -1, its value will be used instead of
dvander@833
   242
 *						a strlen() call on the replace parameter.
dvander@833
   243
 * @return				Index into the buffer (relative to the start) from where
dvander@833
   244
 *						the last replacement ended, or -1 if no replacements were
dvander@833
   245
 *						made.
dvander@833
   246
 */
dvander@833
   247
native ReplaceStringEx(String:text[], maxlength, const String:search[], const String:replace[], searchLen=-1, replaceLen=-1);
dvander@833
   248
dvander@748
   249
/** 
dvander@748
   250
 * Returns the number of bytes a character is using.  This is
dvander@748
   251
 * for multi-byte characters (UTF-8).  For normal ASCII characters,
dvander@748
   252
 * this will return 1.
dvander@748
   253
 *
dvander@748
   254
 * @param source		Source input string.
dvander@748
   255
 * @return				Number of bytes the current character uses.
dvander@748
   256
 */
dvander@748
   257
native GetCharBytes(const String:source[]);
dvander@748
   258
dvander@748
   259
/**
dvander@748
   260
 * Returns whether a character is an ASCII alphabet character.
dvander@748
   261
 *
dvander@748
   262
 * @note Multi-byte characters will always return false.
dvander@748
   263
 *
dvander@748
   264
 * @param char			Character to test.
dvander@748
   265
 * @return				True if character is alphabetical, otherwise false.
dvander@748
   266
 */
dvander@748
   267
native bool:IsCharAlpha(chr);
dvander@748
   268
dvander@748
   269
/**
dvander@748
   270
 * Returns whether a character is numeric.
dvander@748
   271
 *
dvander@748
   272
 * @note Multi-byte characters will always return false.
dvander@748
   273
 *
dvander@748
   274
 * @param char			Character to test.
dvander@748
   275
 * @return				True if character is numeric, otherwise false.
dvander@748
   276
 */
dvander@748
   277
native bool:IsCharNumeric(chr);
dvander@748
   278
dvander@748
   279
/**
dvander@748
   280
 * Returns whether a character is whitespace.
dvander@748
   281
 *
dvander@748
   282
 * @note Multi-byte characters will always return false.
dvander@748
   283
 *
dvander@748
   284
 * @param char			Character to test.
dvander@748
   285
 * @return				True if character is whitespace, otherwise false.
dvander@748
   286
 */
dvander@748
   287
native bool:IsCharSpace(chr);
dvander@748
   288
dvander@748
   289
/**
dvander@748
   290
 * Returns if a character is multi-byte or not.
dvander@748
   291
 *
dvander@748
   292
 * @param char			Character to test.
dvander@748
   293
 * @return				0 for a normal 7-bit ASCII character,
dvander@748
   294
 *						otherwise number of bytes in multi-byte character.
dvander@748
   295
 */
dvander@748
   296
native IsCharMB(chr);
dvander@748
   297
dvander@748
   298
/**
dvander@748
   299
 * Returns whether an alphabetic character is uppercase.
dvander@748
   300
 *
dvander@748
   301
 * @note Multi-byte characters will always return false.
dvander@748
   302
 *
dvander@748
   303
 * @param char			Character to test.
dvander@748
   304
 * @return				True if character is uppercase, otherwise false.
dvander@748
   305
 */
dvander@748
   306
native bool:IsCharUpper(chr);
dvander@748
   307
dvander@748
   308
/**
dvander@748
   309
 * Returns whether an alphabetic character is lowercase.
dvander@748
   310
 *
dvander@748
   311
 * @note Multi-byte characters will always return false.
dvander@748
   312
 *
dvander@748
   313
 * @param char			Character to test.
dvander@748
   314
 * @return				True if character is lowercase, otherwise false.
dvander@748
   315
 */
dvander@748
   316
native bool:IsCharLower(chr);
dvander@748
   317
dvander@748
   318
/**
dvander@748
   319
 * Returns an uppercase character to a lowercase character.
dvander@748
   320
 *
dvander@748
   321
 * @param chr			Characer to convert.
dvander@748
   322
 * @return				Lowercase character on success, 
dvander@748
   323
 *						no change on failure.
dvander@748
   324
 */
dvander@748
   325
stock CharToUpper(chr)
dvander@748
   326
{
dvander@748
   327
	if (IsCharLower(chr))
dvander@748
   328
	{
dvander@748
   329
		return (chr & ~(1<<5));
dvander@748
   330
	}
dvander@748
   331
	return chr;
dvander@748
   332
}
dvander@748
   333
dvander@748
   334
/**
dvander@748
   335
 * Returns a lowercase character to an uppercase character.
dvander@748
   336
 *
dvander@748
   337
 * @param chr			Characer to convert.
dvander@748
   338
 * @return				Uppercase character on success, 
dvander@748
   339
 *						no change on failure.
dvander@748
   340
 */
dvander@748
   341
stock CharToLower(chr)
dvander@748
   342
{
dvander@748
   343
	if (IsCharUpper(chr))
dvander@748
   344
	{
dvander@748
   345
		return (chr | (1<<5));
dvander@748
   346
	}
dvander@748
   347
	return chr;
dvander@748
   348
}
dvander@748
   349
dvander@749
   350
/**
dvander@749
   351
 * Concatenates one string onto another.
dvander@749
   352
 *
dvander@749
   353
 * @param buffer		String to append to.
dvander@749
   354
 * @param maxlength		Maximum length of entire buffer.
dvander@749
   355
 * @param source		Source string to concatenate.
dvander@749
   356
 * @return				Number of bytes written.
dvander@749
   357
 */
dvander@749
   358
stock StrCat(String:buffer[], maxlength, const String:source[])
dvander@749
   359
{
dvander@749
   360
	new len = strlen(source);
dvander@749
   361
	if (len >= maxlength)
dvander@749
   362
	{
dvander@749
   363
		return 0;
dvander@749
   364
	}
dvander@749
   365
	
dvander@749
   366
	return Format(buffer[len], maxlength-len, "%s", source);
dvander@749
   367
}
ds@830
   368
ds@830
   369
/**
ds@830
   370
 * Removes whitespace characters from the beginning and end of a string.
ds@830
   371
 *
ds@830
   372
 * @param str			The string to trim.
ds@830
   373
 * @return				Number of bytes written (UTF-8 safe).
ds@830
   374
 */
ds@830
   375
native TrimString(String:str[]);