Skip to content

Commit

Permalink
Add encoding parameter for Dir#each and foreach. Fixes #2547.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Apr 17, 2015
1 parent d2ea85f commit 26ed114
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
42 changes: 37 additions & 5 deletions core/src/main/java/org/jruby/RubyDir.java
Expand Up @@ -387,10 +387,24 @@ public static IRubyObject foreach(ThreadContext context, IRubyObject recv, IRuby
public static IRubyObject foreach19(ThreadContext context, IRubyObject recv, IRubyObject arg, Block block) {
RubyString pathString = RubyFile.get_path(context, arg);

return foreachCommon(context, recv, context.runtime, pathString, block);
return foreachCommon(context, recv, context.runtime, pathString, null, block);
}

private static IRubyObject foreachCommon(ThreadContext context, IRubyObject recv, Ruby runtime, RubyString _path, Block block) {
@JRubyMethod(name = "foreach", meta = true)
public static IRubyObject foreach19(ThreadContext context, IRubyObject recv, IRubyObject path, IRubyObject enc, Block block) {
RubyString pathString = RubyFile.get_path(context, path);
RubyEncoding encoding;

if (enc instanceof RubyEncoding) {
encoding = (RubyEncoding) enc;
} else {
throw context.runtime.newTypeError(enc, context.runtime.getEncoding());
}

return foreachCommon(context, recv, context.runtime, pathString, encoding, block);
}

private static IRubyObject foreachCommon(ThreadContext context, IRubyObject recv, Ruby runtime, RubyString _path, RubyEncoding encoding, Block block) {
if (block.isGiven()) {
RubyClass dirClass = runtime.getDir();
RubyDir dir = (RubyDir) dirClass.newInstance(context, new IRubyObject[]{_path}, block);
Expand All @@ -399,7 +413,11 @@ private static IRubyObject foreachCommon(ThreadContext context, IRubyObject recv
return runtime.getNil();
}

return enumeratorize(runtime, recv, "foreach", _path);
if (encoding == null) {
return enumeratorize(runtime, recv, "foreach", _path);
} else {
return enumeratorize(runtime, recv, "foreach", new IRubyObject[]{_path, encoding});
}
}

/** Returns the current directory. */
Expand Down Expand Up @@ -508,22 +526,36 @@ public IRubyObject close() {
/**
* Executes the block once for each entry in the directory.
*/
public IRubyObject each(ThreadContext context, Block block) {
public IRubyObject each(ThreadContext context, Encoding enc, Block block) {
checkDir();

String[] contents = snapshot;
for (pos = 0; pos < contents.length; pos++) {
block.yield(context, getRuntime().newString(contents[pos]));
block.yield(context, RubyString.newString(context.runtime, contents[pos], enc));
}

return this;
}

/**
* Executes the block once for each entry in the directory.
*/
public IRubyObject each(ThreadContext context, Block block) {
return each(context, context.runtime.getDefaultInternalEncoding(), block);
}

@JRubyMethod(name = "each")
public IRubyObject each19(ThreadContext context, Block block) {
return block.isGiven() ? each(context, block) : enumeratorize(context.runtime, this, "each");
}

@JRubyMethod(name = "each")
public IRubyObject each19(ThreadContext context, IRubyObject encoding, Block block) {
if (!(encoding instanceof RubyEncoding)) throw context.runtime.newTypeError(encoding, context.runtime.getEncoding());

return block.isGiven() ? each(context, ((RubyEncoding)encoding).getEncoding(), block) : enumeratorize(context.runtime, this, "each", encoding);
}

@Override
@JRubyMethod
public IRubyObject inspect() {
Expand Down
7 changes: 3 additions & 4 deletions test/mri/excludes/TestDir_M17N.rb
@@ -1,14 +1,13 @@
exclude :test_glob_incompatible, "needs investigation"
exclude :test_error_nonascii, "needs investigation"
exclude :test_filename_as_bytes_extutf8, "needs investigation"
exclude :test_filename_bytes_euc_jp, "needs investigation"
exclude :test_filename_euc_jp, "needs investigation"
exclude :test_filename_ext_euc_jp_and_int_utf_8, "needs investigation"
exclude :test_filename_extutf8, "needs investigation"
exclude :test_filename_extutf8_inteucjp_representable, "needs investigation"
exclude :test_filename_extutf8_inteucjp_unrepresentable, "needs investigation"
exclude :test_filename_extutf8_invalid, "needs investigation"
exclude :test_error_nonascii, "needs investigation"
exclude :test_inspect_nonascii, "needs investigation"
exclude :test_filename_utf8_raw_jp_name, "broken subprocess logic in setup"
exclude :test_filename_utf8_raw_windows_1251_name, "broken subprocess logic in setup"
exclude :test_filename_utf8_raw_windows_1252_name, "broken subprocess logic in setup"
exclude :test_glob_incompatible, "needs investigation"
exclude :test_inspect_nonascii, "needs investigation"

0 comments on commit 26ed114

Please sign in to comment.