Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can the circumflex sign be used for control chars in Delphi and is it a good idea?

I just came across an answer on SO with a curious syntax:

How do I include a newline character in a string in Delphi?

MyString := 'Hello,' + ^M + ^J + 'world!';

I've been using Delphi for several years now, but I didn't know you could use the circumflex sign for control characters.

Is this just a left over from the early Delphi or Turbo Pascal days?

Should it be used nowadays?

PS: I'm not asking about advice on the line break characters, there is sLineBreak and other methods as discussed in the original question.

like image 597
Jens Mühlenhoff Avatar asked Oct 25 '12 06:10

Jens Mühlenhoff


2 Answers

No, it is not from Turbo Pascal days. It is from decades before TP, and before MS-DOS, and probably even before UNIX. Something old like first 300 bit-per-second dialup modems and DEC VT-52 terminal, RT-8 OS on PDP-8 machine and early version of C. Or maybe even older - though everything older to me is mere legends :-).

"^" sign is shortcut for "Ctrl" key. So ^C in traditional notation stands for Ctrl+C in Microsoft notation. That notation was vastly used for textmode menus in MS-DOS times, like in the aforementioned Turbo Pascal, Norton Utilities, DOS Navigator, etc.

Out of my memory you can consider "^" for "subtract 64".
So as Chr(65) is 'A' then Chr(1) would be ^A.
And ^@ would be #0 :-) AFAIR in MS-DOS times pressing Ctrl+Shift+"2/@" would actually produce #0 into BIOS keyboard buffer :-)

^[ would AFAIR be #27 aka Esc(ape) char - and if you run telnet.exe you would see it prompted as the escape character.

So Turbo Pascal long ago chosen to follow the time-blessed convention, and then rules of backward compatibility engaged ever since. Personally, i take 'bla-bla'^M^J'foo-baz' literal more string-like than 'bla-bla'#13#10'foo-baz' when you want it on one line. And constructing the value with plus is better fit when your literal takes several source lines.

The pity is that syntax highlighting in Delphi IDE is hopelessly broken on that kind of constants.

like image 154
Arioch 'The Avatar answered Oct 25 '22 04:10

Arioch 'The


Yes this is a legacy from days of yore.

And no you should not get into the habit of using this feature. Remember that code is read more often than it is written. Always think of your readers who most likely won't know what that syntax means.

like image 43
David Heffernan Avatar answered Oct 25 '22 06:10

David Heffernan