I wrote an implementation of several of the VB string functions in C++ yesterday, if anyone is interested. The ones I have done are:
InStr
Mid
Replace
TrimL
TrimR
Trim
I also have functions to remove all but one space from a series of spaces, and one to remove tabs from a string:
Code:
If the input is "Hello Zaei", the output would be "Hello Zaei", and, if the input is "Hello\tZaei", the output would be "Hello Zaei"
I am also working on a split function. If anyone is interested, I will post the code (its on another computer, and I have to find a disk). Just let me know.
I'm happy to have a look and debate their efficiency etc.. If they're not for C style strings, then don't bother, anything else is inefficient
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Efficiency and resource-management are difficult to get a good compromise with.
As it goes, using the standard string class is just as good, if not better, than doing it yourself using C-style strings since it inlines better and has all the extra allocation.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You". -- Linus Torvalds
Originally posted by parksie Efficiency and resource-management are difficult to get a good compromise with.
As it goes, using the standard string class is just as good, if not better, than doing it yourself using C-style strings since it inlines better and has all the extra allocation.
Not at all, C style strings doesn't nessesary mean that you use the c style string functions. the string class is dynamic and that already is a big big hog
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
I was going to do split using a char**, instead of a vector, as well as allowing a char* to be used as the delimiter. Ill get the code in a few minutes. I didnt try much to optimize these, but I did take care to use things like strlen() once per string, etc.
1. New without Delete:
Allocating memory in one function and deallocating it in another is pretty dangerous thing to do, a user of your api's could assume that dynamical objects will allocate and deallocate automatically, and your code would be a permanent source of memory leaks. Thumb rule with dynamical memory allocation is that allocators deallocate (newer's deletes), another way at looking at it is that a specific function or object own's it's dynamic data and is responsible of proper deallocation.
Just some OOP etiquette.
2. those of your functions that returns a allocated pointer are restricted to output on the heap, although you'd have to face the complications of static size of buffers, allowing buffers on the stack is quite a big deal to a lot of programmers so your code will be a lot less usefull, if they wanted dynamical strings they would go with strings class anyways.
3. instr, iterate another copy of find syncronous to p, and reset it to find if not equal, otherways if !*p then return. Also keep the othermost loop clean with only one if.
I haven't looked at replace and the others yet, but will do tomorrow, too tired now.
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
1. Yep, thats really the worst thing with this code, but, since these functions are for my own use, I dont care much =).
2. Same as above =).
3. So, saves me an strlen()?
In the trim function, there should be a buffer copy, and delete *r between the trimr and triml calls. I think it will leak data otherwise (just noticed this one, not sure though (its late =)).
Did you say you had made a C++ equivalen of the VB Mid$??
If you have, is it possible to either post it, or send it to my email address ([email protected])
it's abovemost in his code, but if you ask me, don't use it unless you are a C programmer (or "just know" what you're doing like Zaei claims to do =))
I'd recommend the string class if you're just out after an easy interface
Use
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.