Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/wabt/string-format.h
1 /* 2 * Copyright 2021 WebAssembly Community Group participants 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef WABT_STRING_FORMAT_H_ 18 #define WABT_STRING_FORMAT_H_ 19 20 #include <cstdarg> 21 #include <string> 22 #include <vector> 23 24 #include "wabt/config.h" 25 26 #define PRIstringview "%.*s" 27 #define WABT_PRINTF_STRING_VIEW_ARG(x) \ 28 static_cast<int>((x).length()), (x).data() 29 30 #define PRItypecode "%s%#x" 31 #define WABT_PRINTF_TYPE_CODE(x) \ 32 (static_cast<int32_t>(x) < 0 ? "-" : ""), std::abs(static_cast<int32_t>(x)) 33 34 #define WABT_DEFAULT_SNPRINTF_ALLOCA_BUFSIZE 128 35 #define WABT_SNPRINTF_ALLOCA(buffer, len, format) \ 36 va_list args; \ 37 va_list args_copy; \ 38 va_start(args, format); \ 39 va_copy(args_copy, args); \ 40 char fixed_buf[WABT_DEFAULT_SNPRINTF_ALLOCA_BUFSIZE]; \ 41 char* buffer = fixed_buf; \ 42 size_t len = wabt_vsnprintf(fixed_buf, sizeof(fixed_buf), format, args); \ 43 va_end(args); \ 44 if (len + 1 > sizeof(fixed_buf)) { \ 45 buffer = static_cast<char*>(alloca(len + 1)); \ 46 len = wabt_vsnprintf(buffer, len + 1, format, args_copy); \ 47 } \ 48 va_end(args_copy) 49 50 namespace wabt { 51 52 inline std::string WABT_PRINTF_FORMAT(1, 2) 53 StringPrintf(const char* format, ...) { 54 va_list args; 55 va_list args_copy; 56 va_start(args, format); 57 va_copy(args_copy, args); 58 size_t len = wabt_vsnprintf(nullptr, 0, format, args) + 1; // For \0. 59 std::vector<char> buffer(len); 60 va_end(args); 61 wabt_vsnprintf(buffer.data(), len, format, args_copy); 62 va_end(args_copy); 63 return std::string(buffer.data(), len - 1); 64 } 65 66 } // namespace wabt 67 68 #endif // WABT_STRING_FORMAT_H_