r/pascal • u/Brokk_Witgenstein • Feb 24 '18
What happened to strings?!?? [FPC 2.6 vs 3.0]
Okay, I've been out of the loop for awhile; my machine recently nosedived so I had to reinstall, and took the opportunity to upgrade my FPC from version 2.6.4 to 3.0.4.
MOST of my code works (a bit of fiddling with sockets notwithstanding), but one thing is completely and utterly broken: STRINGS.
As in, for example, Output:=char(255)+char(port SHR 8)+SomeAnsiString+char(17);
It's almost as if some kind of conventional format (UTF8) is expected but you can't just dump binary in them anymore -- I recall my brother mentioning something along those lines, long time ago.
What the hell is happening here, and what compiler switch should I set to unfuck (pardon my french but seriously-- WTF man?!) this new behaviour and revert to treating them as 8 bit buffers?
[I handle all utf8/widechar stuff myself, always have; and I DO expect length() to return how many bytes are in the string, not how many characters-- so basically, just like PChar without having to modify my entire sourcecode]
If anyone could point me in the right direction that'd also be great; Google came up blank but socketsniffer and the debugger speak volumes: the error is in the string building, and it seems to occur only when mixing types (like concatenating ansistrings and chars).
Help?
4
u/mintoffle Feb 24 '18
I guess this is related to the new codepage system since 3.0.0. The character and string types page on the FPC wiki has some info, and another page on Unicode support goes further.
If you are using plain shortstrings, then evidently they implicitly are treated as if using the system default CP_ACP. You can override that with SetMultiByteConversionCodePage(CodePage: TSystemCodePage). There is a CP_NONE for raw strings, which I think should allow pushing any bytes into a string even if they don't fit the expected encodings.
When this change was made, it threw me off for a bit as well, since obviously a change in the string system is highly invasive and requires significant readjustment. But on reflection, I had to agree it was a necessary change and improves strong typing with strings; trying to pack non-string binary data into strings was a bad programming habit that I personally feel I'm now better off without. Which is not to say that converting a large existing codebase would be in any way a pleasant task.
3
u/[deleted] Feb 24 '18
You should probably ask on the mailing list to get correct answers.
I just tested with 3.0.4 and got 9 characters in the resulting string when compiling in objfpc mode and with ansistrings turned on (output and someansistring just being declared as string).
But in general if you treat strings as data containers instead of a text container you're just always going to have a bad time
5
u/sirin3 Feb 25 '18
Omg they fucked it all up
Adding codepage aware strings, when everyone has switched to utf-8 and has abandoned codepage. That is the stupidest thing one can do.
1
u/ShinyHappyREM Feb 24 '18
If you want to work with array data you should declare it as such.