Forgetting MutableString

I keep forgetting to wrap up CLR strings in MutableStrings when
returning
strings retrieved from calls to .NET Framework library methods.

I had this RSpec that had something like:

some_array[3].should == “some mutable string”

that was failing with the error:

Expected “some mutable string” to equal “some mutable string”:

It would be great if there were some way of picking this up, possibly at
compile time (possibly with SpecSharp??) or may be more simply changing
the
way a CLR string is printed in the console so that it is different from
a
MutableString.

For instance how about a CLR string being printed out as <>
or <CLR String “my CLR string”> or something. This could be a debug
thing
only if you wanted. Then the error message would be:

Expected <> to equal “some mutable string”

Which makes it more clear what is wrong, even if you don’t know the
convention.

[I realise that I ought to just check that the class of the some_array[3]
was actually String in the first place but this would still help.]

[Also, I realise that there is no implicit conversion between CLR string
and
MutableString, so if the method was typed to return a MutableString then
this would not compile, but Ruby doesn’t half like having stuff returned
in
arrays, so you lose the strong typing]

Pete

I would guess that IR-teams plan is to implement auto-wrapping of
strings coming from DotNet as MutableStrings?

At least I hope so since that would get rid of my code of extensive to_s
calls from parameters / values coming from C# :slight_smile:
Robert B.
Software architect
Napa Ltd


From: [email protected]
[mailto:[email protected]] On Behalf Of Peter Bacon
Darwin
Sent: 8. toukokuuta 2008 15:47
To: [email protected]
Subject: [Ironruby-core] Forgetting MutableString

I keep forgetting to wrap up CLR strings in MutableStrings when
returning strings retrieved from calls to .NET Framework library
methods.

I wasn’t a fan of all the calls to to_s either so I put together a small
patch (which undoubtedly is nothing like what the actual fix is going to
be)
to relieve my pains. The bug I submitted along with a patch I made
locally
can be found at:

http://rubyforge.org/tracker/?func=detail&aid=19873&group_id=4359&atid=16798

Cheers,
Steve

On Thu, May 8, 2008 at 8:55 AM, Robert B.
[email protected]

Peter Bacon D.:

I understand that Tomas is working on a version of MutableString that
has a hybrid internal organisation to enable easy cooperation with CLR
strings while maintaining compatibility with Ruby strings (hopefully
without losing out in performance).

Yes- Tomas is working on a new MutableString this week. The idea is to
avoid perf hits in the general case (eg a byte string is created and you
are doing byte-string like things to it). However we will lazy eval
conversions to other string types where necessary.

Not sure where things will line up when the new implementation shows up
this week or next.

Thanks,
-John

Robert B.:

At least I hope so since that would get rid of my code of extensive
to_s calls from parameters / values coming from C# :slight_smile: Robert
Brotherus Software architect Napa Ltd

Yes - your to_s hacks should go away :slight_smile:

Thanks,
-John

The trouble is that MutableStrings and CLR Strings are (going to be, if
not
already) quite different creatures. For a start the CLR string is
always
UTF-16, while the MutableString is basically just an array of bytes,
which
can be interpreted in different ways depending on the K-Code you are
using.

In order to compare these two kinds of strings you need an intelligent
comparer that understands what locale you are working against.

I understand that Tomas is working on a version of MutableString that
has a
hybrid internal organisation to enable easy cooperation with CLR strings
while maintaining compatibility with Ruby strings (hopefully without
losing
out in performance).

Pete

From: [email protected]
[mailto:[email protected]] On Behalf Of Steve E.
Sent: Thursday,08 May 08, 2008 14:06
To: [email protected]
Subject: Re: [Ironruby-core] Forgetting MutableString

I wasn’t a fan of all the calls to to_s either so I put together a small
patch (which undoubtedly is nothing like what the actual fix is going to
be)
to relieve my pains. The bug I submitted along with a patch I made
locally
can be found at:

http://rubyforge.org/tracker/?func=detail
<http://rubyforge.org/tracker/?func=detail&aid=19873&group_id=4359&atid=1679
8> &aid=19873&group_id=4359&atid=16798

Cheers,
Steve

On Thu, May 8, 2008 at 8:55 AM, Robert B.
[email protected]
wrote:

I would guess that IR-teams plan is to implement auto-wrapping of
strings
coming from DotNet as MutableStrings?

At least I hope so since that would get rid of my code of extensive to_s
calls from parameters / values coming from C# :slight_smile:

Robert B.

Software architect
Napa Ltd


From: [email protected]
[mailto:[email protected]] On Behalf Of Peter Bacon
Darwin
Sent: 8. toukokuuta 2008 15:47
To: [email protected]
Subject: [Ironruby-core] Forgetting MutableString

I keep forgetting to wrap up CLR strings in MutableStrings when
returning
strings retrieved from calls to .NET Framework library methods.

This actually makes my problem worse! Now there is no way of catching
the
times when I accidentally return a CLR string rather than a
MutableString.
It will only flag up when client code tries to call a MutableString
method
on the CLR string.
Pete

Peter Bacon D.:

I keep forgetting to wrap up CLR strings in MutableStrings when
returning strings retrieved from calls to .NET Framework library
methods.

I finally understand what this is … is this what you’re looking for?

    public override bool Equals(object other) {
        if (other is MutableString)
            return Equals(other as MutableString);
        else if (other is string)
            return Equals(other as string);
        else
            return false;
    }

    public bool Equals(string other) {
        if (other == null) return false;
        if (_data.Length != other.Length) return false;

        for (int i = 0; i < _data.Length; ++i)
            if (_data[i] != other[i])
                return false;

        return true;
    }

    public bool Equals(MutableString other) {
        if (ReferenceEquals(this, other)) return true;
        if (other == null) return false;
        if (_data.Length != other.Length) return false;

        for (int i = 0; i < _data.Length; ++i)
            if (_data[i] != other.Chars[i])
                return false;

        return true;
    }

If so, it’s coming in the next release.

Thanks,
-John