[Support] Add StringRef::withNullAsEmpty()
authorZachary Turner <zturner@google.com>
Mon, 19 Sep 2016 15:34:51 +0000 (15:34 +0000)
committerZachary Turner <zturner@google.com>
Mon, 19 Sep 2016 15:34:51 +0000 (15:34 +0000)
When porting large bodies of code from using const char*
to StringRef, it is helpful to be able to treat nullptr
as an empty string, since that it is often what it is used
to indicate in C-style code.

Differential Revision: https://reviews.llvm.org/D24697

llvm-svn: 281906

llvm/include/llvm/ADT/StringRef.h

index 37c214c..5d0aafc 100644 (file)
@@ -88,6 +88,10 @@ namespace llvm {
     /*implicit*/ StringRef(const std::string &Str)
       : Data(Str.data()), Length(Str.length()) {}
 
+    static StringRef withNullAsEmpty(const char *data) {
+      return StringRef(data ? data : "");
+    }
+
     /// @}
     /// @name Iterators
     /// @{