Prefix-separated list

From Woozle Writes Code
Jump to navigation Jump to search

About

A prefix-separated list is a way of encoding a short list of static values of varying length within a single string.

Benefits

It is easy to handle in code, as there are no special escape sequences to look for. It also stores the values very efficiently, requiring only an extra character (the separator) per list element. It also eliminates the hassle of having to separately specify the separator, since this is specified within the data.

Format

The rule is that the first character in the string is the character which separates the list elements. While this means that it is not possible to represent every possible character simultaneously within a list, in real-world usage it is usually trivially easy to find an unused character which is visually satisfying as a separator.

Examples

  • /one/two/three/four
  • .first.second.third.fourth.fifth
  • :Monday:Tuesday:Wednesday:Thursday:Friday:Saturday:Sunday

Notes

  • 2010-09-12 As far as I know, I'm the only one who uses this technique; I haven't seen it anywhere else. So maybe it's only useful to my weird brain.
  • 2022-05-01 This is mainly of use in languages that won't let you use a string array as a constant or default value.