Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 3 additions & 31 deletions core/src/main/java/org/jruby/RubyArgsFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
import static org.jruby.api.Convert.asBoolean;
import static org.jruby.api.Convert.asFixnum;
import static org.jruby.api.Convert.asSymbol;
import static org.jruby.api.Convert.toByte;
import static org.jruby.api.Convert.toInt;
import static org.jruby.api.Convert.toLong;
import static org.jruby.api.Create.newArray;
Expand Down Expand Up @@ -504,7 +503,7 @@ public static IRubyObject each_byte(final ThreadContext context, IRubyObject rec
if (!block.isGiven()) return enumeratorize(context.runtime, recv, "each_byte");

IRubyObject bt;
while ((bt = getc(context, recv)) != context.nil) {
while ((bt = getbyte(context, recv)) != context.nil) {
block.yield(context, bt);
}

Expand Down Expand Up @@ -533,36 +532,9 @@ public static IRubyObject chars(final ThreadContext context, IRubyObject recv, B
}

public static IRubyObject each_charCommon(ThreadContext context, IRubyObject recv, Block block) {
final Ruby runtime = context.runtime;

ArgsFileData data = ArgsFileData.getArgsFileData(runtime);

IRubyObject ch;
while ((ch = getc(context, recv)) != context.nil) {
boolean cont = true;
while (cont) {
cont = false;
byte c = toByte(context, ch);
int n = runtime.getKCode().getEncoding().length(c);
IRubyObject file = data.currentFile;
RubyString str = runtime.newString();
str.cat(c);

while(--n > 0) {
if ((ch = getc(context, recv)) == context.nil) {
block.yield(context, str);
return recv;
}
if (data.currentFile != file) {
block.yield(context, str);
cont = true;
continue;
}
c = toByte(context, ch);
str.cat(c);
}
block.yield(context, str);
}
block.yield(context, ch);
}
return recv;
}
Expand Down Expand Up @@ -884,7 +856,7 @@ public static IRubyObject getc(ThreadContext context, IRubyObject recv) {

IRubyObject bt;
if (data.currentFile instanceof RubyFile) {
bt = ((RubyIO) data.currentFile).getbyte(context);
bt = ((RubyIO) data.currentFile).getc(context);
} else {
bt = data.currentFile.callMethod(context,"getc");
}
Expand Down
21 changes: 21 additions & 0 deletions spec/ruby/core/argf/each_byte_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,24 @@
describe "ARGF.each_byte" do
it_behaves_like :argf_each_byte, :each_byte
end

describe "ARGF.each_byte when reading $stdin" do
before :each do
@r, @w = IO.pipe
@w.write "hé"
@w.close
@stdin = $stdin
$stdin = @r
end

after :each do
$stdin = @stdin
@r.close unless @r.closed?
end

it "yields each byte as an Integer" do
argf ['-'] do
@argf.each_byte.to_a.should == [104, 195, 169]
end
end
end
21 changes: 21 additions & 0 deletions spec/ruby/core/argf/each_char_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,24 @@
describe "ARGF.each_char" do
it_behaves_like :argf_each_char, :each_char
end

describe "ARGF.each_char when reading $stdin" do
before :each do
@r, @w = IO.pipe("UTF-8")
@w.write "hé"
@w.close
@stdin = $stdin
$stdin = @r
end

after :each do
$stdin = @stdin
@r.close unless @r.closed?
end

it "yields a multibyte character as one String" do
argf ['-'] do
@argf.each_char.to_a.should == ["h", "é"]
end
end
end
1 change: 1 addition & 0 deletions spec/ruby/core/argf/fixtures/multibyte.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
été€
8 changes: 8 additions & 0 deletions spec/ruby/core/argf/shared/each_char.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
end
end

it "yields a multibyte character as one String" do
multibyte_name = fixture __FILE__, "multibyte.txt"
argf [multibyte_name] do
@argf.set_encoding(Encoding::UTF_8)
@argf.send(@method).to_a.should == ["é", "t", "é", "€"]
end
end

it "returns self when passed a block" do
argf [@file1_name, @file2_name] do
@argf.send(@method) {}.should equal(@argf)
Expand Down
9 changes: 9 additions & 0 deletions spec/ruby/core/argf/shared/getc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
before :each do
@file1 = fixture __FILE__, "file1.txt"
@file2 = fixture __FILE__, "file2.txt"
@multibyte = fixture __FILE__, "multibyte.txt"

@chars = File.read @file1
@chars += File.read @file2
Expand All @@ -14,4 +15,12 @@
chars.should == @chars
end
end

it "returns each character as a String" do
argf [@multibyte] do
@argf.set_encoding(Encoding::UTF_8)
chars = 4.times.map { @argf.send(@method) }
chars.should == ["é", "t", "é", "€"]
end
end
end
1 change: 0 additions & 1 deletion test/mri/excludes/TestArgf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
exclude :test_lineno, "needs investigation"
exclude :test_lineno2, "needs investigation"
exclude :test_readlines_limit_0, "HANGS"
exclude :test_skip_in_each_char, "needs investigation"
exclude :test_skip_in_each_codepoint, "needs investigation"
exclude :test_to_io, "needs investigation"
exclude :test_wrong_type, "needs investigation"
Loading