ss->string = xrealloc (ss->string, size);
}
-/* Makes a pool_alloc_unaligned()'d copy of the contents of OLD
- in POOL, and stores it in NEW. */
+/* Makes a pool_alloc_unaligned()'d, null-terminated copy of the contents of
+ OLD in POOL, and stores it in NEW. */
void
ss_alloc_substring_pool (struct substring *new, struct substring old,
struct pool *pool)
{
- new->string = pool_alloc_unaligned (pool, old.length);
+ new->string = pool_alloc_unaligned (pool, old.length + 1);
new->length = old.length;
memcpy (new->string, old.string, old.length);
+ new->string[old.length] = '\0';
}
/* Allocates room for a CNT-byte string in NEW in POOL. */
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
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
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
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