views:

214

answers:

2

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?

+3  A: 

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.

Kornel Kisielewicz
Yes, I do know how to fix it. That is not the issue. I guess I'm just perturbed that Delphi didn't warn me of this and I had to start debugging to get to the root of the problem. But I'm using the default Unicode strings which are unlimited length. My assumption was that Delphi 2009 would also have had string constants of unlimited length. Leaving them at 255 characters is like limiting line length to 80 columns because that's what the computer card limit was. :-P
lkessler
I expanded my answer :)
Kornel Kisielewicz
Kornel: Thanks for the link to Andy's IDE Fix Pack. I had never heard of it before. It looks like it corrects some pretty severe errors (like slow debugging) that were in Update 3. And after installing it, the longer string constants are now reported as errors.
lkessler
You're welcome :)
Kornel Kisielewicz
I'd like to see some evidence for your claim that the compiler stores string literals in ShortStrings. String literals may contain any Unicode character, but ShortString can't.
Rob Kennedy
A: 

Delphi has a limit of 255 characters for a string constant. You can make it longer I think by concatinating together using + but its a very old limit based on the fact that in pascal and Delphi 1 all Strings were limited to a maximum of 255 characters.

Annoying but easily worked around.

Toby Allen
Only Delphi 1 had the 255 character limit. Strings changed in Delphi 2 (the first 32bit release) to allow longer lengths.
Allen Bauer
updated to reflect that.
Toby Allen