Giter Club home page Giter Club logo

prnf's People

Contributors

mickjc750 avatar zappitec avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

dosworld zappitec

prnf's Issues

Consider non-standard feature. Re-purpose useless %n to print a string then free it from the heap

When attempting to print strings generated by application specific functions, there are some difficulties...

For example, consider a function which takes an integer 'seconds' and describes the duration as xxDxxHxxMxxS.

char* period2txt(char* buf, int seconds);

If this function is used for multiple arguments to a single prnf(), a separate buffer must be provided for each call, eg.

char buf1[20];
char buf2[20];
prnf("Fred is %s, Bob is %s\n", period2txt(buf1, freds_age), period2txt(buf2, bobs_age));

The above is ugly and error prone. Accidentally put buf1 in both, and it breaks. Undersize the buffer and it breaks.

Proposed solution

The standard printf() provides %n, which is normally used to write the number of characters printed so far, to an integer pointed to by the argument. I cannot think of a practical application of this, and I don't know why it exists.
As %n expects a pointer (int*) as an argument, this could actually point to a string in a heap allocation. prnf() could then free the allocation after printing it.
If our function period2txt() makes a heap allocation when buf is passed as NULL, then we could do this:

prnf("Fred is %n, Bob is %n\n", (int*)period2txt(NULL, freds_age), (int*)period2txt(NULL, bobs_age));

Or, if period2txt is only to be used as a prnf argument, it could be of the form:

int* prnf_n_period(int seconds);

This would allow usage as simple as:

prnf("Fred is %n, Bob is %n\n", prnf_n_period(freds_age), prnf_n_period(bobs_age));

There are some benefits to this.

  1. Mixing up %s and %n will give you a compilation warning, as %s expects (char*) and %n expects(int*)
  2. Misuse of prnf_n_period() for other purposes, such as using it in an assignment to a char* will also cause a compilation warning due to pointer types.

Unfortunately the compilers format checking will complain if we try to specify width or precision for %n. So no alignment by prnf for %n strings would be possible.

If anyone is out there.. please share your thoughts on this.

possible performance increase

If the buffer output, and function pointer output, were to be handled separately, this could improve the performance of both.

Buffer output, would no longer need to call a function pointer per character.
Function pointer output, would no longer need to test for a size limitation.

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.