Skip to content

Commit

Permalink
Arity-split fnmatch(?)
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Jan 4, 2024
1 parent 3315ec7 commit 85afb4e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
36 changes: 27 additions & 9 deletions core/src/main/java/org/jruby/RubyFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -1005,21 +1005,27 @@ public static IRubyObject realpath(ThreadContext context, IRubyObject recv, IRub
* [set]: Matches a single char in a set (re: [...]).
*
*/
@JRubyMethod(name = {"fnmatch", "fnmatch?"}, required = 2, optional = 1, checkArity = false, meta = true)
public static IRubyObject fnmatch(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
int argc = Arity.checkArgumentCount(context, args, 2, 3);
@JRubyMethod(name = {"fnmatch", "fnmatch?"}, meta = true)
public static IRubyObject fnmatch(ThreadContext context, IRubyObject recv, IRubyObject _pattern, IRubyObject _path) {
return fnmatchCommon(context, _pattern, _path, 0);
}

Ruby runtime = context.runtime;
int flags = argc == 3 ? RubyNumeric.num2int(args[2]) : 0;
@JRubyMethod(name = {"fnmatch", "fnmatch?"}, meta = true)
public static IRubyObject fnmatch(ThreadContext context, IRubyObject recv, IRubyObject _pattern, IRubyObject _path, IRubyObject _flags) {
return fnmatchCommon(context, _pattern, _path, RubyNumeric.num2int(_flags));
}

private static RubyBoolean fnmatchCommon(ThreadContext context, IRubyObject _path, IRubyObject _pattern, int flags) {
boolean braces_match = false;
boolean extglob = (flags & FNM_EXTGLOB) != 0;

ByteList pattern = StringSupport.checkEmbeddedNulls(runtime, args[0].convertToString()).getByteList();
ByteList path = StringSupport.checkEmbeddedNulls(runtime, get_path(context, args[1])).getByteList();
Ruby runtime = context.runtime;
ByteList pattern = StringSupport.checkEmbeddedNulls(runtime, _path.convertToString()).getByteList();
ByteList path = StringSupport.checkEmbeddedNulls(runtime, get_path(context, _pattern)).getByteList();

if(extglob) {
String spattern = args[0].asJavaString();
ArrayList<String> patterns = org.jruby.util.Dir.braces(spattern, flags, new ArrayList<String>());
String spattern = _path.asJavaString();
ArrayList<String> patterns = Dir.braces(spattern, flags, new ArrayList<String>());

ArrayList<Boolean> matches = new ArrayList<Boolean>();
for(int i = 0; i < patterns.size(); i++) {
Expand Down Expand Up @@ -2612,4 +2618,16 @@ public static IRubyObject realpath(ThreadContext context, IRubyObject recv, IRub
}
return file;
}

@Deprecated
public static IRubyObject fnmatch(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
switch (args.length) {
case 2:
return fnmatch(context, recv, args[0], args[1]);
case 3:
return fnmatch(context, recv, args[0], args[1], args[2]);
default:
throw context.runtime.newArgumentError(args.length, 2, 3);
}
}
}
26 changes: 21 additions & 5 deletions core/src/main/java/org/jruby/ext/pathname/RubyPathname.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,16 @@ public IRubyObject sub_ext(ThreadContext context, IRubyObject newExt) {

/* Facade for File */

@JRubyMethod(alias = "fnmatch?", required = 1, optional = 1, checkArity = false)
public IRubyObject fnmatch(ThreadContext context, IRubyObject[] args) {
Arity.checkArgumentCount(context, args, 1, 2);
@JRubyMethod(alias = "fnmatch?")
public IRubyObject fnmatch_p(ThreadContext context, IRubyObject arg0) {
RubyClass File = context.runtime.getFile();
return sites(context).fnmatch_p.call(context, File, File, arg0, getPath());
}

args = insertPath(args, 1);
return context.runtime.getFile().callMethod(context, "fnmatch?", args);
@JRubyMethod(alias = "fnmatch?")
public IRubyObject fnmatch_p(ThreadContext context, IRubyObject arg0, IRubyObject arg1) {
RubyClass File = context.runtime.getFile();
return sites(context).fnmatch_p.call(context, File, File, arg0, getPath(), arg1);
}

@JRubyMethod
Expand Down Expand Up @@ -447,4 +451,16 @@ public IRubyObject taint(ThreadContext context) {
public IRubyObject untaint(ThreadContext context) {
return this;
}

@Deprecated
public IRubyObject fnmatch(ThreadContext context, IRubyObject[] args) {
switch (args.length) {
case 1:
return fnmatch_p(context, args[0]);
case 2:
return fnmatch_p(context, args[0], args[1]);
default:
throw context.runtime.newArgumentError(args.length, 1, 2);
}
}
}
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/runtime/JavaSites.java
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ public static class PathnameSites {
public final CallSite glob = new FunctionalCachingCallSite("glob");
public final CallSite op_plus = new FunctionalCachingCallSite("+");
public final CallSite sub = new FunctionalCachingCallSite("sub");
public final CallSite fnmatch_p = new FunctionalCachingCallSite("fnmatch?");
}

public static class DateSites {
Expand Down

0 comments on commit 85afb4e

Please sign in to comment.