Giter Club home page Giter Club logo

Comments (9)

mpaland avatar mpaland commented on June 4, 2024 2

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.

Snaipe avatar Snaipe commented on June 4, 2024 1

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.

Leandros avatar Leandros commented on June 4, 2024 1

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.

mpaland avatar mpaland commented on June 4, 2024

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.

Snaipe avatar Snaipe commented on June 4, 2024

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.

Leandros avatar Leandros commented on June 4, 2024

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.

mpaland avatar mpaland commented on June 4, 2024

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.

mpaland avatar mpaland commented on June 4, 2024

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.

mpaland avatar mpaland commented on June 4, 2024

@Leandros , pretty cool idea! Implemented. ✔️

from printf.

Related Issues (20)

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.