Skip to content

Commit 9bd135f

Browse files
committedJul 6, 2018
[refactor] unify Dir each/entries encoding usage + cleanup
... use getDefaultEncoding uniformly across all methods
1 parent 1f0f235 commit 9bd135f

File tree

2 files changed

+59
-38
lines changed

2 files changed

+59
-38
lines changed
 

Diff for: ‎core/src/main/java/org/jruby/RubyDir.java

+55-38
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ private final void checkDirIgnoreClosed() {
115115
// update snapshot (if changed) :
116116
if (snapshot == null || dir.exists() && dir.lastModified() > lastModified) {
117117
lastModified = dir.lastModified();
118-
final List<String> contents = getContents(dir);
119-
snapshot = contents.toArray(new String[contents.size()]);
118+
snapshot = list(dir);
120119
}
121120
}
122121

@@ -294,10 +293,12 @@ public RubyArray entries() {
294293
return RubyArray.newArrayMayCopy(getRuntime(), JavaUtil.convertJavaArrayToRuby(getRuntime(), snapshot));
295294
}
296295

296+
@Deprecated
297297
public static RubyArray entries(IRubyObject recv, IRubyObject path) {
298298
return entries(recv.getRuntime().getCurrentContext(), recv, path);
299299
}
300300

301+
@Deprecated
301302
public static RubyArray entries(IRubyObject recv, IRubyObject path, IRubyObject arg, IRubyObject opts) {
302303
return entries(recv.getRuntime().getCurrentContext(), recv, path, opts);
303304
}
@@ -317,7 +318,7 @@ public static RubyArray entries(ThreadContext context, IRubyObject recv, IRubyOb
317318
@JRubyMethod(name = "entries", meta = true)
318319
public static RubyArray entries(ThreadContext context, IRubyObject recv, IRubyObject arg, IRubyObject opts) {
319320
Ruby runtime = context.runtime;
320-
Encoding encoding = runtime.getDefaultInternalEncoding();
321+
Encoding encoding = null;
321322

322323
RubyString path = StringSupport.checkEmbeddedNulls(runtime, RubyFile.get_path(context, arg));
323324

@@ -327,6 +328,8 @@ public static RubyArray entries(ThreadContext context, IRubyObject recv, IRubyOb
327328
encoding = runtime.getEncodingService().getEncodingFromObject(encodingArg);
328329
}
329330
}
331+
if (encoding == null) encoding = runtime.getDefaultEncoding();
332+
330333
return entriesCommon(context, path.asJavaString(), encoding, false);
331334
}
332335

@@ -526,31 +529,33 @@ public static IRubyObject foreach19(ThreadContext context, IRubyObject recv, IRu
526529
}
527530

528531
private static IRubyObject eachChildCommon(ThreadContext context, IRubyObject recv, RubyString path, RubyEncoding encoding, Block block) {
532+
final Ruby runtime = context.runtime;
529533
if (block.isGiven()) {
530-
RubyDir dir = (RubyDir) context.runtime.getDir().newInstance(context, path, Block.NULL_BLOCK);
534+
RubyDir dir = (RubyDir) runtime.getDir().newInstance(context, path, Block.NULL_BLOCK);
531535

532-
dir.each_child(context, block);
536+
dir.each_child(context, encoding == null ? runtime.getDefaultEncoding() : encoding.getEncoding(), block);
533537
return context.nil;
534538
}
535539

536540
if (encoding == null) {
537-
return enumeratorize(context.runtime, recv, "each_child", path);
541+
return enumeratorize(runtime, recv, "each_child", path);
538542
}
539-
return enumeratorize(context.runtime, recv, "each_child", path, encoding);
543+
return enumeratorize(runtime, recv, "each_child", path, encoding);
540544
}
541545

542546
private static IRubyObject foreachCommon(ThreadContext context, IRubyObject recv, RubyString path, RubyEncoding encoding, Block block) {
547+
final Ruby runtime = context.runtime;
543548
if (block.isGiven()) {
544-
RubyDir dir = (RubyDir) context.runtime.getDir().newInstance(context, path, Block.NULL_BLOCK);
549+
RubyDir dir = (RubyDir) runtime.getDir().newInstance(context, path, Block.NULL_BLOCK);
545550

546-
dir.each(context, block);
551+
dir.each(context, encoding == null ? runtime.getDefaultEncoding() : encoding.getEncoding(), block);
547552
return context.nil;
548553
}
549554

550555
if (encoding == null) {
551-
return enumeratorize(context.runtime, recv, "foreach", path);
556+
return enumeratorize(runtime, recv, "foreach", path);
552557
}
553-
return enumeratorize(context.runtime, recv, "foreach", path, encoding);
558+
return enumeratorize(runtime, recv, "foreach", path, encoding);
554559
}
555560

556561
/** Returns the current directory. */
@@ -682,6 +687,31 @@ public IRubyObject each(ThreadContext context, Encoding enc, Block block) {
682687
return this;
683688
}
684689

