| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 42f7d7c commit 354026d
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -65,33 +65,42 @@ std::string ToBaseString(const T& value) { | |||
| 65 | 65 | return ToStringHelper::BaseConvert<BASE_BITS>(value); | |
| 66 | 66 | } | |
| 67 | 67 | ||
| 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) return std::string(format); | ||
| 71 | + CHECK_LT(offset + 1, format.size()); | ||
| 72 | + CHECK_EQ(format[offset + 1], | ||
| 73 | + '%'); // Only '%%' allowed when there are no arguments. | ||
| 73 | 74 | ||
| 74 | - return std::string(format, p + 1) + SPrintFImpl(p + 2); | ||
| 75 | + return std::string(format.substr(0, offset + 1)) + | ||
| 76 | + SPrintFImpl(format.substr(offset + 2)); | ||
| 75 | 77 | } | |
| 76 | 78 | ||
| 77 | 79 | template <typename Arg, typename... Args> | |
| 78 | 80 | std::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); | ||
| 81 | + std::string_view format, | ||
| 82 | + Arg&& arg, | ||
| 83 | + Args&&... args) { | ||
| 84 | + auto offset = format.find('%'); | ||
| 85 | + CHECK_NE(offset, std::string_view::npos); // If you hit this, you passed in | ||
| 86 | + // too many arguments. | ||
| 87 | + std::string ret(format.substr(0, offset)); | ||
| 83 | 88 | // Ignore long / size_t modifiers | |
| 84 | - while (strchr("lz", *++p) != nullptr) {} | ||
| 85 | - switch (*p) { | ||
| 89 | + while (++offset < format.size() && | ||
| 90 | + (format[offset] == 'l' || format[offset] == 'z')) { | ||
| 91 | + } | ||
| 92 | + switch (offset == format.size() ? '\0' : format[offset]) { | ||
| 86 | 93 | case '%': { | |
| 87 | - return ret + '%' + SPrintFImpl(p + 1, | ||
| 88 | - std::forward<Arg>(arg), | ||
| 89 | - std::forward<Args>(args)...); | ||
| 94 | + return ret + '%' + | ||
| 95 | + SPrintFImpl(format.substr(offset + 1), | ||
| 96 | + std::forward<Arg>(arg), | ||
| 97 | + std::forward<Args>(args)...); | ||
| 90 | 98 | } | |
| 91 | 99 | default: { | |
| 92 | - return ret + '%' + SPrintFImpl(p, | ||
| 93 | - std::forward<Arg>(arg), | ||
| 94 | - std::forward<Args>(args)...); | ||
| 100 | + return ret + '%' + | ||
| 101 | + SPrintFImpl(format.substr(offset), | ||
| 102 | + std::forward<Arg>(arg), | ||
| 103 | + std::forward<Args>(args)...); | ||
| 95 | 104 | } | |
| 96 | 105 | case 'd': | |
| 97 | 106 | case 'i': | |
@@ -120,17 +129,21 @@ std::string COLD_NOINLINE SPrintFImpl( // NOLINT(runtime/string) | |||
| 120 | 129 | break; | |
| 121 | 130 | } | |
| 122 | 131 | } | |
| 123 | - return ret + SPrintFImpl(p + 1, std::forward<Args>(args)...); | ||
| 132 | + return ret + | ||
| 133 | + SPrintFImpl(format.substr(offset + 1), std::forward<Args>(args)...); | ||
| 124 | 134 | } | |
| 125 | 135 | ||
| 126 | 136 | template <typename... Args> | |
| 127 | 137 | std::string COLD_NOINLINE SPrintF( // NOLINT(runtime/string) | |
| 128 | - const char* format, Args&&... args) { | ||
| 138 | + std::string_view format, | ||
| 139 | + Args&&... args) { | ||
| 129 | 140 | return SPrintFImpl(format, std::forward<Args>(args)...); | |
| 130 | 141 | } | |
| 131 | 142 | ||
| 132 | 143 | template <typename... Args> | |
| 133 | - void COLD_NOINLINE FPrintF(FILE* file, const char* format, Args&&... args) { | ||
| 144 | + void COLD_NOINLINE FPrintF(FILE* file, | ||
| 145 | + std::string_view format, | ||
| 146 | + Args&&... args) { | ||
| 134 | 147 | FWrite(file, SPrintF(format, std::forward<Args>(args)...)); | |
| 135 | 148 | } | |
| 136 | 149 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,9 +33,9 @@ inline std::string ToString(const T& value); | |||
| 33 | 33 | // - Supports %p and %s. %d, %i and %u are aliases for %s. | |
| 34 | 34 | // - Accepts any class that has a ToString() method for stringification. | |
| 35 | 35 | template <typename... Args> | |
| 36 | - inline std::string SPrintF(const char* format, Args&&... args); | ||
| 36 | + inline std::string SPrintF(std::string_view format, Args&&... args); | ||
| 37 | 37 | template <typename... Args> | |
| 38 | - inline void FPrintF(FILE* file, const char* format, Args&&... args); | ||
| 38 | + inline void FPrintF(FILE* file, std::string_view format, Args&&... args); | ||
| 39 | 39 | void NODE_EXTERN_PRIVATE FWrite(FILE* file, const std::string& str); | |
| 40 | 40 | ||
| 41 | 41 | // Listing the AsyncWrap provider types first enables us to cast directly | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -140,7 +140,7 @@ void OOMErrorHandler(const char* location, const v8::OOMDetails& details); | |||
| 140 | 140 | #define V(code, type) \ | |
| 141 | 141 | template <typename... Args> \ | |
| 142 | 142 | inline v8::Local<v8::Object> code( \ | |
| 143 | - v8::Isolate* isolate, const char* format, Args&&... args) { \ | ||
| 143 | + v8::Isolate* isolate, std::string_view format, Args&&... args) { \ | ||
| 144 | 144 | std::string message; \ | |
| 145 | 145 | if (sizeof...(Args) == 0) { \ | |
| 146 | 146 | message = format; \ | |
@@ -165,17 +165,18 @@ void OOMErrorHandler(const char* location, const v8::OOMDetails& details); | |||
| 165 | 165 | } \ | |
| 166 | 166 | template <typename... Args> \ | |
| 167 | 167 | inline void THROW_##code( \ | |
| 168 | - v8::Isolate* isolate, const char* format, Args&&... args) { \ | ||
| 168 | + v8::Isolate* isolate, std::string_view format, Args&&... args) { \ | ||
| 169 | 169 | isolate->ThrowException( \ | |
| 170 | 170 | code(isolate, format, std::forward<Args>(args)...)); \ | |
| 171 | 171 | } \ | |
| 172 | 172 | template <typename... Args> \ | |
| 173 | 173 | inline void THROW_##code( \ | |
| 174 | - Environment* env, const char* format, Args&&... args) { \ | ||
| 174 | + Environment* env, std::string_view format, Args&&... args) { \ | ||
| 175 | 175 | THROW_##code(env->isolate(), format, std::forward<Args>(args)...); \ | |
| 176 | 176 | } \ | |
| 177 | 177 | template <typename... Args> \ | |
| 178 | - inline void THROW_##code(Realm* realm, const char* format, Args&&... args) { \ | ||
| 178 | + inline void THROW_##code( \ | ||
| 179 | + Realm* realm, std::string_view format, Args&&... args) { \ | ||
| 179 | 180 | THROW_##code(realm->isolate(), format, std::forward<Args>(args)...); \ | |
| 180 | 181 | } | |
| 181 | 182 | ERRORS_WITH_CODE(V) | |
@@ -258,10 +259,8 @@ PREDEFINED_ERROR_MESSAGES(V) | |||
| 258 | 259 | // Errors with predefined non-static messages | |
| 259 | 260 | inline void THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(Environment* env, | |
| 260 | 261 | int64_t timeout) { | |
| 261 | - std::ostringstream message; | ||
| 262 | - message << "Script execution timed out after "; | ||
| 263 | - message << timeout << "ms"; | ||
| 264 | - THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(env, message.str().c_str()); | ||
| 262 | + THROW_ERR_SCRIPT_EXECUTION_TIMEOUT( | ||
| 263 | + env, "Script execution timed out after %dms", timeout); | ||
| 265 | 264 | } | |
| 266 | 265 | ||
| 267 | 266 | inline void THROW_ERR_REQUIRE_ASYNC_MODULE( | |
@@ -283,7 +282,7 @@ inline void THROW_ERR_REQUIRE_ASYNC_MODULE( | |||
| 283 | 282 | message += "\n Requiring "; | |
| 284 | 283 | message += utf8.ToStringView(); | |
| 285 | 284 | } | |
| 286 | - THROW_ERR_REQUIRE_ASYNC_MODULE(env, message.c_str()); | ||
| 285 | + THROW_ERR_REQUIRE_ASYNC_MODULE(env, message); | ||
| 287 | 286 | } | |
| 288 | 287 | ||
| 289 | 288 | inline v8::Local<v8::Object> ERR_BUFFER_TOO_LARGE(v8::Isolate* isolate) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments