Recent

Author Topic: I didn't expect this  (Read 586 times)

Ten_Mile_Hike

  • Full Member
  • ***
  • Posts: 101
I didn't expect this
« on: April 22, 2025, 10:00:58 pm »
I was bored today and just started playing in Lazarus trying to write stupid (obfuscated) code
for shits and giggles when all of a sudden THIS (indexing a hard coded string) WORKED.

Should I have been surprised that I am allowed to index a hard coded string?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button4Click(Sender: TObject);
  2. var c:char; x:integer;
  3. begin
  4.  for x:=1 to Length('Mississippi') do
  5.    if 'Mississippi'[x]='s' then Button4.caption:=Button4.caption+inttostr(X)+','
  6. end;
When any government, or any church for that matter, undertakes to say to its subjects, This you may not read, this you
must not see, this you are forbidden to know, the end result is tyranny and oppression no matter how holy the motives.

Robert A. Heinlein

ASerge

  • Hero Member
  • *****
  • Posts: 2404
Re: I didn't expect this
« Reply #1 on: April 22, 2025, 11:15:53 pm »
Should I have been surprised that I am allowed to index a hard coded string?
This is a long-standing opportunity. Such strings are usually defined as constants, which is why you didn't notice earlier.

440bx

  • Hero Member
  • *****
  • Posts: 5170
Re: I didn't expect this
« Reply #2 on: April 22, 2025, 11:25:14 pm »
Should I have been surprised that I am allowed to index a hard coded string?
Hard coded or otherwise, a string is an array of characters, indexing an array is completely normal.

It's just not obvious that the string is an array when it is hard coded.  For that reason, it's reasonable to be surprised but, it's completely logical.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Khrys

  • Full Member
  • ***
  • Posts: 215
Re: I didn't expect this
« Reply #3 on: April 23, 2025, 07:25:28 am »
String indexing is a powerful operation  ;)

Code: Pascal  [Select][+][-]
  1. program Calculator;
  2.  
  3. begin
  4.   WriteLn(Integer('eight plus eight'[0])); // Output: 16
  5. end.

Fibonacci

  • Hero Member
  • *****
  • Posts: 669
  • Internal Error Hunter
Re: I didn't expect this
« Reply #4 on: April 23, 2025, 07:49:48 am »
String indexing is a powerful operation  ;)

Code: Pascal  [Select][+][-]
  1. program Calculator;
  2.  
  3. begin
  4.   WriteLn(Integer('eight plus eight'[0])); // Output: 16
  5. end.

Wow :o Get this:

Code: Pascal  [Select][+][-]
  1. program Calculator;
  2. begin
  3.   WriteLn(Integer('2+2='[0])); // Output: 4
  4. end.

Bart

  • Hero Member
  • *****
  • Posts: 5558
    • Bart en Mariska's Webstek
Re: I didn't expect this
« Reply #5 on: April 23, 2025, 12:32:03 pm »
Wow :o Get this:

Code: Pascal  [Select][+][-]
  1. program Calculator;
  2. begin
  3.   WriteLn(Integer('2+2='[0])); // Output: 4
  4. end.

Try that one (or the previous) with {$H+}  >:D
(Or, to have fun with {$ZeroBasedStrings on})

Bart

simone

  • Hero Member
  • *****
  • Posts: 655
Re: I didn't expect this
« Reply #6 on: April 23, 2025, 01:40:01 pm »
In fact, these fun examples are only valid for short strings, where the first character (position #0) of the underlying array contains the length of the string, as shown in:

https://www.freepascal.org/docs-html/current/prog/progsu160.html#x204-2140008.2.7

Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

 

TinyPortal © 2005-2018