Printf utility function for strings #1038
relativityspace-jsmith
started this conversation in
Ideas
Replies: 2 comments 2 replies
-
Have you looked at |
Beta Was this translation helpful? Give feedback.
0 replies
-
I think if I were to add |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I really love ETL strings as a safe alternative to C strings for embedded. (you would not BELIEVE how much dubiously safe C string code I had to review before we adopted ETL.) However, they do have a bit of an awkward limitation, which is that there's not an out of the box way to print formatted data into them. It's not difficult to do yourself, since you can pass the string's buffer in to sprintf(), but that opens the door for memory errors if you mess up your math.
At my company, we've created the following utility functions to make this easier:
This way we don't have to worry about passing the right buffer size into sprintf or checking the return values -- it just works. It's been MONTHS since I've had to read the manual page for snprintf() and I'm so happy.
(side note: As you probably know, the format attributes are GCC (+ clang?) specific and are for generating compiler warnings. They will probably need some preprocessor logic to disable them if they are not supported).
@jwellbelove Would you be interested in adding these functions in to ETL so that they're available for everyone? Feel free to add them based on this code, or if you tell me where to put them and what to call them, I can add them.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions