[Half-Life AMXX] / include / string.inc Repository:
ViewVC logotype

Annotation of /include/string.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download)

1 : ian 1 /* Strings manipulation
2 :     *
3 :     * by the AMX Mod X Development Team
4 :     * originally developed by OLO
5 :     *
6 :     * This file is provided as is (no warranties).
7 :     */
8 :    
9 :     #if defined _string_included
10 :     #endinput
11 :     #endif
12 :     #define _string_included
13 :    
14 :     /* Checks if source contains string. On success function
15 :     * returns position in source, on failure returns -1. */
16 :     native contain(const source[],const string[]);
17 :    
18 :     /* Checks if source contains string with case ignoring. On success function
19 :     * returns position in source, on failure returns -1. */
20 :     native containi(const source[],const string[]);
21 :    
22 :     /* Replaces given string to another in given text. */
23 :     native replace(text[], len, const what[], const with[]);
24 :    
25 :     /* Adds one string to another. Last parameter different from 0, specifies
26 :     * how many chars we want to add. Function returns number of all merged chars. */
27 :     native add(dest[],len,const src[],max=0);
28 :    
29 :     /* Fills string with given format and parameters.
30 :     * Function returns number of copied chars.
31 :     * Example: format(dest,"Hello %s. You are %d years old","Tom",17).
32 :     * If any of your input buffers overlap with the destination buffer,
33 :     * format() falls back to a "copy-back" version as of 1.65. This is
34 :     * slower, so you should using a source string that is the same as
35 :     * the destination.
36 :     */
37 :     native format(output[] ,len ,const format[] , {Float,Sql,Result,_}:...);
38 :    
39 :     /* Same as format(), except does not perform a "copy back" check.
40 :     * This means formatex() is faster, but DOES NOT ALLOW this type
41 :     * of call:
42 :     * formatex(buffer, len, "%s", buffer)
43 :     * formatex(buffer, len, buffer, buffer)
44 :     * formatex(buffer, len, "%s", buffer[5])
45 :     * This is because the output is directly stored into "buffer",
46 :     * rather than copied back at the end.
47 :     */
48 :     native formatex(output[] ,len ,const format[] , {Float,Sql,Result,_}:...);
49 :    
50 :     /* Replacement for format_args. Much faster and %L compatible.
51 :     * This works exactly like vsnprintf() from C.
52 :     * You must pass in the output buffer and its size,
53 :     * the string to format, and the number of the FIRST variable
54 :     * argument parameter. For example, for:
55 :     * function (a, b, c, ...)
56 :     * You would pass 4 (a is 1, b is 2, c is 3, et cetera).
57 :     * There is no vformatex().
58 :     */
59 :     native vformat(buffer[], len, const fmt[], vararg);
60 :    
61 :     /*
62 :     * Same as vformat(), except works in normal style dynamic natives.
63 :     * Instead of passing the format arg string, you can only pass the
64 :     * actual format argument number itself.
65 :     * If you pass 0, it will read the format string from an optional
66 :     * fifth parameter.
67 :     */
68 :     native vdformat(buffer[], len, fmt_arg, vararg, ...);
69 :    
70 :     /* Gets parameters from function as formated string. */
71 :     native format_args(output[] ,len ,pos = 0);
72 :    
73 :     /* Converts number to string. */
74 :     native num_to_str(num,string[],len);
75 :    
76 :     /* Returns converted string to number. */
77 :     native str_to_num(const string[]);
78 :    
79 :     /* Converts float to string. */
80 :     native float_to_str(Float:fl, string[], len);
81 :    
82 :     /* Parses a float. */
83 :     native Float:str_to_float(const string[]);
84 :    
85 :     /* Checks if two strings equal. If len var is set
86 :     * then there are only c chars comapred. */
87 :     native equal(const a[],const b[],c=0);
88 :    
89 :     /* Checks if two strings equal with case ignoring.
90 :     * If len var is set then there are only c chars comapred. */
91 :     native equali(const a[],const b[],c=0);
92 :    
93 :     /* Copies one string to another. By len var
94 :     * you may specify max. number of chars to copy. */
95 :     native copy(dest[],len,const src[]);
96 :    
97 :     /* Copies one string to another until char ch is found.
98 :     * By len var you may specify max. number of chars to copy. */
99 :     native copyc(dest[],len,const src[],ch);
100 :    
101 :     /* Sets string with given character. */
102 :     native setc(src[],len,ch);
103 :    
104 :     /* Gets parameters from text.
105 :     * Example: to split text: "^"This is^" the best year",
106 :     * call function like this: parse(text,arg1,len1,arg2,len2,arg3,len3,arg4,len4)
107 :     * and you will get: "This is", "the", "best", "year"
108 :     * Function returns number of parsed parameters. */
109 :     native parse(const text[], ... );
110 :    
111 :     /* Breaks a string into two halves, by token.
112 :     See strbreak() for doing this with parameters.
113 :     Example:
114 :     str1[] = This *is*some text
115 :     strtok(str1, left, 24, right, 24, '*')
116 :     left will be "This "
117 :     Right will be "is*some text"
118 :     If you use trimSpaces, all spaces are trimmed from Left.
119 :     */
120 :     native strtok(const text[], Left[], leftLen, Right[], rightLen, token=' ', trimSpaces=0);
121 :    
122 :    
123 :     /* Gets parameters from text one at a time
124 :     It breaks a string into the first parameter and the rest of the parameters
125 :     (A left side and right side of the string)
126 :     Example: to split text: "^"This is^" the best year",
127 :     strbreak(text, arg1, len1, arg2, len2)
128 :     arg1="This is", arg2=the best year
129 :     This is more useful than parse() because you can keep breaking
130 :     any number of arguments */
131 :     native strbreak(const text[], Left[], leftLen, Right[], rightLen);
132 :    
133 :     /* Strips spaces from the beginning and end of a string. */
134 :     native trim(text[]);
135 :    
136 :     /* Converts all chars in string to lower case. */
137 :     native strtolower(string[]);
138 :    
139 :     /* Converts all chars in string to upper case. */
140 :     native strtoupper(string[]);
141 :    
142 :     /* Make a string's first character uppercase */
143 :     native ucfirst(string[]);
144 :    
145 :     /* Returns true when value is digit. */
146 :     native isdigit(ch);
147 :    
148 :     /* Returns true when value is letter. */
149 :     native isalpha(ch);
150 :    
151 :     /* Returns true when value is space. */
152 :     native isspace(ch);
153 :    
154 :     /* Returns true when value is letter or digit. */
155 :     native isalnum(ch);
156 :    
157 :     /* Concatenates a string. Maxlength is the total buffer of the destination. */
158 :     native strcat(dest[], const source[], maxlength);
159 :    
160 :     /* Finds a string in another string. Returns -1 if not found. */
161 :     native strfind(const string[], const sub[], ignorecase=0, pos=0);
162 :    
163 :     /* Compares two strings with the C function strcmp(). Returns 0 on equal. */
164 :     native strcmp(const string1[], const string2[], ignorecase=0);
165 :    
166 :     /* Tests if given string contains only digits. Also, returns false for zero-length strings. */
167 :     stock bool:is_str_num(const sString[])
168 :     {
169 :     new i = 0;
170 :    
171 :     while (sString[i] && isdigit(sString[i]))
172 :     ++i;
173 :    
174 :     return sString[i] == 0 && i != 0;
175 :     }
176 :    
177 :     /* It is basically strbreak but you have a delimiter that is more than one character in length.
178 :     You pass the Input string, the Left output, the max length of the left output,
179 :     the right output , the max right length, and then the delimiter string.
180 :     By Suicid3
181 :     */
182 :     stock split(const szInput[], szLeft[], pL_Max, szRight[], pR_Max, const szDelim[])
183 :     {
184 :     new iEnd = contain(szInput, szDelim);
185 :     new iStart = iEnd + strlen(szDelim);
186 :    
187 :     //If delimiter isnt in Input just split the string at max lengths
188 :     if (iEnd == -1)
189 :     {
190 :     iStart = copy(szLeft, pL_Max, szInput);
191 :     copy(szRight, pR_Max, szInput[iStart]);
192 :     return;
193 :     }
194 :    
195 :     //If delimter is in Input then split at input for max lengths
196 :     if (pL_Max >= iEnd)
197 :     copy(szLeft, iEnd, szInput);
198 :     else
199 :     copy(szLeft, pL_Max, szInput);
200 :    
201 :     copy(szRight, pR_Max, szInput[iStart]);
202 :    
203 :     return;
204 :     }
205 :    
206 :     /* Removes a path from szFilePath leaving the name of the file in szFile for a pMax length. */
207 :     stock remove_filepath(const szFilePath[], szFile[], pMax)
208 :     {
209 :     new len = strlen(szFilePath);
210 :    
211 :     while ((--len >= 0) && (szFilePath[len] != '/') && (szFilePath[len] != '\')) { }
212 :    
213 :     copy(szFile, pMax, szFilePath[len + 1]);
214 :    
215 :     return;
216 :     }
217 :    
218 :     /* Replaces a contained string iteratively.
219 :     * This ensures that no infinite replacements will take place by
220 :     * intelligently moving to the next string position each iteration.
221 :     */
222 :     stock replace_all(string[], len, const what[], const with[])
223 :     {
224 :     new pos = 0;
225 :    
226 :     if ((pos = contain(string, what)) == -1)
227 :     {
228 :     return 0;
229 :     }
230 :    
231 :     new total = 0;
232 :     new with_len = strlen(with);
233 :     new diff = strlen(what) - with_len;
234 :     new total_len = strlen(string);
235 :     new temp_pos = 0;
236 :    
237 :     while (replace(string[pos], len - pos, what, with) != 0)
238 :     {
239 :     /* jump to position after replacement */
240 :     pos += with_len;
241 :    
242 :     /* update cached length of string */
243 :     total_len -= diff;
244 :    
245 :     /* will the next call be operating on the last character? */
246 :     if (pos >= total_len)
247 :     {
248 :     break;
249 :     }
250 :    
251 :     /* find the next position from our offset */
252 :     temp_pos = contain(string[pos], what);
253 :    
254 :     /* if it's invalid, we're done */
255 :     if (temp_pos == -1)
256 :     {
257 :     break;
258 :     }
259 :    
260 :     /* otherwise, reposition and update counters */
261 :     pos += temp_pos;
262 :     total++;
263 :     }
264 :    
265 :     return total;
266 :     }

Contact
ViewVC Help
Powered by ViewVC 1.0.4