Spicy
fmt.h
1 // Copyright (c) 2020-now by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <string>
6 
7 #include <hilti/rt/3rdparty/tinyformat/tinyformat.h>
8 
9 namespace hilti::rt {
10 
16 template<typename... Args>
17 std::string fmt(const char* fmt, const Args&... args) {
18  return tfm::format(fmt, args...);
19 }
20 
26 template<typename... Args>
27 std::string fmt(std::string_view s, const Args&... args) {
28  // In principal we do not know whether the passed `string_view` is
29  // null-terminated, so `s.data()` could end up accessing out of bounds
30  // data. In generated code `s` is always null-terminated though.
31  //
32  // NOTE: If we ever wanted to make this safe for views not null-terminated,
33  // a fix would be to expand `s` into a true, null-terminated `const char*`,
34  // e.g.,
35  //
36  // char buf[1024];
37  // snprintf(buf, sizeof(buf), "%.*s", static_cast<int>(s.length()), s.data());
38  // return fmt(buf, args...);
39  return fmt(s.data(), args...); // NOLINT(bugprone-suspicious-stringview-data-usage)
40 }
41 } // namespace hilti::rt
Definition: any.h:7
std::string fmt(const char *fmt, const Args &... args)
Definition: fmt.h:17