Git Product home page Git Product logo

50print's Introduction

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
*/

50print's People

Contributors

xhawk18 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.