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