Shop OBEX P1 Docs P2 Docs Learn Events
String Library and other goodies!!! — Parallax Forums

String Library and other goodies!!!

KyeKye Posts: 2,200
edited 2009-03-07 03:14 in Propeller 1
Hey guys,

I've been working on building a standard library object that contains usefull functions for any application. So far I've manage to·only·really find that timing and string manipulation were absent from spin so I wrote a library that includes everything you would need from those two categories.

So, in the library I have:

32-bit number to decimal string conversion
32-bit number to hexidecimal string conversion
32-bit number to binary string conversion

decimal string conversion to 32-bit binary number
hexidecimal string conversion to 32-bit binary number
binary·string conversion to 32-bit binary number

comparing if one string is alphabetically before another
comparing if one string is alphabetically after another

converting all lowercase characters in a string to uppercase
converting all uppercase characters in a string to lowercase

finding a character in a string
finding a substring in a string

pausing execution by seconds
pausing execution by milliseconds

Now, I'm not sure what elese would be useful, if anyone has any suggestions please speak up.

Thanks,

EDIT:
Adding scharacter replacement.
Adding substring replacement (Only from larger to smaller, no growing strings. Because memory is not dynamic.)



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,

Post Edited (Kye) : 3/4/2009 12:57:40 AM GMT

