pspp-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 07/18] str: New functions for checking for and removing string su


From: Ben Pfaff
Subject: [PATCH 07/18] str: New functions for checking for and removing string suffixes.
Date: Sat, 19 Mar 2011 17:09:53 -0700

---
 src/libpspp/str.c |   38 ++++++++++++++++++++++++++++++++++++++
 src/libpspp/str.h |    4 ++++
 2 files changed, 42 insertions(+), 0 deletions(-)

diff --git a/src/libpspp/str.c b/src/libpspp/str.c
index e34c150..ba4a26f 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
diff --git a/src/libpspp/str.h b/src/libpspp/str.h
index d2b39ef..cf4888e 100644
--- a/src/libpspp/str.h
+++ b/src/libpspp/str.h
@@ -99,6 +99,7 @@ size_t ss_rtrim (struct substring *, struct substring 
trim_set);
 size_t ss_ltrim (struct substring *, struct substring trim_set);
 void ss_trim (struct substring *, struct substring trim_set);
 bool ss_chomp_byte (struct substring *, char);
+bool ss_chomp (struct substring *, struct substring);
 bool ss_separate (struct substring src, struct substring delimiters,
                   size_t *save_idx, struct substring *token);
 bool ss_tokenize (struct substring src, struct substring delimiters,
@@ -120,6 +121,7 @@ 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_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);
@@ -178,6 +180,7 @@ size_t ds_rtrim (struct string *, struct substring 
trim_set);
 size_t ds_ltrim (struct string *, struct substring trim_set);
 size_t ds_trim (struct string *, struct substring trim_set);
 bool ds_chomp_byte (struct string *, char);
+bool ds_chomp (struct string *, struct substring);
 bool ds_separate (const struct string *src, struct substring delimiters,
                   size_t *save_idx, struct substring *token);
 bool ds_tokenize (const struct string *src, struct substring delimiters,
@@ -200,6 +203,7 @@ char *ds_end (const struct string *);
 int ds_at (const struct string *, size_t idx);
 int ds_first (const struct string *);
 int ds_last (const struct string *);
+bool ds_ends_with (const struct string *, struct substring suffix);
 size_t ds_span (const struct string *, struct substring skip_set);
 size_t ds_cspan (const struct string *, struct substring stop_set);
 size_t ds_find_byte (const struct string *, char);
-- 
1.7.2.3




reply via email to

[Prev in Thread] Current Thread [Next in Thread]