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