Browse code

core: ut - functions for reverse search of str value

Daniel-Constantin Mierla authored on 23/09/2021 07:22:21
Showing 2 changed files
... ...
@@ -392,6 +392,49 @@ char *str_casesearch_strz(str *text, char *needlez)
392 392
 	return str_casesearch(text, &needle);
393 393
 }
394 394
 
395
+/**
396
+ * @brief search for last occurence of needle in text (reverse search)
397
+ * @return pointer to start of needle in text or NULL if the needle
398
+ *	is not found
399
+ */
400
+char *str_rsearch(str *text, str *needle)
401
+{
402
+    char *p;
403
+
404
+    if(text==NULL || text->s==NULL || needle==NULL || needle->s==NULL
405
+			|| text->len<needle->len)
406
+        return NULL;
407
+
408
+    for (p = text->s + text->len - needle->len; p >= text->s; p--) {
409
+        if (*p == *needle->s && memcmp(p, needle->s, needle->len)==0) {
410
+            return p;
411
+        }
412
+    }
413
+
414
+    return NULL;
415
+}
416
+
417
+/**
418
+ * @brief case insensitive search for last occurence of needle in text (reverse search)
419
+ * @return pointer to start of needle in text or NULL if the needle
420
+ *	is not found
421
+ */
422
+char *str_rcasesearch(str *text, str *needle)
423
+{
424
+	int i,j;
425
+	for(i=text->len-needle->len;i>=0;i--) {
426
+		for(j=0;j<needle->len;j++) {
427
+			if ( !((text->s[i+j]==needle->s[j]) ||
428
+					( isalpha((int)text->s[i+j])
429
+						&& ((text->s[i+j])^(needle->s[j]))==0x20 )) )
430
+				break;
431
+		}
432
+		if (j==needle->len)
433
+			return text->s+i;
434
+	}
435
+	return NULL;
436
+}
437
+
395 438
 /*
396 439
  * ser_memmem() returns the location of the first occurrence of data
397 440
  * pattern b2 of size len2 in memory block b1 of size len1 or
... ...
@@ -1110,6 +1110,10 @@ char *strz_casesearch_strz(char *textz, char *needlez);
1110 1110
 
1111 1111
 char *str_casesearch_strz(str *text, char *needlez);
1112 1112
 
1113
+char *str_rsearch(str *text, str *needle);
1114
+
1115
+char *str_rcasesearch(str *text, str *needle);
1116
+
1113 1117
 /*
1114 1118
  * ser_memmem() returns the location of the first occurrence of data
1115 1119
  * pattern b2 of size len2 in memory block b1 of size len1 or