[llvm-commits] PATCH: Adding an array constructor to StringRef

David Blaikie dblaikie at gmail.com
Mon Jun 18 08:47:32 PDT 2012


One possible problem with this is that if you construct a StringRef
from a string literal, you'll end up including the '\0' inside it.

(also, when I tried this myself a few months ago, there's at least one
case where a StringRef is constructed from a large fixed-size buffer
on the stack - when using this ctor, it obviously gets the size quite
wrong)

That being said, I like the idea, I'm just not sure the right way to
implement it - when I was experimenting with this I implemented it
with a '\0' check for the last character (truncating the size by one
when present, and leaving it at the full size when it wasn't present)
- and added an assert for strlen(Data) == Length to catch the buffer
case (which means the ctor didn't provide performance improvements in
debug builds but only in release builds).

- David

On Mon, Jun 18, 2012 at 8:36 AM, Marshall Clow <mclow.lists at gmail.com> wrote:
> Modeled on the one in ArrayRef
>
>    /// Construct an string ref from a C array.
>    template <size_t N>
>    /*implicit*/ StringRef(const char (&Arr)[N])
>      : Data(Arr), Length(N) {}
>
> -- Marshall
>
> Marshall Clow     Idio Software   <mailto:mclow.lists at gmail.com>
>
> A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait).
>        -- Yu Suzuki
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>




More information about the llvm-commits mailing list