String Library and other goodies!!!
Kye
Posts: 2,200
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
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
Also, spin based math functions (sine, cosine, arctangent, arcsine, summations, nth root, etc) would be good.
http://obex.parallax.com/objects/241/
I'm sure a linked list object is also there.
regards peter
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
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.
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
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,
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
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
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
Free is already in the object. It is called free.
regards peter
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
--Rich
@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
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
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,
comparing if one string is the same as another
·if "stringname" == "srtingname"
Jan Cardell
-Phil
Jan
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,
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
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,
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,