OpenImageIO/oiio — pull request #772 by lgritz (2013-12-30 23:46:42+00:00)
Title: string_ref

This is experimental -- I'm mostly seeking feedback. But I rather like it, and if nobody has a compelling objection I'm inclined to commit it.

(N.B. This pull request implicitly includes the patch separately under review in #771; you need not pay much attention that portion here.)

The basic idea here is to roll our own string_ref, a non-owning wrapper for a char* + length, which can be initialized from either char*, a std::string, or a ustring. A similar construct is being considered for C++14 or 17, but we can't count on that being available to all our users for many years (even full C++11 compliance seems like a distant dream).

But anyway, the advantage is that string_ref gives us a uniform way to accept several string types in functions that don't need to own them, solves the std::string-vs-char* dilemma: unlike char*, it's "safe" and gives the callee a definite length to the string, but compared to passing std::string's around, the danger of accidental copies and allocations is eliminated.

Take a look. Feedback appreciated. So far, I've used it extensively in Strutil and a bit in Filesystem, but not yet in any of the core image-related OIIO APIs. But if you guys like it, I'll make a pass later to see where else it might be the appropriate way to pass string-like data around.