├── test.cpp ├── LICENSE.md ├── Readme.md └── mini_format.h /test.cpp: -------------------------------------------------------------------------------- 1 | #include "mini_format.h" 2 | 3 | struct T { 4 | int n; 5 | }; 6 | 7 | inline std::ostream &operator<<(std::ostream &os, const T &t) { 8 | return os << mini_format("T({})", t.n); 9 | } 10 | 11 | template 12 | inline std::ostream &operator<<(std::ostream &os, const std::vector &ts) { 13 | os << "vector("; 14 | for (size_t i = 0; i < ts.size(); ++i) os << (i == 0 ? "" : ",") << ts[i]; 15 | return os << ")"; 16 | } 17 | 18 | 19 | int main() { 20 | T t{1024}; 21 | 22 | // format string 23 | std::string str = mini_format("xyz is {}", 999); 24 | 25 | // print string 26 | mini_print("tom = {}, jerry = {}\n", "abcd", 1234); 27 | mini_print("float = {}, string = {}, t = {}\n", 3.14159, str, t); 28 | 29 | // log output 30 | MINI_LOG("tom = {}, jerry = {}\n", "abcd", 1234); 31 | MINI_LOG("double = {}, string = {}\n", 3.14159, str); 32 | 33 | // print vector 34 | std::vector ts; 35 | ts.push_back(1.1); 36 | ts.push_back(2.2); 37 | ts.push_back(3.3); 38 | 39 | mini_print("{}", ts); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2022 xhawk18 -at- gmail.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # What's this 2 | 3 | c++20 std::format/print implemented by 50 lines. 4 | 5 | # Usage 6 | 7 | ## print formated string 8 | 9 | ``` 10 | void mini_print(const std::string &fmt, const Args &...args); 11 | ``` 12 | 13 | ## format string 14 | 15 | ``` 16 | std::string mini_format(const std::string &fmt, const Args &...args); 17 | ``` 18 | 19 | ## log out 20 | 21 | ``` 22 | /* Pseudocode, MINI_LOG is a macro */ 23 | void MINI_LOG(const std::string &fmt, const Args &...args); 24 | ``` 25 | 26 | ## format customer types 27 | 28 | override operator<<() on the customer type for ostream is OK. 29 | ``` 30 | inline std::ostream &operator<<(std::ostream &os, const T &t) { 31 | return os << "some_string_here"; 32 | } 33 | ``` 34 | 35 | # The example 36 | ``` 37 | #include "mini_format.h" 38 | 39 | struct T { 40 | int n; 41 | }; 42 | 43 | inline std::ostream &operator<<(std::ostream &os, const T &t) { 44 | return os << mini_format("T({})", t.n); 45 | } 46 | 47 | 48 | int main() { 49 | T t{1024}; 50 | 51 | std::string str = mini_format("xyz is {}", 999); 52 | 53 | mini_print("tom = {}, jerry = {}\n", "abcd", 1234); 54 | mini_print("float = {}, string = {}, t = {}\n", 3.14159, str, t); 55 | 56 | MINI_LOG("tom = {}, jerry = {}\n", "abcd", 1234); 57 | MINI_LOG("double = {}, string = {}\n", 3.14159, str); 58 | return 0; 59 | } 60 | 61 | 62 | /* The code outputs : 63 | tom = abcd, jerry = 1234 64 | float = 3.14159, string = xyz is 999, t = T(1024) 65 | [2022-01-25_21:44:46] [test.cpp:20] tom = abcd, jerry = 1234 66 | [2022-01-25_21:44:46] [test.cpp:21] double = 3.14159, string = xyz is 999 67 | */ 68 | ``` 69 | -------------------------------------------------------------------------------- /mini_format.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using arg_builder_t = std::function; 12 | inline std::string mini_format_impl(const std::string &fmt, const std::vector &arg_builders) { 13 | std::ostringstream ss; 14 | size_t start = 0; 15 | for(size_t i = 0; i < arg_builders.size(); ++i) { 16 | std::size_t pos = fmt.find("{}", start); 17 | if (pos == std::string::npos) break; 18 | ss << fmt.substr(start, pos - start); 19 | arg_builders[i](ss); 20 | start = pos + 2 /* length of "{}" */; 21 | } 22 | ss << fmt.substr(start); 23 | return ss.str(); 24 | } 25 | 26 | template 27 | inline arg_builder_t mini_format_arg(const Arg &arg) { 28 | return [&arg](std::ostream &ss) { ss << arg; }; 29 | } 30 | 31 | template 32 | inline std::string mini_format(const std::string &fmt, const Args &...args) { 33 | return mini_format_impl(fmt, {mini_format_arg(args)...}); 34 | } 35 | 36 | template 37 | inline void mini_print(const std::string &fmt, const Args &...args) { 38 | printf("%s", mini_format(fmt, args...).c_str()); 39 | } 40 | 41 | inline void mini_log_info(const std::string &file, int line) { 42 | auto pos = file.find_last_of("\\/"); 43 | auto now = time(0); 44 | mini_print("[{}] [{}] [{}:{}] ", 45 | std::put_time(localtime(&now), "%Y-%m-%d_%H:%M:%S"), 46 | std::this_thread::get_id(), 47 | (pos == std::string::npos ? file : file.substr(pos + 1)), line); 48 | } 49 | 50 | #define MINI_LOG(...) do { mini_log_info(__FILE__, __LINE__); mini_print(__VA_ARGS__); } while(0) 51 | --------------------------------------------------------------------------------