Coder Social home page Coder Social logo

print-stmt's Introduction

An implementation of a print statement in C++.

#include "print.h"

print expression[, expression]*
print "format" % expression[, expression]*
warn expression[, expression]*
warn "format" % expression[, expression]*

/** examples **/
print "Hello", "world", time(0);       // Hello world 1360826578
print "name=%q age=%s" % "Alex", 26;   // name="Alex" age=26

std::vector<std::string> things = {"apples", "oranges", "elephants"};
print things;                          // [apples, oranges, elephants]

The first form converts each expression into a string and prints it to standard output, with each expression separated by a space and a terminating newline at the end. warn is similar except it prints to standard error.

The second form uses a format string similar to printf().

  • %s The argument is printed directly.
  • %q The argument is quoted.

Expressions are converted to strings according to their type, as follows:

  • char* are output as NUL-terminated strings like %s.
  • int, short, long, signed char, unsigned char are printed in decimal notation like with %d.
  • char prints the charcter like %c.
  • void * or any other kind of pointers prints ths pointer in hex like %p.
  • double, float prints the number in decimal notation like %g.
  • bool prints 'true' or 'false'
  • A type that defines a member function called c_str() is converted to a string by calling this function.
  • A type than can be streamed to an std::ostream like std::cout << thing but does not have a c_str() member function is converted to a string using <<.
  • A type that has a begin() member function that returns in iterator but does not have a c_str() member function is printed like a list, e.g. ["one", "two"].
  • std:map, std::unordered_map, std::multimap, std::unordered_multipmap, QHash, QMap, QMultiHash, QMultiMap are special-cased and printed like dictionaries, e.g. {"foo": "bar"}.
  • QString is printed as a string.
  • std::pair is rendered as ("first", "second").

Usage

The easiest way to use this is to clone this repo and copy the file print.h into your project. Then simply

#include "print.h"` 

at the top of the file, but after the system #includess.

Misc####

print is thread and exception safe. The output stream is locked once before anything is printed and then unlocked at the end inside a destructor. This ensures that the whole line will be printed without being interleaved with output from another thread.

Internally, the fwrite() family of function from C standard library are used for output. C++ streams are not used at all.

In benchmakrs print is a little bit faster than cout and a little bit slower than printf.

print.h includes stdio.h and nothing else.

print-stmt's People

Contributors

adob avatar

Watchers

 avatar  avatar

Forkers

lopesivan

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.