• tunetardis
    link
    fedilink
    English
    arrow-up
    2
    ·
    15 days ago

    Better in what sense? I put some thought into this when designing an object serialization library modelled like a binary JSON.

    When it got to string-encoding, I had to decide whether to go null-terminated vs length + data? The former is very space-efficient, particularly when you have a huge number of short strings. And let’s face it, that’s a common enough scenario. But it’s nice to have the length beforehand when you are parsing the string out of a stream.

    What I did in the end was come up with a variable-length integer encoding that somewhat resembles what they do in UTF-8. It means for strings < 128 chrs, the length is a single byte. Longer than that and more bytes get used as necessary.

    • zarenki@lemmy.ml
      link
      fedilink
      arrow-up
      1
      ·
      15 days ago

      a variable-length integer encoding that somewhat resembles what they do in UTF-8. It means for strings < 128 chrs, the length is a single byte. Longer than that and more bytes get used as necessary.

      What you used might be similar to unsigned LEB128, which is used in DWARF, Webassembly, Android’s DEX format, and protobuf. Essentially encodes 7 bits of the number in each byte, with the high bit being 1 in any byte except the last one representing the number.

      Though unlike UTF-8 the number’s length isn’t encoded in the first byte but instead implied by the final byte. Arguably making the number’s encoding similar to a terminated string.

    • LalSalaamComrade@lemmy.mlOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      15 days ago

      What about data structures like gap buffer or piece table? Would they be ideal for something like, say, a TUI-interface application?

      • tunetardis
        link
        fedilink
        English
        arrow-up
        1
        ·
        15 days ago

        Oh, so you’re talking about text representation in an editor or something along those lines? That’s kind of a separate problem isn’t it?

        At the lowest level though, I suppose you still need to consider whether to use null-terminated segments. I think I’d still be going length + data, though I wouldn’t worry about packing down the length representation like with serialization formats. Your code will need to be highly cognizant of the length of strings and managing dynamic memory allocation all over the place, so it’s good to have those lengths quickly accessible at all times.