Computer Science Canada

String class

Author:  Clayton [ Mon Dec 11, 2006 8:51 pm ]
Post subject:  String class

got bored, so I made a string class, heres the source:

Turing:

class String
    import Str
    export insert, content, remove, strip, size, make_upper, make_lower, gets, make_original, as_lower, as_upper, as_stripped, originally

    var value : string := ""
    var original : string := ""

    fcn content : string
        result value
    end content

    procedure insert (letter : string (1), position : int)
        var oldword := value
        value := oldword (1 .. position - 1) + letter + oldword (position .. *)
    end insert

    procedure remove (position : int)
        if original = "" then
            original := value
        end if
        var oldword := value
        if position = length (value) then
            value := oldword (1 .. * -1)
        elsif position = 1 then
            value := oldword (2 .. *)
        else
            value := oldword (1 .. position - 1) + oldword (position + 1 .. *)
        end if
    end remove

    %removes all whitespace from a word
    fcn as_stripped : string
        if original = "" then
            original := value
        end if
        var oldword := value
        var newword := ""
        if oldword ~= "" then
            for i : 1 .. length (oldword)
                if oldword (i) ~= " " then
                    newword += oldword (i)
                end if
            end for
        end if
        result newword
    end as_stripped

    procedure strip
        value := as_stripped
    end strip

    fcn size : int
        result length (value)
    end size

    fcn as_upper : string
        result Str.Upper (value)
    end as_upper

    procedure make_upper
        if original = "" then
            original := value
        end if
        value := Str.Upper (value)
    end make_upper

    fcn as_lower : string
        result Str.Lower (value)
    end as_lower

    procedure make_lower
        if original = "" then
            original := value
        end if
        value := Str.Lower (value)
    end make_lower

    fcn is_empty : boolean
        if value = "" then
            result true
        else
            result false
        end if
    end is_empty

    fcn originally : string
        result original
    end originally

    procedure make_original
        value := originally
    end make_original

    procedure gets
        get value : *
    end gets
end String

var my_string : ^String

new String, my_string

put "Please enter anything " ..
my_string -> gets

put "All lowercase"
put my_string -> as_lower

put "All uppercase"
put my_string -> as_upper

put "Stripped of whitespace"
put my_string -> as_stripped

put "String size"
put my_string -> size

put "Inserted \'a\' at position 1"
my_string -> insert ("a", 1)
put my_string -> content

put "Removing \'a\' at position 1"
my_string -> remove (1)
put my_string -> content

put "Original"
put my_string -> originally


its really quite readable (it almost reads like english Very Happy) so you can probably tell what most of these do. please post suggestions, improvements, constructive critisism etc.

Author:  Hackmaster [ Tue Dec 12, 2006 9:32 am ]
Post subject: 

Good Stuff, Freakman...but...

can't you do most of this stuff without this class? you can find the length of a word anyways, and I believe that you can to stuff like toupper and tolower work to0 (but that just might be me thinking in C). but the strip function is cool, and repalcing single letters and such.... that's getting pretty sweet. Hey, you are on your way to making FreeWord!

keep it up!

Author:  Clayton [ Tue Dec 12, 2006 1:41 pm ]
Post subject: 

sure you can do those things without the class, but the whole point is to make the string an object. As an object, there is so much more freedom and flexibility.

Author:  ericfourfour [ Tue Dec 12, 2006 3:27 pm ]
Post subject: 

If you want to make something very helpful, not that this isn't helpful, make a BigString class. It would remove the limit from the size of strings.

Author:  [Gandalf] [ Tue Dec 12, 2006 7:56 pm ]
Post subject: 

Good idea, ericfourfour.

Freakman, not bad, though next time I suggest you do something like learning a new language in your bored time. Surprised

Also, the benefits of a String class are greatly diminished in Turing thanks to it's largely awkward OOP syntax.

Author:  Clayton [ Tue Dec 12, 2006 9:45 pm ]
Post subject: 

Yes I know, but seeing as we're going to be forced of another year of Turing (doing OOP woohoo!), I figured I might as well try and do something to prepare for it....

I guess I could do something like make a module out of it (ie, like they do with most of the other modules, take a class and manipulate it with a module), but then things could get confusing...

Author:  wtd [ Thu Dec 14, 2006 4:12 am ]
Post subject: 

You may wish to have separate get and get_line methods.

Also, may I suggest throwing in something a bit functional and having filtering methods which can taken a predicate function as an argument?

