5 ms·
> [...] so you can't use it for just blindly extracting numeric tokens at all [...] So in other words, it's completely useless? > This is not an intended use
by adwn 6y ago
> [...] so you can't use it for just blindly extracting numeric tokens at all [...]
So in other words, it's completely useless?
> This is not an intended use case for sscanf.
Then what is?
- nitrogen 6y agoThe comment to which you reply uses the %[] conversion to impose stricter limits on acceptable characters to be interpreted as numbers. My guess at the historical intention of *scanf is that it was meant for parsing tab-delimited and fixed-width records from old-school data files.
- sdenton4 6y ago(As opposed to the comma-separated data files that we enlightened future-folk use!)
- Darkphibre 6y agoYou know, why don't we use 0x1F (Unit Separator)? Seems like we have had a hierarchical delimiters since ASCII / punch cards, yet I never hear about them. https://en.wikipedia.org/wiki/C0_and_C1_control_codes#Field_separators https://en.wikipedia.org/wiki/C0_and_C1_control_codes#Field_... CSVs are such a pain if you have freeform text (in which you have to handle newlines and escaped quotes). It seems like using FS/GS/RS/US from the C0 control codes would be divine (provided there was implicit support for viewing/editing them in a simple text editor). I get that it's a non-printable control character, and thus not traditionally rendered... but that latter point could have been done ages ago as a standard convention in editors (enter VIM et. al., as they will indeed allow you to see them).
- jimmaswell 6y agoIt's nice being able to edit a CSV in a text editor. It wouldn't be worth it to lose that just to not have to escape some strings.
- afiori 6y agomore than that it is nice to edit files with a standard keyboard,
- db48x 6y agoIf your editor doesn't allow you to enter arbitrary characters using a standard keyboard then you need a better editor.
- chousuke 6y agoThe problem is not the editor, it's the human typing on the keyboard that sees , and thinks "that'll do". Using an obscure dedicated character is not going to happen.
- Darkphibre 6y agoI guess my point was a curiosity that these control codes had become obscure, when text format interchange is so prevalent throughout computing history.
- afiori 6y agoI do not want to learn new input methods for every program, I want a standardized compose-like functionality that allow me to write both diacritics and backticks ( a plus for the rest of unicode)
- db48x 6y agoAll operating systems have that too. Look up "Input Method Editor" in your favorite OS's documentation. See also dead keys, compose key, AltGr, etc, etc. If you think you can't edit a file because it has funny characters in it, then you're not trying hard enough.
- afiori 6y agoI know, still there is no reasonable way* to do both common european diacritics and backticks/tilde on windows without installing third-party software. * I find dead keys irritating beyond reason, so I do not count them as an option
- 0xbadcafebee 6y agoThere are plenty of ways a lazy programmer will want to pass the records into something else that either doesn't handle non-printables, or does but the user doesn't like how it looks. TSV also works more reliably than CSV, because most people don't put tab characters in the data in these kinds of records. Tab is even the default field delimiter for cut. But everybody uses CSV, because again it's easier to reason about the above. (shrug)
- nitrogen 6y agoNo matter what delimiters we use, we still have either a data sanitization problem or a data escaping problem. I've worked with a wire protocol that used the ASCII record delimiters, but with binary data so you also had to use binary escape (0x1b) and set the eighth bit on the following byte.
- elygre 6y agoWhich is a mess, because some parts of the world use commas as decimal commas: 3,14159etc. So we have to use semicolon for our CSVs.
- bmn__ 6y ago> some parts of the world most parts Why can't you be normal? https://commons.wikimedia.org/wiki/File:DecimalSeparator.svg https://commons.wikimedia.org/wiki/File:DecimalSeparator.svg
- dmit 6y agoI submitted a patch to support decimal commas when parsing timestamps in Go in 2013. I thought this was a slam dunk because while major users of decimal dots include USA, China, India, and Japan, the decimal comma is used by pretty much every other country in the world. Going from ~40% support to ~99.9% support seemed like an obvious win. Rob Pike politely declined the patch, commenting "I might prefer to leave it unaddressed for the moment. Proper locale treatment is coming (I hope soon) and this seems like the wrong place to start insinuating special locale handling into the standard library." Three years later another Go team member commented that "Date localization is definitely still in the planning." We're in year 8 now. The issue is still open. Rob Pike is still hoping.
- nicoburns 6y agoThis is where Rust's decision to keep such functions out of the standard library entirely starts to look pretty smart.
- boogies 6y ago> ~40% support to ~99.9% support № of countries ≠ № of people though. ⅘ of the 5 most populous countries¹ use decimal points, and together they alone² have ~42½% of the world’s people. Do all decimal point countries together have ≤50%? (Edit: Is it really not “normal” to do things the Anglophone way?) ¹According to https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population https://en.wikipedia.org/wiki/List_of_countries_and_dependen... ²Not counting territories
- unilynx 6y agoC is full of record based stuff. Apart from the numeric modifiers to printf and scanf we have fread, fwrite, calloc all taking record sizes
- deleted 6y ago[deleted]
- kazinator 6y agoYes, scanf is almost completely useless, and sscanf a little less so. sscanf is useful if you need a pattern-based breakdown of some string datum, and the patterns are not out of sscanf's league. It will work "anywhere"; it's been in C since before 1989 ANSI C. In the entire TXR project, I used sscanf in one function: a floating to integer conversion routine. The context is that integers can be bignums, so we can't just rely on ordinary conversion, except in the fixnum range. http://www.kylheku.com/cgit/txr/tree/arith.c?h=txr-252#n2912 http://www.kylheku.com/cgit/txr/tree/arith.c?h=txr-252#n2912 The approach taken is to sprintf the double value into a generous buffer, with a generous precision, using the %g format which will automatically use the e notation if necessary. We detect whether the e notation was produced, and other characteristics, and then proceed accordingly. I see that an impossible case is being tested for; since values with a small magnitude are handled via the fixnum route, we will never hit the case where we can short-circuit to zero. Can anyone see a bug here, by the way? :)