Skip to content

Copy() doesn't work correctly with ZerobasedStrings

Summary

Copy(SomeString,1,1) gives incorrect result if ZeroBasedStrings is on

System Information

Windows10-64, fpc main (3.3.1-6095-gd0b4e8730a) 32-bit.

  • Device: Computer

Steps to reproduce

See example code below.

Example Project

program z;

{$longstrings on}
{$zerobasedstrings on}

var
  S, T: string;
begin
  S := 'ABC';
  write('S[0]        = "',S[0],'"');
  if (S[0] <> 'A') then writeln(', FAIL: expected: A') else writeln(' [Ok]');
  write('S[1]        = "',S[1],'"');
  if (S[1] <> 'B') then writeln(', FAIL: expected: A') else writeln(' [Ok]');
  write('S[2]        = "',S[2],'"');
  if (S[2] <> 'C') then writeln(', FAIL: expected: A') else writeln(' [Ok]');
  T := Copy(S,0,1);
  write('Copy(S,0,1) = "',T,'"');
  if (T <> 'A') then writeln(', FAIL: expected: A') else writeln(' [Ok]');
  T := Copy(S,1,1);
  write('Copy(S,1,1) = "',T,'"');
  if (T <> 'B') then writeln(', FAIL: expected: B') else writeln(' [Ok]');
end.

What is the current bug behavior?

Outputs:

S[0]        = "A" [Ok]
S[1]        = "B" [Ok]
S[2]        = "C" [Ok]
Copy(S,0,1) = "A" [Ok]
Copy(S,1,1) = "A", FAIL: expected: B

What is the expected (correct) behavior?

...
Copy(S,1,1) = "B" [Ok]
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information