PDA

View Full Version : Mutable string interface for complex parsing


Algorithm
12-31-2002, 01:31 AM
Update: Added .slice() method.

The CharList class implements a string as a doubly linked list, so that its constituent characters can be changed or moved about with ease, and without having to process the entire string. It's ideal for complex parsing, as well as for simple encryption (text scrambling and the like).

CharList is open-source freeware, so use it as you like. I welcome any comments, criticisms, or improvements you may have to offer.

A brief summary of the class's properties follows.
_________________________________________________

CharList(txt) -- Constructor. If something other than a string is passed to it, it will be converted to a string and processed.

.Node(chr, [next=null], [prev=null]) -- Subclass constructor. Holds a single character and pointers to neighbors.
.Node.chr -- The character held by the node.
.Node.next -- The following node.
.Node.prev -- The preceding node.
.Node.go(i) -- Finds the node with (i) distance from the current node. Example: node.go(-2) == node.prev.prev
.Node.toString() -- Returns the value of chr.

.head -- Beginning of the list.
.tail -- End of the list.

.append(txt, [node=tail]) -- Inserts the given text, node, or CharList into the list after the specified node.

.find(chr, [node=head]) -- Finds the first instance of chr in the list, starting with node and moving forward. Returns the node found.

.findLast(chr, [node=tail]) -- Finds the first instance of chr in the list, starting with node and moving backward. Returns the node found.

.indexOf(node) -- Returns the index of the given node, or -1 if it is not found.

.length() -- Returns the length of the list. This value is calculated dynamically, so it is advised not to employ it directly in loops.

.nodeAt(idx) -- Returns the node at the specified index, or null if no such node exists.

.prepend(txt, [node=head]) -- Inserts the given text, node, or CharList into the list before the specified node.

.remove(node) -- Removes the specified node from the list.

.slice(start, end) -- Returns a substring of the list, beginning at the given node or index and continuing until the end node or index is encountered.

.substr(start, length) -- Returns a substring of the list, beginning at the given node or index.

.toString() -- Rebuilds the list into a string form.
_________________________________________________

Enjoy!

jkd
12-31-2002, 02:27 AM
Just looking through the object description... where is something like changeNodeAt(int index, char newValue)
?

Writing this heavy duty string class is nice, but I don't see the use unless you can change it in place. I mean, you have remove() and prepend()... you could even write changeNodeAt just by using those two already-written methods. :)

Algorithm
12-31-2002, 05:55 AM
You can do that using just one line of code:

nodeAt(index).chr = newValue;

I figured it was simple enough not to merit its own function. :)

jkd
12-31-2002, 06:09 AM
Oh, oops. Figures I didn't entirely read through your documetation. :D

Bosko
12-31-2002, 12:28 PM
Sounds cool,you might want to release it under the GPL (http://www.gnu.org/licenses/gpl-faq.html) .

Algorithm
01-13-2003, 11:49 PM
I've updated the code to address concerns I've found when implementing the object. See first post.