Author:  Clayton [ Thu Dec 14, 2006 12:21 pm ]
Post subject: 

and what might these certain filtering methods do? things like searching through a string for whatever, or something less obvious that I'm missing?

EDIT: I'm working on it right now, and now I really, really appreciate dynamic typing, forcing someone to pass a function with the correct parameters is annoying...

Author:  wtd [ Thu Dec 14, 2006 12:52 pm ]
Post subject: 

Imagine you wanted to remove all punctuation from a string. So you write a method to do that.

Now you want a method that removes all whitespace. So you write a method to do that.

Except that those methods would be identical, save for one thing... the check to see if a character should be removed. So that can be factored out into separate functions, and those functions passed to a single, generic method.

Author:  Clayton [ Thu Dec 14, 2006 12:54 pm ]
Post subject: 

not quite sure what you want me to do here. You're saying I should have a generic method that does all of the work, with other functions to be passed to it to specify exaclty what is needed?

Author:  rdrake [ Thu Dec 14, 2006 12:56 pm ]
Post subject: 

Freakman wrote:
and what might these certain filtering methods do? things like searching through a string for whatever, or something less obvious that I'm missing?

EDIT: I'm working on it right now, and now I really, really appreciate dynamic typing, forcing someone to pass a function with the correct parameters is annoying...
As wtd said below me, there can be slight differences between methods. This in Ruby is usually represented by adding either "!" or "?". For example "chomp" and "chomp!". There are only slight differences between them (one modifies the original value, one does not... but both return the modified value regardless), but they are still slightly different.

Author:  wtd [ Thu Dec 14, 2006 12:56 pm ]
Post subject: 

Yes. All of the code that handles iterating through the string, and creating a new string... it's the same for both methods. The only thing that changes is how you check each character. So that gets factored out into separate functions.

Author:  Clayton [ Thu Dec 14, 2006 12:59 pm ]
Post subject: 

What exactly do you mean by "How you check each character"? Does that mean I should have a function that tells the generic function what to remove from a string? If so, would this function be passed to the function from outside of the class, or internally?

Author:  wtd [ Thu Dec 14, 2006 1:01 pm ]
Post subject: 

Well, to check for punctuation:

code:
function is_punctuation (ch : string) : boolean
   result (ch = "." or ch = "," or ch = "!" or ch = "?" or ch = ";")
end is_punctuation

Author:  Clayton [ Thu Dec 14, 2006 1:05 pm ]
Post subject: 

okay, I see where you're going now, so just have said generic method take in that function to check for punctuation, if its punctuation remove it? Or am I still missing something?

Author:  wtd [ Thu Dec 14, 2006 1:17 pm ]
Post subject: 

Imagine:

code:


    %removes all whitespace from a word
    fcn as_string_without (predicate : function f (ch : string) : boolean) : string
        if original = "" then
            original := value
        end if
        var oldword := value
        var newword := ""
        if oldword ~= "" then
            for i : 1 .. length (oldword)
                if not predicate (oldword (i)) then
                    newword += oldword (i)
                end if
            end for
        end if
        result newword
    end as_stripped


Then to strip all punctuation:

code:
new_str := my_str -> as_string_without (is_punctuation)

Author:  wtd [ Thu Dec 14, 2006 1:19 pm ]
Post subject: 

Oh, and ericfourfour, when your string class has a method that returns a string, you should probably have it return a String object. Smile

Author:  Clayton [ Thu Dec 14, 2006 1:22 pm ]
Post subject: 

I get it Very Happy it makes so much more sense now. This way you can just pass the function and away you go Razz

Author:  ericfourfour [ Thu Dec 14, 2006 4:51 pm ]
Post subject: 

wtd wrote:
Oh, and ericfourfour, when your string class has a method that returns a string, you should probably have it return a String object. Smile

What? Did I miss something?

Author:  wtd [ Thu Dec 14, 2006 11:45 pm ]
Post subject: 

Well, consider:

code:
    %removes all whitespace from a word
    fcn as_stripped : string
        if original = "" then
            original := value
        end if
        var oldword := value
        var newword := ""
        if oldword ~= "" then
            for i : 1 .. length (oldword)
                if oldword (i) ~= " " then
                    newword += oldword (i)
                end if
            end for
        end if
        result newword
    end as_stripped


It would seem consistent to have this method, as a single example, return a String object, rather than a value of type "string".


: