From: Ben Pfaff Date: Mon, 26 Nov 2018 00:41:44 +0000 (-0800) Subject: str: New functions ss_starts_with() and ss_find_substring(). X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?p=pspp;a=commitdiff_plain;h=c1a772dd16c825866b0b1b4cfa5c1ba57a9fa48f str: New functions ss_starts_with() and ss_find_substring(). These will have their first users in an upcoming commit. --- diff --git a/src/libpspp/str.c b/src/libpspp/str.c index 5aa7e41594..f31677b929 100644 --- a/src/libpspp/str.c +++ b/src/libpspp/str.c @@ -691,6 +691,14 @@ ss_last (struct substring ss) return ss.length > 0 ? (unsigned char) ss.string[ss.length - 1] : EOF; } +/* Returns true if SS starts with PREFIX, false otherwise. */ +bool +ss_starts_with (struct substring ss, struct substring prefix) +{ + return (ss.length >= prefix.length + && !memcmp (ss.string, prefix.string, prefix.length)); +} + /* Returns true if SS ends with SUFFIX, false otherwise. */ bool ss_ends_with (struct substring ss, struct substring suffix) @@ -733,6 +741,16 @@ ss_find_byte (struct substring ss, char c) return p != NULL ? p - ss.string : SIZE_MAX; } +/* Returns the offset in HAYSTACK of the first instance of NEEDLE, + or SIZE_MAX if NEEDLE does not occur in HAYSTACK. */ +size_t +ss_find_substring (struct substring haystack, struct substring needle) +{ + const char *p = memmem (haystack.string, haystack.length, + needle.string, needle.length); + return p != NULL ? p - haystack.string : SIZE_MAX; +} + /* Compares A and B and returns a strcmp()-type comparison result. */ int diff --git a/src/libpspp/str.h b/src/libpspp/str.h index dd41ede819..66552a3964 100644 --- a/src/libpspp/str.h +++ b/src/libpspp/str.h @@ -122,10 +122,12 @@ char *ss_end (struct substring); int ss_at (struct substring, size_t idx); int ss_first (struct substring); int ss_last (struct substring); +bool ss_starts_with (struct substring, struct substring prefix); bool ss_ends_with (struct substring, struct substring suffix); size_t ss_span (struct substring, struct substring skip_set); size_t ss_cspan (struct substring, struct substring stop_set); size_t ss_find_byte (struct substring, char); +size_t ss_find_substring (struct substring, struct substring); int ss_compare (struct substring, struct substring); int ss_compare_case (struct substring, struct substring); int ss_equals (struct substring, struct substring);