Comments (9)
That was easier than thought. 😄
I created a out_function
-branch which fixes the return value - and simplified the code. Should be conform to spec now.
@Leandros , giving a nullptr as buffer to get the formatted string length is working as well now.
@Snaipe should be easy for you to modify/adapt the low level output routines (_out_char
) for your UTF-8 support.
I have to make some more tests and if all works well it's going to be merged. Test suite is already passing.
Thanx a lot for your feedback!
from printf.
AFAIK fmt
requires a C++ compiler; and other solutions that are C-only aren't quite as complete.
Reading the code, I see the rationale behind the current behaviour, and I understand why it wouldn't be that high priority. I'm mostly interested in this issue because I'm looking for a replacement for the printf function family on Windows, because their implementation is horrible to work with when manipulating UTF-8 strings (and even the mingw implementation has a weird locale-aware behaviour that's quite inconsistent when formating characters not in the "preferred code page").
I could submit a PR when I get the time, if you don't mind.
from printf.
Awesome! Looks great! One minor nitpick, though. You could move the NULL
check out of the _out_buffer
routine, and simply pass in an empty function when the output buffer is null. That would remove the branches in this case and slightly improve performance.
from printf.
Hi Arvid, thanks for your feedback.
Currently the return value is defined as
Functions snprintf() and vsnprintf() don't write more than count bytes, including the terminating null byte ('\0'). If the output was truncated due to this limit, the return value is count, which indicates any truncation.
The "hello, world" produces 12 chars plus the trailing \0. 12 would normally be returned.
The count size of 10 in the example above is the limit. As truncation occurs, 10 is returned and "hello, wo"\0 is written.
I think, it's okay.
from printf.
This interpretation is somewhat incorrect. According to the man page (emphasis mine, also see 7.19.6.5.3 from the C99 spec):
The functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte ('\0')). If the output was truncated due to this limit, then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated.
Which means that in this case, the return value should be 12
, not 10
. This behaviour is actually very important for vsnprintf
, as it allows one to call vsnprintf(NULL, 0, fmt, vl)
to determine the appropriate size of a buffer suitable for formatting.
from printf.
This behavior is documented in the readme. An as far as I can tell, the desired behavior.
@mpaland I can see your reasoning there, makes sense.
from printf.
Guys, I see your point here and I know that the current behavior is not conform to specification.
In the first way I thought that the printf
return value doesn't care most people (including me) at all.
To get the length of a formatted string or to format a string itself, there are many libs around, like fmt.
I tried to find a balance between code size, complexity and supported features. My goal was not to write a 100% feature complete 1:1 plugin for the original printf
.
Anyway, I have an idea to refactor the output a little bit to get a spec conform return value, but this has no high prio.
I leave this issue open for tracking.
from printf.
Hello @Snaipe , I don't mind at all!
My idea for refactoring this is to have a central (string) buffer write routine which checks for buffer maxlen and stop writing accordingly. This could eliminate a lot of (idx < maxlen)
and simply the code.
I'll check this out.
from printf.
@Leandros , pretty cool idea! Implemented. ✔️
from printf.
Related Issues (20)
- Use more appropriate types for base, precision and width HOT 4
- Avoid geneal-case division and modulus in _ntoa functions
- Undefined behavior on INT_MIN HOT 1
- Proper handling of denormals
- printf_("%.1e", 9.96) prints "10.0e+00", should print "1.0e+01"
- Printing +/-0 with "%g" doesn't fall back to decimal mode
- More cases of "%g" not falling back to decimal mode
- Support for FreeRTOS lxip specifier HOT 1
- Siginificant-digits-to-precision transition made invalid by rounding
- Floating point printing - how?
- Vfctprintf
- could you support %I64d %I64u like msvc done? HOT 1
- [notice] https://github.com/eyalroz/printf <-This repo is maintaining HOT 8
- Zero padding disabled when precision specified HOT 3
- this is incredible HOT 6
- WebAssembly HOT 2
- GCC flags for freestanding? HOT 2
- for declaration not compatible with C compilers HOT 1
- _vsnprintf reads past the format parameter value HOT 6
- I've created an Arduino Library from your source code HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from printf.