Peter N Lewis wrote:
In the Mac OS, we have a concept of a FourCharCode, which is essentially a UInt32 except that it often has values that are made up of four characters. In Mac OS compilers then, the UNSIGNEDLONG (Cardinal attribute( size = 32 ))is compatible as a special case with constant strings of four characters.
[...]
Changing FourCharCode = UInt32 is probably better since the concept behind it is as a fixed size low cost use, and it is essentially never used as a string as such.
I agree. It's probably the only way it can be used in `case' as well.
The problem then comes from the fact that you can't case the constants above to UInt32's, eg:
typeBoolean = UInt32('bool');
this doesn't work.
One possibility might be to extend BP's # character hack to work with quoted strings in reverse, so #'bool' would return an unsigned integer constant equal to ((((ord('b)*256)_+ord('o'))*256+ord('o'))*256+ord('l').
[...]
I'm certainly open to hear of any existing GPC solutions that we could use instead.
I thought of a macro encapsulating the code above.
{$define UInt32(s) ((((Ord (s[1]) * 256) + Ord (s[2])) * 256 + Ord (s[3])) * 256 + Ord (s[4]))}
Then you can do:
const c = 'abcd';
WriteLn (UInt32 (c));
Unfortunately, this doesn't work:
WriteLn (UInt32 ('abcd'));
because after macro expansion it will contain things like 'abcd'[1] which are syntactically invalid (in any Pascal dialect AFAIK).
I'm not sure yet how difficult or desirable it would be to allow this syntax. But perhaps someone has an idea for a less intrusive way ...
Frank