What are Pascal Strings?

Technology CommunityCategory: Data StructuresWhat are Pascal Strings?
VietMX Staff asked 3 years ago

Pascal strings were made popular by one specific, but huge influential Pascal implementation, named UCSD. So UCSD Strings is a better term. In general it is not one specific type, but the basic principle of having the size prefixed to the character data. This makes getting the length a constant time operation O(1) instead of scanning the character data for a nul character.

Pascal strings excel in… everything. NUL terminated strings save one to three bytes on short strings, which may have been useful in 1970 but isn’t even worth mentioning today in virtually all circumstances. Aside from not being able to store a zero byte (which isn’t too bad for text but rules out any kind of binary data), you can’t determine string length efficiently. This affects a a good portion of string algorithms negatively. One example, in the comment you link to, is string comparison: If you have the length, you can instantly return false when comparing strings of different length.