Alfie Richards <[email protected]> writes:
> +/* string_slice inherits from array_slice, specifically to refer to a
> substring
> + of a character array.
> + It includes some string like helpers. */
> +class string_slice : public array_slice<const char>
> +{
> +public:
> + string_slice () : array_slice<const char> () {}
> + string_slice (const char *str) : array_slice (str, strlen (str)) {}
> + explicit string_slice (const char *str, size_t len) :
> + array_slice (str, len) {}
> + explicit string_slice (const char *start, const char *end) :
> + array_slice (start, end - start) {}
Sorry for the formatting nits, but I think the usual style is to put
the : on the next line:
explicit string_slice (const char *str, size_t len)
: array_slice (str, len) {}
explicit string_slice (const char *start, const char *end)
: array_slice (start, end - start) {}
> +
> + friend bool operator== (const string_slice &lhs, const string_slice &rhs)
> + {
> + if (!lhs.is_valid () || !rhs.is_valid ())
> + return false;
> + if (lhs.size () != rhs.size ())
> + return false;
> + return memcmp (lhs.begin (), rhs.begin (), lhs.size ()) == 0;
> + }
> +
> + friend bool operator!= (const string_slice &lhs, const string_slice &rhs)
> + {
> + return !(lhs == rhs);
> + }
> +
> + /* Returns an invalid string_slice. */
> + static string_slice invalid ()
> + {
> + return string_slice (nullptr, ~0U);
> + }
> +
> + /* tokenize is used to split a string by some deliminator into
> + strtok_slice's. Similarly to the posix strtok_r.but without modifying
> the
string_slices
OK with those changes, thanks.
Richard
> + input string, and returning all tokens which may be empty in the case
> + of an empty input string of consecutive deliminators. */
> + static string_slice tokenize (string_slice *str, string_slice delims);
> +
> + /* Removes white space from the front and back of the string_slice. */
> + string_slice strip ();
> +
> + /* Compares two string_slices in lexographical ordering. */
> + static int strcmp (string_slice str1, string_slice str2);
> +};
> +
> #endif // GCC_VEC_H