Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: add bounded versions of f2s(), d2s() et al #183

Open
biojppm opened this issue Oct 12, 2020 · 1 comment
Open

Feature request: add bounded versions of f2s(), d2s() et al #183

biojppm opened this issue Oct 12, 2020 · 1 comment

Comments

@biojppm
Copy link

biojppm commented Oct 12, 2020

A very common usage pattern of such functions is the following

// the output buffer; could be another type, eg char*
std::string s;
// how large is enough? We can never 100% sure in advance
// that the buffer size is enough for a safe write
s.resize(5);
// s is not enough for this:
float value = 1234567.f;

// So make s2f() aware of how much space we can write on.
// The requested function receives how large the write buffer is,
// so it can safely stop writing to it when the space is exhausted,
// but does not return and in fact proceeds
// through the algorithm /as if/ if it was writing, so it can find the needed size,
// which would be the return value:
int needed_size = f2s_buffered_sz(value, s.data(), (int)s.size()); 

// With this we now know the needed size, so we call the function again if the
// buffer was not large enough:
if(needed_size >= (int)s.size()) {
     s.resize(needed_size + 1); 
     needed_size = f2s_buffered_sz(value, s.data(), (int)s.size());
     assert(needed_size < s.size()); // there!
}

But to have this, we would require a version of such functions taking the buffer size and returning the required size.

Would you be open to a PR for this feature?

@biojppm
Copy link
Author

biojppm commented Oct 12, 2020

FWIW I just saw #149 which is a different approach but in the same spirit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant