[fpc-devel] Copy() and ZeroBaseStrings

Bart bartjunk64 at gmail.com
Sun Apr 6 23:18:47 CEST 2025


Hi,

Is this a known issue?
===
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.
===

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


(I played a little with this because Copy() accepts Index < 1 for
"normal" AnsiStrings, and in Lazarus Utf8Copy() does not, so I wanted
to see how his behaved with zerobasedstrings on.
B.t.w. Is there an associate short option for this, as currently you
cannot check {$IFOPT zerobasedstrings+} nor any other longoptions
IIRC.)

-- 
Bart


More information about the fpc-devel mailing list