10 #include <platon/str/strplus.h>
11 #include <platon/str/strctype.h>
14 PLATON_FUNC(strestr)(s1, s2)
18 return strstr(s1,s2) == NULL ? NULL : strendstr(s1,s2);
21 #if ! defined(SELF) && ! defined(SELFTEST) && ! defined(SELF_STRPLUS)
24 * Functions strtolower() and strtoupper(), used by function below,
25 * are defined in strctype.c so when we are building self-testing
26 * binary, skip this section.
30 PLATON_FUNC(stristr)(s1, s2)
35 register char *ret = NULL;
40 if (a_s1 != NULL && a_s2 != NULL) {
41 ret = strstr(PLATON_FUNC(strtolower)(a_s1),
42 PLATON_FUNC(strtolower)(a_s2));
44 ret = (char *) s1 + (ret - a_s1);
59 PLATON_FUNC(str_white_str)(str, substr, size)
66 * This is fastfix code substitution for str_white_str() function,
67 * because new wersion from 'Crasher' was not fully tested yet.
70 *size = strlen(substr);
71 return strstr(str, substr);
74 register int slen, plen, ssize;
75 register char *pptr, *sptr, *start;
78 plen = strlen(substr);
80 for (start = str, pptr = substr; slen >= plen; start++, slen--) {
82 /* Find start of pattern in string. */
83 while (*start != *substr) {
85 if ((isspace(*start)) && (isspace(*substr)))
90 /* If pattern longer than string. */
101 #ifdef DEBUG_STRPLUS /* if str_white_str() works properly, delete this */
102 printf("comparing %d [%s] with %d [%s]\n",
103 *sptr, sptr, *pptr, pptr);
106 if ((isspace(*sptr)) && (isspace(*pptr))) {
107 while (isspace(*sptr)) {
110 if (isspace(*pptr)) {
118 else if (*sptr == *pptr) {
119 while (*sptr == *pptr && *sptr != '\0' && ! isspace(*sptr)) {
129 /* If end of pattern then pattern was found. */
144 PLATON_FUNC(strcnt)(str, c)
152 if (*str++ == (char) c)
159 PLATON_FUNC(strcnt_str)(str, substr)
166 for (count = 0; ; count++) {
167 if ((s = strstr(str, substr)) == NULL)
170 str += (s - str) + 1;
177 PLATON_FUNC(strcnt_sepstr)(str, substr)
182 register int count, substr_size;
184 substr_size = strlen(substr);
186 for (count = 0; ; count++) {
187 if ((s = strstr(str, substr)) == NULL)
190 str += (s - str) + substr_size;
197 PLATON_FUNC(strdel)(s)
201 return (char *) memmove(s, s + 1, strlen(s));
205 for (i = 0; s[i] != '\0'; i++)
213 PLATON_FUNC(strrmlf)(s)
218 while ((p_lf = strchr(s, '\n')) != NULL)
219 PLATON_FUNC(strdel)(p_lf);
225 PLATON_FUNC(strrmcr)(s)
230 while ((p_cr = strchr(s, '\r')) != NULL)
231 PLATON_FUNC(strdel)(p_cr);
237 PLATON_FUNC(str_left_trim)(s)
242 for (pos = s; *pos != '\0' && isspace(*pos); pos++) ;
245 memmove((void *) s, (void *) pos, strlen(pos) + 1);
251 PLATON_FUNC(str_right_trim)(s)
256 for (pos = s + (strlen(s) - 1); pos >= s && isspace(*pos); pos--) ;
264 PLATON_FUNC(str_trim_whitechars)(s)
267 register char *pos, *start;
269 for (pos = s, start = NULL; ; pos++) {
276 memmove(start + 1, pos, strlen(pos) + 1);
292 PLATON_FUNC(strins)(str, ins)
296 register int ins_len = strlen(ins);
298 memmove(str + ins_len, str, strlen(str) + 1);
299 strncpy(str, ins, ins_len);
305 PLATON_FUNC(strrev)(str)
308 register int i, c, len = strlen(str);
310 /* Code borrowed from PHP: Hypertext Preprocessor, http://www.php.net/ */
311 for (i = 0; i < len - 1 - i; i++) {
313 str[i] = str[len - 1 - i];
314 str[len - 1 - i] = c;
321 PLATON_FUNC(strrcmp)(s1, s2)
325 register char *x1, *x2;
327 x1 = strchr(s1,'\0');
328 x2 = strchr(s2,'\0');
330 while (x1 > s1 && x2 > s2) {
334 return strcmp(x1,x2);
337 return strlen(s1) - strlen(s2);
340 #if defined(SELF) || defined(SELFTEST) || defined(SELF_STRPLUS) || defined(SELF_STRPLUS2)
344 #define TESTSTR1 "___AAA_BBBB__C_DaDaD____"
346 #define TESTSEP2 "BB"
348 #define TESTSTR2 " \t AAA\nBBBB__C D\taDa D \t \t"
351 strins_selftest(void)
355 #define SEARCH_STR "56"
356 #define INSERT_STR "<-now-goes-56->"
358 strcpy(long_str, "1234567890");
359 printf(" long_str before: %s\n", long_str);
361 printf(" Now we're searching '%s' and want to insert '%s' before it.\n",
362 SEARCH_STR, INSERT_STR);
364 strins(strstr(long_str, SEARCH_STR), INSERT_STR);
365 printf(" long_str after: %s\n", long_str);
377 puts("Entering str_white_str() selftest:");
382 str2 = PLATON_FUNC(str_white_str)(argv[1], argv[2], &size);
383 /* str2 = str_white_str("telnet \t atlantis.sk 5678", "t a", &size);
385 printf(" ptr = [%s], size = %d\n", str2, size);
390 printf(" Usage: %s <string> <substring>\n\n", argv[0]);
392 str2 = strdup(TESTSTR2);
394 printf("strcnt(\"%s\", '%c') = %d\n",
396 PLATON_FUNC(strcnt)(TESTSTR1, TESTSEP1));
398 printf("strcnt_str(\"%s\", \"%s\") = %d\n",
400 PLATON_FUNC(strcnt_str)(TESTSTR1, TESTSEP2));
402 printf("strcnt_sepstr(\"%s\", \"%s\") = %d\n",
404 PLATON_FUNC(strcnt_sepstr)(TESTSTR1, TESTSEP2));
406 printf("str_trim_whitechars(\"%s\") = \"%s\"\n",
408 PLATON_FUNC(str_trim_whitechars)(str2));
410 printf("strdel(\"%s\") = ", str2);
411 printf("\"%s\"\n", PLATON_FUNC(strdel)(str2));
413 printf("strrev(\"%s\") = ", str2);
414 printf("\"%s\"\n", PLATON_FUNC(strrev)(str2));
415 strrev(str2); /* Reversing back, just for sure */
418 #if ! defined(SELF) && ! defined(SELFTEST) && ! defined(SELF_STRPLUS)
419 { /* stristr() selftest */
420 char *ptr = "Reply-To";
421 char *search = "reply-to";
422 char *output = stristr(ptr, search);
423 printf("stristr(\"%s\", \"%s\") = \"%s\"\n", ptr, search, output);
424 printf(" (\"%s\" == \"%s\") == %d\n", ptr, output, ptr == output);
429 puts("\nEntering strins_selftest():");
435 #endif /* #if defined(SELF) || defined(SELFTEST) || defined(SELF_STRPLUS) || defined(SELF_STRPLUS2) */