Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi silently cropping string literals

Delphi 2009 Win32.

The code below tries to add a 257 length string to a memo. It compiles and runs fine, but nothing is added to the memo.

Memo1.Lines.Add('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');

Looks like a compiler bug. Is it? Because if the string was 256 long, I'd get a compiler error and couldn't compile the app.

Any way to make the app break when the developer tries to do something like this?

I know I could split the string and make this code work, but my point is to prevent developers for using this invalid code without noticing.

Thanks

like image 448
Erick Sasse Avatar asked Mar 27 '09 12:03

Erick Sasse


3 Answers

This is a Delphi 2009 bug with string literals, it should raise the same error as D2007.

Try latest version of Andreas IDE Fix pack, its supose to fix this bug. http://andy.jgknet.de/blog/?page_id=246

like image 105
Cesar Romero Avatar answered Nov 14 '22 21:11

Cesar Romero


I agree with Gamecat, however if your dealing with a string that large, I would break it into muliple lines to assist in reading/editing.

if you are LITTERALLY trying to create 257 "a"'s then why not use the DupeString function in the StrUtils unit?

Memo.Lines.Add( DupeString('a',257) );

Much easier to read, and maintain later. If you are doing this in a loop and therefore are worried about performance, then assign the function result to a local variable and use the variable.

var
  sLotsOfAs : string;
  ix : integer;
begin
  sLotsOfAs := DupeString('a',257);
  for ix := 0 to 1000000 do
    Memo.Lines.Add( sLotsOfAs );
end;
like image 2
skamradt Avatar answered Nov 14 '22 19:11

skamradt


The string literal can be only 255 characters long. Not sure why they kept this limitation. But you can solve it using multiple literals:

Memo1.Lines.Add('i have 128 chars' + 'i also have 128 chars').
like image 1
Toon Krijthe Avatar answered Nov 14 '22 19:11

Toon Krijthe