mickjc750 / prnf Goto Github PK
View Code? Open in Web Editor NEWA lightweight tiny printf alternative. With some reasonable limitations, extensions, and alternative behaviour suited to microcontrollers.
License: The Unlicense
A lightweight tiny printf alternative. With some reasonable limitations, extensions, and alternative behaviour suited to microcontrollers.
License: The Unlicense
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.
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.
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.
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.