[fpc-devel] Copy() and ZeroBaseStrings

Sven Barth pascaldragon at googlemail.com
Mon Apr 7 08:01:25 CEST 2025


Bart via fpc-devel <fpc-devel at lists.freepascal.org> schrieb am So., 6. Apr.
2025, 23:19:

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

Please report a bug.

B.t.w. Is there an associate short option for this, as currently you
> cannot check {$IFOPT zerobasedstrings+} nor any other longoptions
> IIRC.)
>

No there's not.

Also it would be best to simply document as UTF8Copy working 1-based
because you can't detect from the callee-side whether it had been enabled
on the caller-side (especially if they're from different units).

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20250407/21be516e/attachment.htm>


More information about the fpc-devel mailing list