5 ms·
The complexity might be minimized, but not avoided. You would still need an escape mechanism for something like «She said «The \» key on the server doesn't work
by eesmith 9y ago
The complexity might be minimized, but not avoided. You would still need an escape mechanism for something like «She said «The \» key on the server doesn't work.»»
ASCII did add <>, [], and {}, any of which could have been used for quoted strings, had the programming language designers chosen that option.
https://en.wikipedia.org/wiki/String_literal#Paired_delimiters https://en.wikipedia.org/wiki/String_literal#Paired_delimite... points out that PostScript and Tcl have a string literal which allows matched quotes.
PostScript: (The quick (brown fox))
Tcl: {The quick {brown fox}}
- lisper 9y ago> You would still need an escape mechanism for something like... Yes, but that's a pretty rare case, much more so than embedded strings. Even that case could be solved by having two different quotes, like Python which allows both 'string' and "string". So you could do: «This is a string that mentions the ” character without escaping it» “This is a string that mentions the « character without escaping it” Yes, there are still some edge cases, like embedding both “ and « in the same string. But that's really rare.
- PickNChoose 9y agoYou don't want any 'rare' cases at all. That's the point. Stop using "punctuation" when you are attempting to "delimit" text. Use a character that is not punctuation, specifically designed for "field delimiter" purposes. Trying to do two things at once is ridiculous.
- diamondo25 9y agoI've been trying to push TSV (tab separated values) as a standard response/implementation when they ask for CSV. "Yes but its comma separated!", sure is, but text can contain commas... I have seen issues with Google Spreadsheets not recognizing the tabs however... Excel doesnt know what to do with a TSV either. But both have a complete wizard for parsing CSV...
- emmelaich 9y agoIf it's for my own programs, I use pipe (|) separated values. They're visually appropriate and even less likely than tabs.
- spc476 9y agoASCII does contain control characters set aside for record and unit separators (codes 30 and 31 respectively). Sometimes I wish they got more use than they do.
- Pxtl 9y agoExcept pipe is really easy to get as a typo. It's right next to the enter key. And then you're dealing with escaping characters and before you know if you've rolled your own file format. Been there. Use a lib that implements a documented standard, even a bad one. Only problem is Excel, which basically standardizes on CSV and occasionally mangles your data into malformed dates because reasons anyways.
- coldtea 9y agoThere is actually an ASCII character that doesn't appear in strings, and is meant to be used as such a separator. Actually two of them, record (30) and unit separator (31).
- slavik81 9y agoSadly, eventually someone will want to enter one document as a field in another document and then you end up needing escaping anyways. Using a rare symbol for the delimiter would still be nice for typing documents by hand, but it would have to be available on modern keyboards to be convenient.
- coldtea 9y ago>Sadly, eventually someone will want to enter one document as a field in another document and then you end up needing escaping anyways. Yes, but CSV files are record collections, they are not in 99% of cases recursive like that. If a column contains escaped secondary documents, there's something wrong.
- panic 9y agoIf your text is always valid UTF-8, there are various illegal UTF-8 octets available for this purpose: 0xff, 0xfe, and so on. Unlike null terminators or record separator characters, these characters are guaranteed not to exist in your string by the UTF-8 validation code you're already running.
- dragonwriter 9y agoAlso Ruby: %q{This is a string with an %q{embedded quote}.}
- stephengillie 9y agoPowershell: "This is a string with an 'embedded quote'." It's helpful to remember that quotes will interpret the variables inside, while apostrophes will not. Very useful for scripting the creation of scripts. Example: "It is $time" > It is 15:22 'It is $time' > It is $time "'$time' is $time" > '$time' is 15:22
- alanh 9y agonot sure why you were downvoted. Your comment is relevant and the convention can be useful. (PHP worked exactly the same way as your first two examples, although the third would have produced <'15:22' is 15:22>.)
- dragonwriter 9y agoThat's not a string where the same thing used for quoting is used inside the string without escaping, nor is it an example of the distinct begin-vs-end quote pairs approach under discussion. But, yes, having single and double quoted strings is another way to avoid escaping (which Ruby and a number of other languages discussed as supporting the approach being discussed also support.)
- stormbrew 9y agoRuby lets you use arbitrary tokens for string literals with %s{} (where the braces can be a bunch of things). I wish more languages would adopt this tbh.
- vorg 9y agoApache Groovy also had that in its early 1.0 betas but they were removed before their official 1.0 release party.
- petters 9y agoC++ has something similar.
- ziotom78 9y agoC++11 has this feature too [1], e.g.: const char * str = R"*^*(This is string containing an embedded "quoted" string)*^*"; [1] http://en.cppreference.com/w/cpp/language/string_literal http://en.cppreference.com/w/cpp/language/string_literal
- __david__ 9y agoRuby lifted that from Perl. say qq<I can do '" in here>;
- xelxebar 9y ago> You would still need an escape mechanism for ... I think this is actually desirable, since in your case the escape denotes different semantics. The unescaped pairs act like quotation operators while the escaped version is a character literal.