commit b6b10a07378ceebd496a60063ded16001d2aa363
parent 99b0c73862bbf4b6e31c08223283ed76b3ecf464
Author: Allis IO <0xa111510@gmail.com>
Date: Thu, 23 Apr 2020 14:49:15 -0400
Use strstr() to search for substrings
Diffstat:
1 file changed, 1 insertion(+), 27 deletions(-)
diff --git a/paleofetch.c b/paleofetch.c
@@ -62,39 +62,13 @@ void truncate_spaces(char *str) {
}
/*
- * Returns index of substring in str, if it is there
- * Assumes that strlen(substring) >= len
- */
-int contains_substring(const char *str, const char*substring, size_t len) {
- if(len == 0) return -1;
-
- /* search for substring */
- int offset = 0;
- for(;;) {
- int match = 1;
- for(size_t i = 0; i < len; i++) {
- if(*(str+offset+i) == '\0') return -1;
- else if(*(str+offset+i) != *(substring+i)) {
- match = 0;
- break;
- }
- }
-
- if(match) break;
- offset++;
- }
-
- return offset;
-}
-
-/*
* Removes the first len characters of substring from str
* Assumes that strlen(substring) >= len
* Returns index where substring was found, or -1 if substring isn't found
*/
void remove_substring(char *str, const char* substring, size_t len) {
/* shift over the rest of the string to remove substring */
- int offset = contains_substring(str, substring, len);
+ int offset = strstr(str, substring) - str;
if(offset < 0) return;
int i = 0;