Re:Parsing can become accidentally quadratic because of sscanf
by jchw on Monday March 01, 2021 @03:24PM
(#26307907)
I agree with you. However, the point of the parent comment is that you should not use scanf to do this; it is not designed to parse a small part of a large string. And indeed, I would say it is bad for more reasons than just this performance cliff. You should really prefer to use strtod or strtol on the exact scanned token instead. Even then, these functions may be impacted by C locale settings, which means you should probably instead prefer using implementations of this algorithm outside of libc...
[ Parent
| on HN ]
Re:Parsing can become accidentally quadratic because of sscanf
by josefx on Monday March 01, 2021 @04:50PM
(#26308938)
I just ran strtod through a debugger it at least ends up in strlen.S, so it probably wont improve the runtime complexity . It seems to be best to just assume the worst when it comes to any functions in the C standard library that have to do with string handling and avoid them in favor of literally anything else.
Re:Parsing can become accidentally quadratic because of sscanf
by jchw on Monday March 01, 2021 @05:02PM
(#26309056)
Did you call strtod with a null endptr? If not, I'd be surprised if it actually strlen'd the input rather than a copy of it. Still, I agree, you shouldn't really use the C standard library to do strings.
Re:Parsing can become accidentally quadratic because of sscanf
by bscphil on Monday March 01, 2021 @07:48PM
(#26310547)
> Still, I agree, you shouldn't really use the C standard library to do strings.
As someone who's been learning some C, I wonder if you have a recommendation for a guide on how to handle strings properly.
Re:Parsing can become accidentally quadratic because of sscanf
by cormacrelf on Monday March 01, 2021 @08:58PM
(#26311036)
It probably shouldn’t involve writing your own string functions. They’re so hard to get right, obviously. Last time I wrote C, I copied out the Rust Vec and String API as C headers and didn’t quite nail the implementation, but it was fun.
There are at least a few libs like sds that store the length information in various ways, which solves at least this kind of problem.
Re:Parsing can become accidentally quadratic because of sscanf
by jchw on Tuesday March 02, 2021 @01:21PM
(#26318493)
There’s plenty of string libraries, although ultimately no answer is perfect. It’s totally OK to use the libc string routines when learning C, but it would be good to keep in mind that it is not necessarily easy to write safe and correct software using it.
(Languages with more advanced string types and slicing mechanics are not impacted; C++, Rust, Go, etc. They do have their own issues of course.)