开发者

Why Doesn't Delphi 2009 Give A Message For A String Constant that is Too Long?

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 开发者_如何学JAVAhave 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