mludvig / mini-printf Goto Github PK
View Code? Open in Web Editor NEWMinimal printf() implementation for embedded projects.
Minimal printf() implementation for embedded projects.
Please specify a license for the code. Include a license file, specify the license in the readme, or put some license notice in the header of the files.
I recommend you a non-copyleft open source license like the BSD, Apache or MIT so this Software can be embedded in embedded applications that interact with other open source licenses or closed source, if you think is ok.
Kind regards
I tried this library.
This code includes three printf() which emit 2, 7, 9 respectively.
I think the second mini_snprintf with %s format stores "hello\0" (\0 = null termination) but returns 5 (\0 excluded from the count).
I think this behavior is really surprising for users. How do you think? With this behavior, I think it is impossible to know that we reach the end of the buffer.
#include <stdio.h>
#include "mini-printf.h"
#define N 10
int main(void)
{
char buf[N];
int head = 0;
head += mini_snprintf(buf + head, N - head, "%d", 10);
printf("%d\n", head);
// buf = 10
head += mini_snprintf(buf + head, N - head, "%s", "hello");
printf("%d\n", head);
// buf = 10hello
head += mini_snprintf(buf + head, N - head, "%s", "hello");
printf("%d\n", head);
// buf = 10hello\0he ????
// expected: 10hellohel
return 0;
}
Thanks for making this amazing library public.
I included a variant of mini-printf as part of my tinypy fork:
A few improvements:
an extension interface for outsource %o and %O to an external function. (under an ifdef, so this is optional) The interface is not thread-safe. So there is space to improve it; but we may want to allow stubbing out the entire buff interface if thread safety is already a concern.
always return the number of bytes would be written (excluding nul) even if that exceeds the size limit passed in. this is not posix though.
support for 'ld' 'lu', 'lx', 'lX' as long types.
padding of 's'.
5, padding to more than 9 chars and padding with space.
I am wondering if you are interested in merging some of these features. If so I'll prepare a PR.
As specified in the printf man :
o, u, x, X
The unsigned int argument is converted to unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal (x and X) notation. The letters abcdef are used for x conversions; the
letters ABCDEF are used for X conversions. The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded
on the left with zeros. The default precision is 1. When 0 is printed with an explicit precision 0, the output is empty.
I get a compiler error in MSVC 2013. Does this code work?
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.