Path: csiph.com!news.swapon.de!aioe.org!.POSTED!not-for-mail From: "Rod Pemberton" Newsgroups: comp.lang.forth Subject: Re: Questions about strings Date: Mon, 02 Nov 2015 02:34:51 -0500 Organization: Aioe.org NNTP Server Lines: 57 Message-ID: References: <281b9816-848c-4abd-b62e-02cb7d0181a3@googlegroups.com> NNTP-Posting-Host: n4wpt9zq8xR26Ttf9mo2BA.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Opera Mail/12.16 (Linux) X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.forth:43085 On Sat, 31 Oct 2015 13:48:36 -0400, Chris Curl wrote: > I have decided that in my own personal Forth, strings will be both > counted and NULL terminated. The count is in the first BYTE and does > NOT include the NULL terminator. > > So "ABC" would use 5 bytes and look like this: > > 3 65 66 67 0 > > This makes it easy to work with the strings using either approach. > And I find that some activities are more straightforward when > stopping at NULL as opposed to keeping a count and stopping when > that count exceeds the count byte. > > If it turns out that I have a need to deal with longer strings in > the future, then I will cross that bridge when I get to it. BTW, NULL is used by C for a pointer to non-C-object area, but NUL is a character of all bits zero for ASCII and EBCDIC, probably Unicode too. Or, NUL is a C-sized byte of all bits zero for C where C's byte may be larger than the character size in bits. Some issues you might run into is which one to use and what to do if the length of the nul terminated string exceeds the ability to store the length in the byte. I implemented null terminated strings in my Forth, as have some others here. I've not noticed any serious issues yet. IIRC, you only need to change one Forth word to work slightly differently than intended by the specifications, but isn't in violation of them. I had planned to insert a byte for the count since null terminated strings won't pass Hayes Core tests as they're currently implemented, but I have yet to do so as it will require reformatting my dictionary code. Having programmed heavily in both C and PL/1 (a PL/I variant), one which uses nul-terminated strings and the other counted strings, I'm of the strong opinion that nul terminated strings is the better solution. However, saying so seems to consistently incite some flames here. As Ms. Rather noted, some of Forth's string words are typically implemented as primitives, many of which almost map directly onto various C string functions. CMOVE strncpy() ENCLOSE strchr() or strpbrk() COMPARE strcmp() COUNT strlen() Rod Pemberton