From: Zachary Turner Date: Mon, 19 Sep 2016 15:34:51 +0000 (+0000) Subject: [Support] Add StringRef::withNullAsEmpty() X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=781fb6ecc8e89b36ed03ad41087e9ce34d854507;p=llvm [Support] Add StringRef::withNullAsEmpty() 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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@281906 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/ADT/StringRef.h b/include/llvm/ADT/StringRef.h index 37c214cdff7..5d0aafc4683 100644 --- a/include/llvm/ADT/StringRef.h +++ b/include/llvm/ADT/StringRef.h @@ -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 /// @{