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: Mon, 05 Dec 2016 09:41:33 -0800 Organization: None to speak of Lines: 33 Message-ID: References: <97ca2dbf-f636-423d-93f8-61204901a182@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="f21d38049ad0d524c578eed3c5fbedbb"; logging-data="29249"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/WvJOTfQeOtbvGug7lEJSg" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:I2Zf8JBwt75ms3LUQxWlDkvT+II= sha1:PmSVfHwnPXDJ7wh1oDwkbkjOwr4= Xref: csiph.com comp.lang.c:98028 Malcolm McLean writes: > On Saturday, December 3, 2016 at 10:58:20 PM UTC, jacobnavia wrote: >> What are your ideas about counted strings? >> >> How could it be done? >> > It's a very standard data compression problem. > > You need to gather statistics, but also consider ease of implementation > and obviousness. > I think 8 clear bits need to be the empty string. If the high bit of > the first byte is unset, it's asciiz (backwards compatibility). > That then gives us 7 bits to play with for encodings. We can store > UTF8 / ascii, 8 bit, 16 bit, 32 bit, 64 bit counts, and we've still > 4 bits left. So nul/non-nul terminated. Now it's three bits. So for strings of length 127 or less, you still have to scan for the terminating '\0' to determine the length. That could be a performance problem (and no improvement over standard C strings) if you have a lot of relatively short strings. In addition to improving the speed of length calculations, another advantage of counted strings is that they allow the possibility of having null bytes within a string. Your scheme loses that. I suppose there might be some contexts where your scheme would make sense. -- 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"