[llvm-commits] PATCH: Additional SmallString methods

Talin viridia at gmail.com
Thu Jan 19 23:42:33 PST 2012


This patch adds many convenience methods to class SmallString:

   - void assign(unsigned NumElts, char Elt);
   - void assign(in_iter S, in_iter E);
   - void assign(StringRef RHS);
   - void assign(const SmallVectorImpl<char> &RHS);
   - void append(in_iter S, in_iter E);
   - void append(StringRef RHS);
   - void append(const SmallVectorImpl<char> &RHS);
   - bool equals(StringRef RHS) const;
   - bool equals_lower(StringRef RHS) const;
   - int compare(StringRef RHS) const;
   - int compare_lower(StringRef RHS) const;
   - int compare_numeric(StringRef RHS) const;
   - bool startswith(StringRef Prefix) const;
   - bool endswith(StringRef Suffix) const;
   - size_t find(char C, size_t From = 0) const;
   - size_t find(StringRef Str, size_t From = 0) const;
   - size_t rfind(char C, size_t From = StringRef::npos) const;
   - size_t rfind(StringRef Str) const;
   - size_t find_first_of(char C, size_t From = 0) const;
   - size_t find_first_of(StringRef Chars, size_t From = 0) const;
   - size_t find_first_not_of(char C, size_t From = 0) const;
   - size_t find_first_not_of(StringRef Chars, size_t From = 0) const;
   - size_t find_last_of(char C, size_t From = StringRef::npos) const;
   - size_t find_last_of(StringRef Chars, size_t From = StringRef::npos)
   const;
   - size_t count(char C) const;
   - size_t count(StringRef Str) const;
   - StringRef substr(size_t Start, size_t N = StringRef::npos) const;
   - StringRef slice(size_t Start, size_t End) const;

Most of the methods are taken directly from StringRef (find_x, compare_x,
substr, and so on). My motivation for adding these methods is that often I
find myself working with a SmallString instead of a StringRef, and it would
be nice to call substr() for example directly without having to convert to
a StringRef. Internally, of course, the methods simply convert the
SmallString to a StringRef (which is extremely cheap), and then call the
corresponding StringRef method. Doing it this way minimizes template
expansion, since StringRef is a non-template class.

The 'assign' and 'append' methods are taken from standard STL, with
additional overloads that are convenient for strings. For example, assign()
now allows assigning to a SmallString from an iterator pair, which was not
supported before.

(Note: I thought about adding support for assign/append/construct from an
ArrayRef<char>, but that would add an additional include file dependency.)

This patch also includes many additions to the unit tests for SmallString,
as well as some minor typographical cleanups in the comments in
SmallString.h.

-- 
-- Talin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20120119/452ea450/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smallstring.patch
Type: application/octet-stream
Size: 14244 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20120119/452ea450/attachment.obj>


More information about the llvm-commits mailing list