Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    USA
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Mutable string interface for complex parsing (Updated 1-13-03)

    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!
    Attached Files Attached Files
    Last edited by Algorithm; 01-13-2003 at 11:45 PM.

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    USA
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Oh, oops. Figures I didn't entirely read through your documetation.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Netherlands
    Posts
    217
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sounds cool,you might want to release it under the GPL .

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    USA
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts

    New v1.2

    I've updated the code to address concerns I've found when implementing the object. See first post.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •