@@ -65,31 +65,32 @@ std::string ToBaseString(const T& value) {
6565 return ToStringHelper::BaseConvert<BASE_BITS>(value);
6666}
6767
68- inline std::string SPrintFImpl (const char * format) {
69- const char * p = strchr (format, ' %' );
70- if (p == nullptr ) [[unlikely]]
71- return format;
72- CHECK_EQ (p[1 ], ' %' ); // Only '%%' allowed when there are no arguments.
68+ inline std::string SPrintFImpl (std::string_view format) {
69+ auto offset = format.find (' %' );
70+ if (offset == std::string_view::npos)
71+ return std::string (format);
72+ CHECK_LT (offset + 1 , format.size ());
73+ CHECK_EQ (format[offset + 1 ], ' %' ); // Only '%%' allowed when there are no arguments.
7374
74- return std::string (format, p + 1 ) + SPrintFImpl (p + 2 );
75+ return std::string (format. substr ( 0 , offset + 1 )) + SPrintFImpl (format. substr (offset + 2 ) );
7576}
7677
7778template <typename Arg, typename ... Args>
7879std::string COLD_NOINLINE SPrintFImpl ( // NOLINT(runtime/string)
79- const char * format, Arg&& arg, Args&&... args) {
80- const char * p = strchr ( format, ' %' );
81- CHECK_NOT_NULL (p ); // If you hit this, you passed in too many arguments.
82- std::string ret (format, p );
80+ std::string_view format, Arg&& arg, Args&&... args) {
81+ auto offset = format. find ( ' %' );
82+ CHECK_NE (offset, std::string_view::npos ); // If you hit this, you passed in too many arguments.
83+ std::string ret (format. substr ( 0 , offset) );
8384 // Ignore long / size_t modifiers
84- while (strchr ( " lz " , *++p) != nullptr ) {}
85- switch (*p ) {
85+ while (++offset < format. size () && (format[offset] == ' l ' || format[offset] == ' z ' ));
86+ switch (offset == format. size () ? ' \0 ' : format[offset] ) {
8687 case ' %' : {
87- return ret + ' %' + SPrintFImpl (p + 1 ,
88+ return ret + ' %' + SPrintFImpl (format. substr (offset + 1 ) ,
8889 std::forward<Arg>(arg),
8990 std::forward<Args>(args)...);
9091 }
9192 default : {
92- return ret + ' %' + SPrintFImpl (p ,
93+ return ret + ' %' + SPrintFImpl (format. substr (offset) ,
9394 std::forward<Arg>(arg),
9495 std::forward<Args>(args)...);
9596 }
@@ -120,17 +121,17 @@ std::string COLD_NOINLINE SPrintFImpl( // NOLINT(runtime/string)
120121 break ;
121122 }
122123 }
123- return ret + SPrintFImpl (p + 1 , std::forward<Args>(args)...);
124+ return ret + SPrintFImpl (format. substr (offset + 1 ) , std::forward<Args>(args)...);
124125}
125126
126127template <typename ... Args>
127128std::string COLD_NOINLINE SPrintF ( // NOLINT(runtime/string)
128- const char * format, Args&&... args) {
129+ std::string_view format, Args&&... args) {
129130 return SPrintFImpl (format, std::forward<Args>(args)...);
130131}
131132
132133template <typename ... Args>
133- void COLD_NOINLINE FPrintF (FILE* file, const char * format, Args&&... args) {
134+ void COLD_NOINLINE FPrintF (FILE* file, std::string_view format, Args&&... args) {
134135 FWrite (file, SPrintF (format, std::forward<Args>(args)...));
135136}
136137
0 commit comments