Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

A more interesting question: what would our code look like if ASCII (or strings in general) didn't have escape codes?


If 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".


>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


At 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).


the full forth may not be "printable if you define printable as using toner and ignoring positioning". But they aren't escape codes.


What would you call the lower 32 or so codes then? Control codes?


Codes 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.)


they 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.


It 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.


Except 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.


This 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)


You 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/c53f187a4b403... has a C quine that doesn't rely on ASCII values either.


In 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.


The 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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: