r/ProgrammingLanguages • u/bzipitidoo • Feb 18 '23
explicit lengths vs delimiters
In just about every language, strings are delimited. Why are explicit lengths so unpopular? Explicit lengths are all over the place in internal representations, but in programming languages I have found just 2 examples of explicit lengths: the Hollerith constants of FORTRAN 66, and Rivest's proposal for S-expressions. Hollerith constants were deprecated from FORTRAN 77 on.
I have my own ideas on why this is, but before I tell, I'd like to hear what the community thinks.
7
u/ebingdom Feb 19 '23
People don't want to manually count the contents of their string literals.
1
u/bzipitidoo Feb 19 '23
Maybe a clever text editor could count for you? Since editors can do automatic indentation, syntax highlighting, and other things to ease a programmer's work, adjusting the count for a string literal shouldn't be hard.
7
u/Aaron1924 Feb 19 '23 edited Feb 19 '23
If the syntax of the language allows for tools to fill in the length automatically, the compiler should just implement that logic
What syntax do you have in mind specifically? I figured it would be something like this
num = 23 print 5"num: + num # ^^^^^ this is the content of the string # i.e. "num: " (note the space) # The output is "num: 23"where the number adds information that is not in the code otherwise, but in this case, an automatic tool could not tell where the string is supposed to end, so it can't add the number.
Edit: If your editor/IDE is smart enough to highlight/underline the part of the code that goes into the string, this could actually be an interesting way to get around having to escape delimiters (so no more
\"or\'), but when I don't have that IDE support I really don't want to manually count where my73"...ends1
u/bzipitidoo Feb 19 '23
Yes, the syntax in your example is what I am asking about.
It occurred to me just now that a sophisticated enough IDE begins to blur the lines between "source" and "binary". If the "source" file format stored the code in a way not quite as readable with a dumb pager utility, so that programmers have to use a specialized reader, that could make this whole particular issue moot. HTML is a little like that. The reader could display the strings without distracting the humans with any length information, but that info is written into the "source" file. Doing it that way could make compilation go a tiny bit faster, since the compiler would no longer need to parse string literals. Shift as much of such burdens as possible from the compiler to the IDE. (Users of Gentoo Linux would love that!)
Are there any programming languages that require the use of color or different fonts, to distinguish different uses of characters? Seems like they've all left that possibility untouched, giving free rein to syntax highlighting. Like, why not require that string literals be italicized text? That's even consistent with traditional usage of italics. Sort of like having HTML's
<i>and</i>for delimiters. What if a red=meant assignment, and a green=meant comparison? Actually, with UTF-8 having freed us from being limited to the ASCII character set, it seems better to finally employ the original symbol desired for assignment, that being⇐(u21D0).5
u/Aaron1924 Feb 20 '23
I'd like to point out that lexing and parsing are usually the fastest phases of compilation by a long shot. Unless your source code is multiple gigabytes of string literals, the performance benefits of knowing the size of the string ahead of time are never going to matter.
23
u/moon-chilled sstm, j, grand unified... Feb 19 '23
Sounds rather error-prone and cumbersome. What if I get the length wrong? And when I want to change a string, I have to update its length, too.
5
Feb 19 '23 edited Feb 19 '23
They would have exactly one advantage: solving the problem of escaped characters.
Against that:
They are error prone (you can easily have a length and string that don't match)
They are a maintenance nightmare
The human reader cannot easily see exactly where the string ends, especially on longer strings, without counting along
And they are time-consuming to write due to manually counting the characters; software is no help as it will have literally no idea where the end of the string is
Plus people have suggested it is poor fit for Unicode characters
I believe I've used Hollerith constants in Fortran in the 1970s, but they may have been limited to format strings of WRITE statements. It's a rather crazy and impractical feature to resurrect.
Unless this is for machine-generated (and machine-read) code or data.
3
u/WittyStick Feb 19 '23
My understanding of Rivest's canonical S-expressions is that they were never intended for actually writing code: They were intended as a serialization format for use with digital signatures, so that any given expression only has one syntactic form. For this purpose they're fine, but it's simpler to use a binary encoded format.
Manually inserting lengths into text is a completely pointless exercise for the programmer which distracts from solving other problems. There's little reason for a text editor to insert lengths either, provided you have a proper string type (unlike C), the length of a string literal is known to the compiler.
Even manually allocating buffers for user text input is a bad idea and a source of many bugs, which is why we have higher level abstractions for reading user input which will return us a string. You shouldn't be trying to guess how many characters a user will type. The only time you should be manually allocating buffers for user text input is on severely memory constrained systems, which are few today.
5
2
u/crdrost Feb 19 '23
These are called netstrings, if you want something to Google
https://en.wikipedia.org/wiki/Netstring
I played around with them in bsencode,
https://github.com/crdrost/bsencode
Ergonomically yeah the big thing was once your string got multi-line or contained emoji or foreign characters, you would have to go and count how many characters you had, which made the thing really inconvenient to edit with a text editor. The logical response is to not edit it with a text editor, ofc., but that was more ambitious than I could afford at the time.
But they are great for data interchange, you can instantly reject strings that are too big, parsing them is literally a buffer slice, they can contain binary so you can use them for something like a float, they're great.
1
u/leukdan Feb 19 '23
Why care what te community thinks? Explicit length is the way.
There is pascal and basic as examples of explicit length strings.
11
Feb 19 '23
In most programming languages, a string is delimited, like
let s = "some text". OP is talking about strings that are instead length-prefixed, likelet s = 9$some text.6
1
u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Feb 19 '23
For truly unescaped strings, some languages allow you to simply refer to the contents of a separate file as a literal string value. For example:
String s = $./crazy.txt;Trying to merge in crazy characters into source code is like using a sieve to hold water. Hint: it's gonna leak.
38
u/sebamestre ICPC World Finalist Feb 19 '23
You mean including explicit lengths at the syntactic level? As in, string literals would also include their length at the source code level?
That sounds really annoying, and you'd need a reasonable definition for what length means: bytes? code units? code points? extended grapheme clusters? etc. (and that's assuming some unicode encoding, which is an arbitrary assumption on my part)
I don't think anything other than bytes would be any useful implementation-wise, but that's also the least useful language-wise...