1

When converting a C string into a Pascal string, why should the length of the original string be less or equal to 127 instead of 256? I understand that an unsigned int ranges from 0~256 and a signed one ranges from -128~127, but isn't the first character of a Pascal string unsigned?

8
  • 4
    whos says 127 or less? Wikipedia says 255 en.wikipedia.org/wiki/String_(computer_science)#Length-prefixed Commented Jan 31, 2018 at 20:50
  • 1
    The question seems lacking in basis. On what grounds do you assert that the original string length should not exceed 127 characters? Does Pascal really place such a limitation, or is that tied to a particular implementation? Or is it even a real requirement at all? Commented Jan 31, 2018 at 20:51
  • 3
    Common early implementations of Pascal on PCs stored strings preceded by a single-byte length (which was unsigned, so strings could have lengths from 0 to 255). This is not a limitation of the language itself. Commented Jan 31, 2018 at 21:00
  • 2
    "Pascal string" is not standardised and everyone talking about Pascal strings talks about something slightly different. My Pascal strings have 4 byte sizes. Commented Feb 1, 2018 at 8:31
  • 2
    Where did you get that number 127? I don't know of any implementations that have that restriction, unless you means so called counted strings, but these have a fixed, explicitly declared size, e.g. var x: string[31], and that size can be anything from 1 to 255. Commented Feb 1, 2018 at 10:42

2 Answers 2

2

The Pascal string you are referring to is probably the one used in older Pascals (called ShortString in e.g. Delphi and FreePascal, the most popular Pascal implementations these days). That can contain 255 single-byte characters (char in C). There is no need to restrict this to 127 characters.

Perhaps you were thinking of the fact that 255 bytes can only contain 127 UTF-16 code points. But these strings were popular in the old CP/M and DOS days, when no one knew anything about Unicode yet, and were made to contain ASCII or "Extended ASCII" (8 bit, using code pages).

But most modern Pascal implementations allow you to use strings up to 2 GB in size. There, the length indicator is not stored as the first element anymore, just close to the text data. And these days, most of these strings can contain Unicode too, either as UTF-16 or as UTF-8, depending on the string type you choose (modern Pascal implementations have several different string types for different purposes, so there is not one single "Pascal string type" anymore).

Some languages do have the ability to restrict the size of a ShortString, as so called "counted" strings:

var
  s: string[18];

That string has a maximum of 18 bytes text data and 1 byte length data (at index 0). Such shorter strings can be used in, say, records, so they don't grow too big.

Sign up to request clarification or add additional context in comments.

Comments

1

FreePascal's wiki has a great page showing all the types of strings that Pascal (at least that implementation) supports: http://wiki.freepascal.org/Character_and_string_types - it includes length-prefixed and null-terminated string types. None of the types on that page have a length restriction of 127.

The string type you're referring to would match ShortString which has a single byte prefix, however their documentation states it accepts 0-255.

I am aware of a string-type that has a variable-length-integer prefix, which would restrict the length of the string to 127 characters if you want the in-memory representation to be binary-compatible with ShortString, as being 128 characters or longer would set the MSB bit to 1 which in variable-length-integers means the integer is at least 2 bytes long instead of 1 byte.

1 Comment

which Pascal do you know that has variable length size "prefixes" (most modern Pascals don't use prefixes anymore)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.