Malcolm Parsons via llvm-dev
2016-Nov-29 17:52 UTC
[llvm-dev] RFC: Constructing StringRefs at compile time
On 29 November 2016 at 17:38, Zachary Turner <zturner at google.com> wrote:> I see, but I looked over your proposed implementation from earlier in the > thread, and if I'm not mistaken I see this:That's a different suggestion.> That said, what did you think about my other proposal of the complicated UDL > with macro? > > #define LIT(x) x_string_ref_literal > constexpr StringRef Strings[] = {LIT("a"), LIT("b"), LIT("c")};Why bother with the UDL? #define LIT(x) StringRef((x), sizeof(x)-1) -- Malcolm Parsons
Mehdi Amini via llvm-dev
2016-Nov-29 17:54 UTC
[llvm-dev] RFC: Constructing StringRefs at compile time
> On Nov 29, 2016, at 9:52 AM, Malcolm Parsons <malcolm.parsons at gmail.com> wrote: > > On 29 November 2016 at 17:38, Zachary Turner <zturner at google.com> wrote: >> I see, but I looked over your proposed implementation from earlier in the >> thread, and if I'm not mistaken I see this: > > That's a different suggestion. > >> That said, what did you think about my other proposal of the complicated UDL >> with macro? >> >> #define LIT(x) x_string_ref_literal >> constexpr StringRef Strings[] = {LIT("a"), LIT("b"), LIT("c")}; > > Why bother with the UDL? > #define LIT(x) StringRef((x), sizeof(x)-1) >There is subtlety though, it changes the result for: LIT(“hello\0world”). — Mehdi
Zachary Turner via llvm-dev
2016-Nov-29 17:55 UTC
[llvm-dev] RFC: Constructing StringRefs at compile time
char buffer[100]; And it also allows LIT(buffer) to compile, whereas the UDL doesn't. On Tue, Nov 29, 2016 at 9:54 AM Mehdi Amini <mehdi.amini at apple.com> wrote:> > > On Nov 29, 2016, at 9:52 AM, Malcolm Parsons <malcolm.parsons at gmail.com> > wrote: > > > > On 29 November 2016 at 17:38, Zachary Turner <zturner at google.com> wrote: > >> I see, but I looked over your proposed implementation from earlier in > the > >> thread, and if I'm not mistaken I see this: > > > > That's a different suggestion. > > > >> That said, what did you think about my other proposal of the > complicated UDL > >> with macro? > >> > >> #define LIT(x) x_string_ref_literal > >> constexpr StringRef Strings[] = {LIT("a"), LIT("b"), LIT("c")}; > > > > Why bother with the UDL? > > #define LIT(x) StringRef((x), sizeof(x)-1) > > > > There is subtlety though, it changes the result for: LIT(“hello\0world”). > > — > Mehdi > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161129/5a923756/attachment.html>
Malcolm Parsons via llvm-dev
2016-Dec-02 11:35 UTC
[llvm-dev] RFC: Constructing StringRefs at compile time
On 29 November 2016 at 17:52, Malcolm Parsons <malcolm.parsons at gmail.com> wrote:> On 29 November 2016 at 17:38, Zachary Turner <zturner at google.com> wrote: >> I see, but I looked over your proposed implementation from earlier in the >> thread, and if I'm not mistaken I see this: > > That's a different suggestion.llvm_strlen could be something like this: #if __cpp_lib_constexpr_char_traits constexpr size_t llvm_strlen(const char *S) { return std::char_traits<char>::length(S); } #elif __has_builtin(__builtin_strlen) || defined(__GNUC__) constexpr size_t llvm_strlen(const char *S) { return __builtin_strlen(S); } #elif __cpp_constexpr >= 201304 constexpr size_t llvm_strlen(const char *S) { const char *const A = S; while (*S != '\0') { ++S; } return S - A; } #else constexpr size_t llvm_strlen(const char *S, size_t L = 0) { return *S ? llvm_strlen(S + 1, L + 1) : L; } #endif -- Malcolm Parsons
James Y Knight via llvm-dev
2016-Dec-02 17:12 UTC
[llvm-dev] RFC: Constructing StringRefs at compile time
On Fri, Dec 2, 2016 at 6:35 AM, Malcolm Parsons via llvm-dev < llvm-dev at lists.llvm.org> wrote:> On 29 November 2016 at 17:52, Malcolm Parsons <malcolm.parsons at gmail.com> > wrote: > > On 29 November 2016 at 17:38, Zachary Turner <zturner at google.com> wrote: > >> I see, but I looked over your proposed implementation from earlier in > the > >> thread, and if I'm not mistaken I see this: > > > > That's a different suggestion. > > llvm_strlen could be something like this: > > #if __cpp_lib_constexpr_char_traits > constexpr size_t llvm_strlen(const char *S) { > return std::char_traits<char>::length(S); > } > #elif __has_builtin(__builtin_strlen) || defined(__GNUC__) > constexpr size_t llvm_strlen(const char *S) { return __builtin_strlen(S); } > #elif __cpp_constexpr >= 201304 > constexpr size_t llvm_strlen(const char *S) { > const char *const A = S; > while (*S != '\0') { > ++S; > } > return S - A; > } > #else > constexpr size_t llvm_strlen(const char *S, size_t L = 0) { > return *S ? llvm_strlen(S + 1, L + 1) : L; > } > #endif >...and we've gone in a loop in this conversation, haven't we? Isn't this again where you end up with really slow code for compilers that end up in the last #else, such as MSVC? +1 from me for the StringLiteral proposal from a few messages back. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161202/248eb92b/attachment.html>