Lazarus

Free Pascal => General => Topic started by: tygraphics on September 29, 2020, 10:36:01 pm

Title: Using ANSI Strings to avoid exceeding string limit at compile
Post by: tygraphics on September 29, 2020, 10:36:01 pm
When I try to compile my pascal code that has a string longer than 255 char I get the following error:
VeryLongNames_G.p(24,250) Fatal: String exceeds line

Is there a setting or flag that allows the compiler to accommodate strings that are greater than 255 char?

Thanks!
Title: Re: Using ANSI Strings to avoid exceeding string limit at compile
Post by: Handoko on September 29, 2020, 10:37:48 pm
Use {$H+}:
https://www.freepascal.org/docs-html/prog/progsu25.html
Title: Re: Using ANSI Strings to avoid exceeding string limit at compile
Post by: tygraphics on September 29, 2020, 10:49:02 pm
I placed the {$H+} on the main program but got the same error. Does it have to be added to any units that have long strings?
After adding {$H+} to the unit with the long strings and it compiled!
Thanks for the help!
Title: Re: Using ANSI Strings to avoid exceeding string limit at compile
Post by: jwdietrich on September 29, 2020, 10:52:43 pm
I placed the {$H+} on the main program but got the same error. Does it have to be added to any units that have long strings?

Yes, in the beginning before declaring or using any strings (but after mode directives).
Title: Re: Using ANSI Strings to avoid exceeding string limit at compile
Post by: tygraphics on September 29, 2020, 11:01:00 pm
I'm generating the unit files as I parse through a collection of hundreds of components.
Is there any harm in adding the {$H+} as a standard part of all my unit files?
Title: Re: Using ANSI Strings to avoid exceeding string limit at compile
Post by: jwdietrich on September 29, 2020, 11:07:53 pm
I'm generating the unit files as I parse through a collection of hundreds of components.
Is there any harm in adding the {$H+} as a standard part of all my unit files?

Probably not, but if your code contains mode directives {$H+} may be deactivated again (with the exception of {$MODE DELPHI}, which implies {$H+}).
Title: Re: Using ANSI Strings to avoid exceeding string limit at compile
Post by: Kays on September 30, 2020, 12:43:36 am
[…] Is there any harm in adding the {$H+} as a standard part of all my unit files?
You can write your own fpc.cfg (https://freepascal.org/docs-html/current/user/usersu10.html) and place -Sh (https://freepascal.org/docs-html/current/user/userap6.html#x193-200001r1) (the command line option to enable ANSI strings by default) in it.
Title: Re: Using ANSI Strings to avoid exceeding string limit at compile
Post by: lucamar on September 30, 2020, 12:51:16 pm
When I try to compile my pascal code that has a string longer than 255 char I get the following error:
VeryLongNames_G.p(24,250) Fatal: String exceeds line

For completeness sake, note also that the error doesn't normally mean that the string is too long but that it is not terminated in the same line, usually meaning that you have code like:
Code: Pascal  [Select][+][-]
  1. somestring := 'This string isn't well formed';
instead of:
Code: Pascal  [Select][+][-]
  1. somestring := 'This string isn''t well formed';
(note the doubled single quotes in "isn''t") or that you forgot  to close the string (i.e. the last single quote is missing).

HTH :)
Title: Re: Using ANSI Strings to avoid exceeding string limit at compile
Post by: tygraphics on September 30, 2020, 02:29:48 pm
Yes, the string was malformed and that caused the error. It was malformed because of a hack I wrote many years ago that breaks long writeln statements into as many write statements as necessary to keep them under 250 char. Unfortunately in this case the chop point was in a bad location. Yesterday I fixed my code and it handled the 367 char string without error due to adding the {$H+} to the top of my generated units that could likely have a long string.

Thank you to everyone who answered! 
TinyPortal © 2005-2018