X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flibpspp%2Fstr.c;h=ba4a26f1426ac51552dd31885d5cdfff5fea321d;hb=530906aaa19f6c209ca008c8187f7f750a0b1283;hp=e34c150df1c32506522155482c3285433c021c99;hpb=086322fd8c85a303ba6f552950d6f057f2867add;p=pspp-builds.git diff --git a/src/libpspp/str.c b/src/libpspp/str.c index e34c150d..ba4a26f1 100644 --- a/src/libpspp/str.c +++ b/src/libpspp/str.c @@ -427,6 +427,20 @@ ss_chomp_byte (struct substring *ss, char c) return false; } +/* If SS ends with SUFFIX, removes it and returns true. + Otherwise, returns false without changing the string. */ +bool +ss_chomp (struct substring *ss, struct substring suffix) +{ + if (ss_ends_with (*ss, suffix)) + { + ss->length -= suffix.length; + return true; + } + else + return false; +} + /* Divides SS into tokens separated by any of the DELIMITERS. Each call replaces TOKEN by the next token in SS, or by an empty string if no tokens remain. Returns true if a token was @@ -655,6 +669,15 @@ ss_last (struct substring ss) return ss.length > 0 ? (unsigned char) ss.string[ss.length - 1] : EOF; } +/* Returns true if SS ends with SUFFIX, false otherwise. */ +bool +ss_ends_with (struct substring ss, struct substring suffix) +{ + return (ss.length >= suffix.length + && !memcmp (&ss.string[ss.length - suffix.length], suffix.string, + suffix.length)); +} + /* Returns the number of contiguous bytes at the beginning of SS that are in SKIP_SET. */ size_t @@ -1041,6 +1064,14 @@ ds_chomp_byte (struct string *st, char c) return ss_chomp_byte (&st->ss, c); } +/* If ST ends with SUFFIX, removes it and returns true. + Otherwise, returns false without modifying ST. */ +bool +ds_chomp (struct string *st, struct substring suffix) +{ + return ss_chomp (&st->ss, suffix); +} + /* Divides ST into tokens separated by any of the DELIMITERS. Each call replaces TOKEN by the next token in ST, or by an empty string if no tokens remain. Returns true if a token was @@ -1180,6 +1211,13 @@ ds_last (const struct string *st) return ss_last (ds_ss (st)); } +/* Returns true if ST ends with SUFFIX, false otherwise. */ +bool +ds_ends_with (const struct string *st, struct substring suffix) +{ + return ss_ends_with (st->ss, suffix); +} + /* Returns the number of consecutive bytes at the beginning of ST that are in SKIP_SET. */ size_t