Comments

  • SRLMSRLM Posts: 5,045
    edited 2009-03-03 05:27
    I was thinking a heap manager (free store) would be useful, along with vectors (dynamic array), stacks, queues, linked lists, trees, etc. From there, sorting algorithms would be the next step. The heap manager is on my list, along with stacks and linked lists, but if you want to write them go ahead!

    Also, spin based math functions (sine, cosine, arctangent, arcsine, summations, nth root, etc) would be good.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-03-03 06:51
    There is a Heap object in the obex:
    http://obex.parallax.com/objects/241/

    I'm sure a linked list object is also there.

    regards peter
  • jazzedjazzed Posts: 11,803
    edited 2009-03-03 07:01
    @Kye, you should consider writing some of the string processing methods like are found in Java. As you may know, Java has indexing, substitution, split, strcmp, yadayadayada, and other methods callable as: stringobject.indexOf("hi"), stringobject.substring(0, 10), and so on.

    There are many examples of what a good string object will do ... and I know of at least one customer for such a package. The problem is in having a bunch of methods that are not used. One could create multiple string objects that have increasing levels of obscurity.

    I don't know if there is much value in having an object encapsulate a single string, but there would be much value in having an object that defines the methods to be used in the form string.indexOf(haystack, needle) for example.

    Peter Verkaik has atoi and itoa etc ... functions in his Format object. He also wrote a limited heap manager.

    I wrote a malloc object that does free as well as malloc, but I'm not to comfy with it. I have stack, queue, and slingly linked list objects based on my malloc, but I'm not interested in publishing yet.

    One thing I have that might be worth publishing that I use all the time is a printf demo that uses parm(val)+parm(val)... to achieve var args.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve
  • SRLMSRLM Posts: 5,045
    edited 2009-03-03 07:39
    @Peter

    Ah, so there is. It's new since the last time I looked. However, I was thinking more of a 'heap manager' (perhaps 'free store manager' would be a better title) that uses all the available memory after the program. In other words, you create an instance of the program (withing OBJ) and, at runtime it determines what the amount of unused RAM is, and allows you to allocate and deallocate from there. Anyway, it shouldn't be that difficult to modify the code to do that (add a start method in place of create). Looks pretty good.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2009-03-03 12:47
    Great work. Strings open up all sorts of useful options - you can interface with a program in plain English. I'm working with an ancient language at the moment called Sbasic, and the reason I like this is you can write your own functions and procedures that then turn into new commands within the language that look and behave like existing inbuilt commands. Not many languages can do that. String manipulation at present is a hybrid of the original commands in the language (Basic type commands) and ones that have been added over the years within C and vb.net and Java. The ones that I use over and over are:

    Right(string, number)
    Left(string,number)
    Mid(string,startnumber,number) ' return a string number long starting at startnumber in string
    Mid(string,startnumber) ' a special case that goes to the end of the string
    Ltrim(string) 'remove a leading blank
    Rtrim(string) ' remove trailing blank
    Trim(string)' remove leading and trailing blanks
    Instr(string,stringtofind,number) ' find stringtofind in string starting at number
    Str(number)' string value of number and adds a - at the beginning if negative or a blank if positive (see trim)
    Val(string)
    Hex(number) ' number to hex string
    Val("&H"+string) ' hex string to number (maybe use 0x to be more modern)
    Space(number)
    ucase(string)
    lcase(string)

    and arrays
    dim mystring(30) as string (or in sbasic var mystring(30)=string)

    And five file functions that have been around since Mbasic and which still exist in vb.net and which I have coded from scratch in sbasic (using the above string functions, some of which also have been coded from scratch):
    fileopen(2,"MYFILE.TXT","OpenMode.Output")
    fileopen(2,"MYFILE.TXT","OpenMode.Input")
    mystring=LineInput(2)
    printline(2,mystring)
    fileclose(2)

    I'm not a propeller expert and I'm sure some of these already exist and I think you might have written some just now. I hope I haven't given you too much homework *grin*.

    Post Edited (Dr_Acula (James Moxham)) : 3/3/2009 1:01:58 PM GMT
  • KyeKye Posts: 2,200
    edited 2009-03-03 14:13
    Well, I really want to use dynamic memory, but how do I do that on the propeller chip intellegently?

    Since the memory size is limited is their really an easy way to do this?

    I've seen the heap objects, but I've always wondered about the cost of using them and how much memory must be moved arround and shifted here and there.

    Is, their anyway to have dynamic memory, without dynamic memory?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • DroneDrone Posts: 433
    edited 2009-03-03 14:13
    Below but with 64 bits to accompany the double-precision floating point in the math section of the Propeller Wiki. Better yet put these in a wrapper that's sorely needed for the Propeller Wiki double-precision code!

    32-bit number to decimal string conversion
    32-bit number to hexidecimal string conversion
    32-bit number to binary string conversion

    decimal string conversion to 32-bit binary number
    hexidecimal string conversion to 32-bit binary number
    binary string conversion to 32-bit binary number

    David
  • KyeKye Posts: 2,200
    edited 2009-03-03 14:18
    That should be left for a floating point library.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-03-03 15:11
    @SRLM,

    If you know where your program ends, my heap's Create method takes a base address and size
    and creates a heap in the area specified by base and size. This area could be the memory
    after your program up to $7FFF. Just call Create at the start of your program. You can even
    create multiple heaps or heaps within heaps.

    regards peter
  • jazzedjazzed Posts: 11,803
    edited 2009-03-03 15:29
    @Key
    Memory managers don't have to be as bloated or fast as GNU malloc. If someone needs dynamic data, there is not much choice in having a heap manager unless a file system is available.

    @Peter
    When are you going to add "free" to your heap object?

    @Dr_Acula (fitting picture[noparse]:)[/noparse] ... That's a pretty complete list of VB6 functions. Would be nice to have C, Java, and VB like objects.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-03-03 15:45
    @Jazzed,
    Free is already in the object. It is called free.

    regards peter
  • DroneDrone Posts: 433
    edited 2009-03-03 16:39
    The double-precision floating point library already exists, here...

    propeller.wikispaces.com/MATH

    But it needs a spin wrapper and your to/from string routines would be good addition to it if it could handle more bits. (I believe the floating point library already has pack and unpack routines.)

    Regards, David
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-03 17:13
    Awhile back I wrote a dynamic strings object, which is included in this thread, along with a heap manager object and a C-like formatted output object. Maybe it will give you some ideas. I suppose I should put it all in the ObEx, since this topic comes up repeatedly.

    -Phil
  • RiJoRiRiJoRi Posts: 157
    edited 2009-03-03 18:23
    String concatenation -- either C$ = A$ + B$ or C$ = strcat(A$,B$) is also helpful.

    --Rich
  • jazzedjazzed Posts: 11,803
    edited 2009-03-03 19:35
    @Peter forgive my misreading and misunderstanding your code.

    @Phil I remember seeing that thread. Your string object features are nice. It is possible to do basic string functions without a memory manager though concatenation, etc would be very clumsy. Have you considered writing a subset without memory management? Were you a student of Knuth or a professor using the "Art" ?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-03 19:59
    jazzed said...
    Were you a student of Knuth or a professor using the "Art" ?
    A student using Knuth's The Art of Computer Programming, yes.

    I haven't given much thought to recasting the strings object to use static arrays. 'Too many irons in the fire right now to consider it, I'm afraid.

    -Phil
  • KyeKye Posts: 2,200
    edited 2009-03-03 20:07
    Okay, thanks for the input guys, I see then that its nearly impossible to do any more of the stuff I already have (expect searching, and tokenization) without dynamic memory.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • russ christensenruss christensen Posts: 84
    edited 2009-03-03 21:27
    i was posting the other day about trying to get a substring out of a string. that would be really useful. also string concatenation would be really handy as well. just my 2c. you'd be my hero!
  • KyeKye Posts: 2,200
    edited 2009-03-04 00:52
    Okay, well I can add those features, but you would need some memory for it to work with... which would be a hardcoded array.

    However, I'll add substring searching, character searching, string replacement, and character replacement. Note that the replacement will allow tokenization and will permanetly alter the string, and you will only be able to shrink the string not grow it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • russ christensenruss christensen Posts: 84
    edited 2009-03-04 01:17
    In the stuff i'm doing right now, i have plenty of memory left over. and like 6 cogs left too. lol. i appreciate your hard work.
  • JanCardellJanCardell Posts: 10
    edited 2009-03-04 13:32
    Yes...
    comparing if one string is the same as another

    ·if "stringname" == "srtingname"

    Jan Cardell
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-04 19:13
    Jan, take a look at the Spin function STRCOMP.

    -Phil
  • JanCardellJanCardell Posts: 10
    edited 2009-03-05 00:07
    Yes...·Sorry....
    Jan
  • KyeKye Posts: 2,200
    edited 2009-03-05 00:46
    Okay, are these okay for the functions?
    PUB findCharacter(charactersToSearch, characterToFind) | counter, length
    '' ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
    '' │ Searches a string of characters for the specified character.                                                              │
    '' │ Returns the address of that character if found and zero if not found.                                                     │
    '' └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
     
    PUB findCharacters(charactersToSearch, charactersToFind) | counter, length 
    '' ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
    '' │ Searches a string of characters for the specified string of characters.                                                   │
    '' │ Returns the address of that string of characters if found and zero if not found.                                          │
    '' └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
     
    PUB replaceCharacter(charactersToSearch, characterToReplace, characterToReplaceWith)
    '' ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
    '' │ Replaces the specified character in a string of characters with another character.                                        │
    '' │ Returns the address of the replaced character plus one on sucess and zero on failure.                                     │
    '' └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
     
    PUB replaceCharacters(charactersToSearch, charactersToReplace, characterToReplaceWith)
    '' ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
    '' │ Replaces the specified string of characters in a string of characters with another string of characters.                  │
    '' │ Returns the address of the replaced string of characters plus one on sucess and zero on failure.                          │
    '' └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
    

    I'm not sure if I should make them have a local memory. It would make them easier to use, but far less flexible.

    Thanks, for feed back.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • jazzedjazzed Posts: 11,803
    edited 2009-03-05 01:34
    Seems like rather long names ... perhaps these? findChar, findString, replaceChar, replaceString If it was me i would almost never use findChar or replaceChar ... string versions would be necessary though so find and replace should be sufficient for most apps ... anyone could easily build a findChar wrapper using the find method. Economy is very important in big spin programs. There are tools that can help if one is willing to use them.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve
  • KyeKye Posts: 2,200
    edited 2009-03-05 02:30
    Oh, mine are very slim, but as for naming i'm not developing my functions for you old timers =). I'm making my functions for people just starting out, so I' trying to be very descriptive.

    And, I was asking more about what they returned. Notice how replace the functions return the next address of the string past themselves or the address of next character in the string if not null. This can be usefull for tokenization.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • KyeKye Posts: 2,200
    edited 2009-03-07 03:14
    Library posted, check the post, optimized string library!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
Sign In or Register to comment.