Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Counted strings Date: Sat, 03 Dec 2016 18:57:27 -0800 Organization: None to speak of Lines: 27 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="c67b6e747431fd30be3f4bcc86507f6a"; logging-data="16946"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18t4gBxt5Z3r4zZTd90ZQyV" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:8ihgYZllVaW6g8qVrBf4HKEky84= sha1:iZwJSYm2LkiDbFZjwPhNXAqYmIw= Xref: csiph.com comp.lang.c:97970 jacobnavia writes: > Instead of prefixing a string with its count, we could do a variable > length encoding, like in utf8. > > Strings of length 0 to 254 would be prefixed by a byte containing the > length. If the value of that byte is 255 (0xff, all ones) it means that > the length is encoded in the next two bytes. > > Those two bytes encode lengths from 255 to 65535-2, --> 65533. The > length is given by > > l == index - 1, > > i.e. if we read 576 from those two bytes, the real length of the > following data bytes is 575. [...] Another possibility is to start the array with a single UTF-8 encoded code point representing the length in bytes. With standard UTF-8 encoding this gives you lengths up to 2**21-1 in 4 bytes. Extrapolating the scheme gives you up to 2**42-1 encoded in 8 bytes. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"