[Ironruby-core] RubyForge bug fixes

Unnikrishnan Nair ksunair at yahoo.com
Sat May 10 17:59:19 EDT 2008


Hi John,
Here is my code that I have, this is the first method I attemepted since this is one of the interesting logic. I thought I post my code for you to review as well. This passed most of the test as well[
{
returnString = path;
}
[
{
}
[
{RubyMethod("basename", RubyMethodAttributes.PublicSingleton)]publicstaticMutableStringBasename(CodeContext/*!*/context, object/*!*/self, [NotNull]MutableString/*!*/path)MutableString[] tokens = path.Split(@"\/".ToCharArray(), Int32.MaxValue, StringSplitOptions.RemoveEmptyEntries);MutableStringreturnString = (tokens.GetLength(0) > 0) ? tokens[tokens.GetLength(0) - 1] : path; if((tokens.GetLength(0) > 0) && (returnString.GetChar(returnString.Length - 1).Equals(':')))returnreturnString;RubyMethod("basename", RubyMethodAttributes.PublicSingleton)]publicstaticMutableStringBasename(CodeContext/*!*/context, object/*!*/self, object/*!*/path)returnBasename(context, self, Protocols.CastToString(context, path));RubyMethod("basename", RubyMethodAttributes.PublicSingleton)]publicstaticMutableStringBasename(CodeContext/*!*/context, object/*!*/self, [NotNull]MutableString/*!*/path, [NotNull]MutableString/*!*/filter)MutableStringfilename = Basename(context, self,
 path);//Treat ? speciallyif(filter.IndexOf('?') >= 0)returnfilename;//Treat .* and exetions specially using regex{
filter.Clear();
filter.Append(
}if(filter.Equals(MutableString.Create(".*")))@"(?x)(\.[^.]*$|$)");else{
filter.Append(
}
System.Text.RegularExpressions.
filename.Replace(mat.Index, mat.Length, 
}
[
{
}
Let me know what do you think? Yes, I found some problems with test spec as well, (bar.txt should be baz)
Thanks."$");RubyRegexreg = newRubyRegex(filter, System.Text.RegularExpressions.RegexOptions.Singleline);Matchmat = reg.Match(filename);if(mat.Success)MutableString.Create(""));returnfilename;RubyMethod("basename", RubyMethodAttributes.PublicSingleton)]publicstaticMutableStringBasename(CodeContext/*!*/context, object/*!*/self, object/*!*/path, object/*!*/filter)returnBasename(context, self, Protocols.CastToString(context, path), Protocols.CastToString(context, filter));



----- Original Message ----
From: John Lam (IRONRUBY) <jflam at microsoft.com>
To: "ironruby-core at rubyforge.org" <ironruby-core at rubyforge.org>
Sent: Saturday, May 10, 2008 3:25:16 PM
Subject: Re: [Ironruby-core] RubyForge bug fixes

Unnikrishnan Nair:

> Quick question, are these following assertions are correct?
>
>    should_raise(TypeError){ File.basename(1) }
>    should_raise(TypeError){ File.basename("bar.txt", 1) }
>    should_raise(TypeError){ File.basename(true) }

Yes they are. You can easily verify this in MRI yourself.

In first and third cases, it hits the overload that accepts a nullable object as the first parameter. The TypeError is raised via Protocols.CastToString().

In the second case, it also hits an overload that accepts a nullable object as its second parameter, which raises TypeError via Protocols.CastToString().

FYI, this is the implementation of #basename that I have in my shelveset. It passes all of the (valid) specs - there are some legitimate bugs in our old copy of the specs (I haven't checked with the latest version of the rubinius specs - I'll do that next week). I'm a bit worried about how I'm handling the special cases for Windows.

[RubyMethod("basename", RubyMethodAttributes.PublicSingleton)]
public static MutableString/*!*/ Basename(CodeContext/*!*/ context, object/*!*/ self, [NotNull]MutableString/*!*/ path, [NotNull]MutableString/*!*/ extensionFilter) {
    if (path.Length == 0)
        return path;

    MutableString trimmedPath = TrimTrailingSlashes(path);

    // Special cases of drive letters C:\\ or C:/
    if (trimmedPath.Length == 2)
        if (Char.IsLetter(trimmedPath.GetChar(0)) && trimmedPath.GetChar(1) == ':')
            return Kernel.FlowTaint(context, path, (path.Length > 2 ? MutableString.Create(path.GetChar(2).ToString()) : MutableString.Create(String.Empty)));

    string trimmedPathAsString = trimmedPath.ConvertToString();
    if (trimmedPathAsString == "/")
        return trimmedPath;

    string filename = System.IO.Path.GetFileName(trimmedPath.ConvertToString());

    // Handle UNC host names correctly
    string root = System.IO.Path.GetPathRoot(trimmedPath.ConvertToString());
    if (extensionFilter.Length == 0)
        return trimmedPathAsString == root ? MutableString.Create(root) : MutableString.Create(filename);

    string fileExtension = System.IO.Path.GetExtension(filename);
    string basename = System.IO.Path.GetFileNameWithoutExtension(filename);

    string result = WildcardExtensionMatch(fileExtension, extensionFilter.ConvertToString()) ? basename : filename;
    return Kernel.FlowTaint(context, self, (result.Equals(root) ? MutableString.Create(root) : MutableString.Create(result)));
}

[RubyMethod("basename", RubyMethodAttributes.PublicSingleton)]
public static MutableString/*!*/ Basename(CodeContext/*!*/ context, object/*!*/ self, [NotNull]MutableString/*!*/ path) {
    return Basename(context, self, path, MutableString.Empty);
}

[RubyMethod("basename", RubyMethodAttributes.PublicSingleton)]
public static MutableString/*!*/ Basename(CodeContext/*!*/ context, object/*!*/ self, object path, object extension) {
    return Basename(context, self, Protocols.CastToString(context, path), Protocols.CastToString(context, extension));
}

[RubyMethod("basename", RubyMethodAttributes.PublicSingleton)]
public static MutableString/*!*/ Basename(CodeContext/*!*/ context, object/*!*/ self, object path) {
    return Basename(context, self, Protocols.CastToString(context, path));
}

> Also, in the basename_spec the test setup has wrong parameter on
> File.open(@name, 'w+'), if I change it to 'w', the test runs fine
> otherwise none of the test works.

This is correct. There is a bug in how we map the semantics of 'w+' to .NET IO. It's fixed in my shelveset.

Thanks,
-John



_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org
http://rubyforge.org/mailman/listinfo/ironruby-core
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20080510/55c016fd/attachment.html>


More information about the Ironruby-core mailing list