4 ms·
A more interesting question: what would our code look like if ASCII (or strings in general) didn't have escape codes?
by amelius 2y ago
A more interesting question: what would our code look like if ASCII (or strings in general) didn't have escape codes?
- wongarsu 2y agoIn PHP you see a lot of print('Hello World' . PHP_EOL); where the dot is string concatenation (underrated choice imho) and PHP_EOL is a predefined constant that maps to \n or \r\n depending on platform. You could easily extend that to have global constants for all non-printable ascii characters.
- jiehong 2y agoThe font you use could choose to display something for control characters, so they would have a visible shape on top of having a meaning. Perhaps like [0] (Unicode notation). [0]: https://rootr.net/im/ASCII/ASCII.gif https://rootr.net/im/ASCII/ASCII.gif
- zokier 2y agoIt would depend more on what we are intending to do, are we controlling a terminal, or are we writing to a file (with specific format). Terminal control is fairly easy answer, there would be some other API to control cursor position, so the code would need to call some function to move the cursor to next line. For files, it would depend on what the format is. So we might be writing just `<p>hello world</p>` instead of `hello world\n`. In fact I find it bit weird that we are using teletype (and telegraph etc) control protocol (what ASCII mostly is) as our "general purpose text" format; it doesn't make much sense to me.
- rswail 2y agoExcept that terminals didn't exist when ASCII was created, printers existed, either with a "carriage" that carried the actual print head (like IBM Selectric typewriters, or the original ASR33 teletypes) or "chain printers" that printed an entire line at a time with a set of rods for each character position that had the entire character set on them.
- ivanjermakov 2y agoThis is actually a valid problem when writing quines[1]. You need to escape string delimiters in a way without using its literal. This is what `chr(39)` is for in the following Python quine: a = 'a = {}{}{}; print(a.format(chr(39), a, chr(39)))'; print(a.format(chr(39), a, chr(39))) [1]: https://en.wikipedia.org/wiki/Quine_(computing) https://en.wikipedia.org/wiki/Quine_(computing)
- eru 2y agoYou don't need these gymnastics of knowing ASCII in Python quines: a = '''a = {} print(a.format(repr(a)))''' print(a.format(repr(a))) (OK, it's not a quine yet, because the output has slightly different formatting. But the formatting very quickly reaches a fixed point, if you keep feeding it into a Python interpreter.) The actual quine is the slightly less readable: a = 'a = {}\nprint(a.format(repr(a)))' print(a.format(repr(a))) https://github.com/matthiasgoergens/Quine/blob/c53f187a4b4030437005ddd12bd5a8594269b437/quine.c https://github.com/matthiasgoergens/Quine/blob/c53f187a4b403... has a C quine that doesn't rely on ASCII values either.
- fsckboy 2y ago>what would our code look like if ASCII didn't have escape codes? ASCII is the layer that doesn't have escape codes (although it does have a single code for ESC), ascii is just a set of mappings from 7-bit numbers to/from mostly-printable characters
- ezequiel-garzon 2y agoAt least a full fourth of the ASCII code points, 0x00-0x1F, are not printable. A bit more, as we should add del (0x7F) and, according to some though more controversially, space (0x20).
- fsckboy 2y agothe full forth may not be "printable if you define printable as using toner and ignoring positioning". But they aren't escape codes.
- amelius 2y agoWhat would you call the lower 32 or so codes then? Control codes?
- fsckboy 2y agothey are explicity defined as escape codes in terms of inputting them, but not in terms of receiving them. They are not by any stretch escape codes. See ANSI (which ASCII is a part of) if you want to learn about ESCAPE codes.
- zzo38computer 2y agoCodes 0x00 to 0x1F are called control codes (and in some contexts (such as use of ASCII control codes with TRON character set) 0x20 is also considered to be a control code). The code 0x7F is also a control code. (The PC character set (which is a superset of ASCII) has graphics for those codes as well, although in plain ASCII there are no graphics for the control codes.)
- rswail 2y agoIf ASCII didn't have control sequences then teletypes wouldn't have worked. How do you make the carriage (a literal thing on a chain being driven back and forth) "return" to the start of the line without a "carriage return" code? How would you make the paper feed go up one line without a "line feed" code? Same for ringing the bell, tabs, backspace etc. A "new line" on a teletype was actually two characters, a CR and a LF. Unfortunately, using one of the other sequences (eg RS, for "Record Separator") would have saved billions of CPU cycles and misinterpreted text files dealing with the CRLF, CR, LF, LFCR sequences to mean "new line".