690+
/**
691+
* Executes the block once for each entry in the directory.
692+
*/
693+
@JRubyMethod(name = "each")
694+
public IRubyObject each(ThreadContext context, Block block) {
695+
return block.isGiven() ? each(context, context.runtime.getDefaultEncoding(), block) : enumeratorize(context.runtime, this, "each");
696+
}
697+
698+
@JRubyMethod(name = "each")
699+
public IRubyObject each(ThreadContext context, IRubyObject encoding, Block block) {
700+
if (!(encoding instanceof RubyEncoding)) throw context.runtime.newTypeError(encoding, context.runtime.getEncoding());
701+
702+
return block.isGiven() ? each(context, ((RubyEncoding) encoding).getEncoding(), block) : enumeratorize(context.runtime, this, "each", encoding);
703+
}
704+
705+
@Deprecated
706+
public IRubyObject each19(ThreadContext context, Block block) {
707+
return each(context, block);
708+
}
709+
710+
@Deprecated
711+
public IRubyObject each19(ThreadContext context, IRubyObject encoding, Block block) {
712+
return each(context, encoding, block);
713+
}
714+
685715
/**
686716
* Executes the block once for each child in the directory
687717
* (i.e. all the directory entries except for "." and "..").
@@ -691,39 +721,20 @@ public IRubyObject each_child(ThreadContext context, Encoding enc, Block block)
691721

692722
String[] contents = snapshot;
693723
for (pos = 0; pos < contents.length; pos++) {
694-
if (!(contents[pos].equals(".") || contents[pos].equals(".."))) {
695-
block.yield(context, RubyString.newString(context.runtime, contents[pos], enc));
696-
}
724+
if (StringSupport.contentEquals(contents[pos], '.')) continue; /* current dir */
725+
if (StringSupport.contentEquals(contents[pos], '.', '.')) continue; /* parent dir */
726+
block.yield(context, RubyString.newString(context.runtime, contents[pos], enc));
697727
}
698728

699729
return this;
700730
}
701731

702-
/**
703-
* Executes the block once for each entry in the directory.
704-
*/
705-
public IRubyObject each(ThreadContext context, Block block) {
706-
return each(context, context.runtime.getDefaultInternalEncoding(), block);
707-
}
708-
709732
/**
710733
* Executes the block once for each child in the directory
711734
* (i.e. all the directory entries except for "." and "..").
712735
*/
713736
public IRubyObject each_child(ThreadContext context, Block block) {
714-
return each_child(context, context.runtime.getDefaultInternalEncoding(), block);
715-
}
716-
717-
@JRubyMethod(name = "each")
718-
public IRubyObject each19(ThreadContext context, Block block) {
719-
return block.isGiven() ? each(context, block) : enumeratorize(context.runtime, this, "each");
720-
}
721-
722-
@JRubyMethod(name = "each")
723-
public IRubyObject each19(ThreadContext context, IRubyObject encoding, Block block) {
724-
if (!(encoding instanceof RubyEncoding)) throw context.runtime.newTypeError(encoding, context.runtime.getEncoding());
725-
726-
return block.isGiven() ? each(context, ((RubyEncoding)encoding).getEncoding(), block) : enumeratorize(context.runtime, this, "each", encoding);
737+
return each_child(context, context.runtime.getDefaultEncoding(), block);
727738
}
728739

729740
@Override
@@ -788,6 +799,7 @@ public String getPath() {
788799
public IRubyObject read() {
789800
checkDir();
790801

802+
final String[] snapshot = this.snapshot;
791803
if (pos >= snapshot.length) return getRuntime().getNil();
792804

793805
RubyString result = getRuntime().newString(snapshot[pos]);
@@ -926,9 +938,15 @@ private static String dirFromPath(final String path, final Ruby runtime) throws
926938
return dir;
927939
}
928940

941+
private static String[] list(FileResource directory) {
942+
final String[] contents = directory.list();
943+
// If an IO exception occurs (something odd, but possible)
944+
// A directory may return null.
945+
return contents == null ? NO_FILES : contents;
946+
}
947+
929948
/**
930-
* Returns the contents of the specified <code>directory</code> as an
931-
* <code>ArrayList</code> containing the names of the files as Java Strings.
949+
* @deprecated no longer used
932950
*/
933951
protected static List<String> getContents(FileResource directory) {
934952
final String[] contents = directory.list();
@@ -946,8 +964,7 @@ protected static List<String> getContents(FileResource directory) {
946964
}
947965

948966
/**
949-
* Returns the contents of the specified <code>directory</code> as an
950-
* <code>ArrayList</code> containing the names of the files as Ruby Strings.
967+
* @deprecated no longer used
951968
*/
952969
protected static List<RubyString> getContents(FileResource directory, Ruby runtime) {
953970
final String[] contents = directory.list();

Diff for: ‎core/src/main/java/org/jruby/util/StringSupport.java

+4
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ public static boolean contentEquals(final CharSequence str, final int chr) {
163163
return (str.length() == 1) && str.charAt(0) == chr;
164164
}
165165

166+
public static boolean contentEquals(final CharSequence str, final int chr1, final int chr2) {
167+
return (str.length() == 2) && str.charAt(0) == chr1 && str.charAt(1) == chr2;
168+
}
169+
166170
public static CharSequence concat(final CharSequence str1, final CharSequence str2) {
167171
return new StringBuilder(str1.length() + str2.length()).append(str1).append(str2);
168172
}

0 commit comments

Comments
 (0)
Please sign in to comment.