Word of the day!

43 views
Skip to first unread message

Julian Skidmore

unread,
Jun 13, 2014, 4:31:47 AM6/13/14
to FIGnition
Hi folks,

I thought I'd start a FIGnition google group series on particular FIGnition commands. Every day, there will be a "Word of the day". It'll be pretty random as I'll just pick commands that take my interest, though sometimes they'll be simple commands.

Today's word is

str1 str2 "<>

it's like strcmp in c or str2.compareTo(str1) in Java, but better :-)

"<> compares two zero-terminated strings, returning 0 if they match; 0< if str1<str2 (earlier in ascii alphabetical order) or >0 if str1>str2. Thus if we have:

create biff " Word up!"
create baff " Word of the day!"
create beff " Word"

biff biff "<> ( would return 0 as they are the same string)
biff baff "<> ( would return a positive value as "Word up!" comes later alphabetically)
baff biff "<> ( would return a negative value because I've swapped the inputs to "<> )

However, "<> can also be used to perform a str2.startsWith(str1), because the lower byte of the result is 0 if str1 matches str2 up to the end of str1. Thus:

beff baff "<> 255 and ( returns 0. )
biff baff "<> 255 and ( returns 255 )

Really handy for comparing substrings :-)

-cheers from Julz

--
                             
                  The DIY 8-bit computer from nichemachines™

NmLogoMini.jpg
FIG - black on whiteMini.jpg

Mark Wills

unread,
Jun 13, 2014, 5:18:07 AM6/13/14
to fign...@googlegroups.com

That's a really nice word.

If you're interested in doing a little porting work (or someone else is) I have a very extensive string stack library here:

http://turboforth.net/string_library.html

It's in Forth-83 dialect.

Strings live on a dedicated string stack. You can DUP them,  SWAP them,  reverse them,  change case,  search them etc.

Mark

--
You received this message because you are subscribed to the Google Groups "FIGnition" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fignition+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Julian Skidmore

unread,
Jun 17, 2014, 2:09:02 PM6/17/14
to FIGnition
So, today's words are ". and the humble Quote command:

" someTextEndingIn"

Quote works in immediate mode to provide constant strings such as:

create msg " Quoted String"

Here, the string is just added to the end of the program. Typing:

msg

Returns the address of the string.

But Quote also works in compiled mode to provide constant inline strings that return an address. For example:

: dummyMsg " FIGnition Strings!" ;

Puts on the top of the stack the address of that string " FIGnition Strings!"

The handy thing about Quote is that it can be used with ". or other string functions in FIGnition Forth to do useful operations, such as:

: appendExclaim ( str --) " !" swap "+ ;

Will append an exclamation mark to a string.

FIGnition Forth exclusively uses zero-terminated strings. Thus:

find dup lfa>nfa

Will provide the address of the name string for the dup command. As with other strings you could print it out with ". or copy it to a string buffer with "! or append it to a string buffer with "+ .

The natural command to go with " is ". which displays zero-terminated strings (type is also available for a little conventional Forth string support). In fact in FIGnition Forth ." some string" really compiles a zero-terminated string and upon execution uses the underlying (".) to display it.

Why does FIGnition Forth support strings in this way? Because it means I can provide a core set of string functions in under 256b of compiled Forth (including the headers). Secondly, it means that I can sort-of leverage 'C' style string handling, which is a common, though fairly crude, way to think about strings.

It's possible you might not like FIGnition Forth's strings! If so, why not take up Mark Willis's invitation to convert his Turbo Forth string library to FIGnition :-) ?

-cheers from Julz
FIG - black on whiteMini.jpg
NmLogoMini.jpg
Reply all
Reply to author
Forward
0 new messages