Skip to content

std::format/print implemented in 50 lines ( have fun! )

License

Notifications You must be signed in to change notification settings

xhawk18/50print

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

What's this

c++20 std::format/print implemented by 50 lines.

Usage

print formated string

void mini_print(const std::string &fmt, const Args &...args);

format string

std::string mini_format(const std::string &fmt, const Args &...args);

log out

/* Pseudocode, MINI_LOG is a macro */
void MINI_LOG(const std::string &fmt, const Args &...args);

format customer types

override operator<<() on the customer type for ostream is OK.

inline std::ostream &operator<<(std::ostream &os, const T &t) {
    return os << "some_string_here";
}

The example

#include "mini_format.h"

struct T {
    int n;
};

inline std::ostream &operator<<(std::ostream &os, const T &t) {
    return os << mini_format("T({})", t.n);
}


int main() {
    T t{1024};
    
    std::string str = mini_format("xyz is {}", 999);

    mini_print("tom = {}, jerry = {}\n", "abcd", 1234);
    mini_print("float = {}, string = {}, t = {}\n", 3.14159, str, t);

    MINI_LOG("tom = {}, jerry = {}\n", "abcd", 1234);
    MINI_LOG("double = {}, string = {}\n", 3.14159, str);
    return 0;
}


/* The code outputs :
tom = abcd, jerry = 1234
float = 3.14159, string = xyz is 999, t = T(1024)
[2022-01-25_21:44:46] [test.cpp:20] tom = abcd, jerry = 1234
[2022-01-25_21:44:46] [test.cpp:21] double = 3.14159, string = xyz is 999
*/

About

std::format/print implemented in 50 lines ( have fun! )

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages