R – Why Doesn’t Delphi 2009 Give A Message For A String Constant that is Too Long

compiler-errorsconstantsdelphidelphi-2009string

It got me stuck for an hour. I happened to have a string constant with about 280 characters in it.

I was trying to use Pos to look for a substring within a long string constant. It gives the result 0 when the string is longer than 255 characters, but gives the correct result when the string is shorter.

e.g.:

pos('5', '123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.12345')

Containing 255 characters correctly returns the number 5.

pos('5', '123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456')

Containing 256 characters gives a compiler error:
[DCC Error] E2056 String literals may have at most 255 elements.

pos('5', '123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.1234567')

But 257 characters or more does not produce any messages and incorrectly returns the number 0.

This led me on a wild goose chase for a while.

I also found the same is true for a simple assignment to a string, e.g.:

S1 :='123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456';

gives that error message and won't compile.

But add one more character, and S1 is assigned a null string.

Is there a Delphi option that should be turned on to warn me of this, or is this just a "bug" by the Embarcadero programmers?

Could someone please check if Delphi 2010 now gives a message for all strings >= 255 characters, or for just 256 characters and not for those >= 257.

If not, how do I go about getting it noticed at Quality Central? I can't even figure out how to see if that problem has been reported.


Thanks for allowing me to vent.

Kornel's answer linked to a forum discussion that links to the bug report that says it has been fixed in build 14.0.3513.24210.


p.s. Don't you think Embarcadero should have eliminated the 255 limit when Delphi 2009's Unicode strings were introduced?

Best Solution

Are you using AnsiStrings or ShortStrings? ShortStrings (string) have a cap on length, AnsiStrings don't (they're null terminated). Or alternatively, have you tried compiling with {$H+} (AnsiStrings by default)?

To get over the length limit of the constant, use "split longer into addition" + "of shorter strings under 255 chars".

Also, there's a similar discussion on the Delphi support forums here.

I assume that the reason why the literals can't be longer is because the compiler stores them as short strings (not to allocate them on heap) hence the one-byte-size length limit stands here.

As for why Delphi doesn't report it... well, it's a known bug that supposedly has been fixed, and even has a compiler patch.