Created
December 9, 2022 10:55
-
-
Save YO4/262e9bd5e44a37a7a2fa9118e271b30b to your computer and use it in GitHub Desktop.
ruby newline conversion sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'tempfile' | |
$tmp = Tempfile.new.binmode | |
$tmp.write("a" "\r\n" "\r" "\x1a" "1" "\r") | |
$tmp.flush | |
def read(mode, **opts) | |
File.open($tmp.path, mode, **opts) do |f| | |
f.readlines.join('') | |
end | |
end | |
def classify(str) | |
case str | |
when "a" "\n" "\r" | |
"DOS crlf+eof" | |
when "a" "\n" "\n" "\x1a" "1" "\n" | |
"universal newline" | |
when "a" "\r\n" "\r" "\x1a" "1" "\r" | |
"no conversion" | |
else | |
str | |
end | |
end | |
def format(modestr, **opts) | |
if opts.empty? | |
modestr.inspect | |
else | |
"\"#{modestr}\" with #{opts.map { |k, v| "#{k.to_s}: #{v}"}.join(", ")}" | |
end | |
end | |
def test(modestr, **opts) | |
puts sprintf("%18s %s", classify(read(modestr, **opts)), format(modestr, **opts)) | |
end | |
test("rt") | |
test("rt", universal_newline: false) | |
test("rt", crlf_newline: true) | |
test("r") | |
test("r", universal_newline: true) | |
test("r", universal_newline: false) | |
test("r", crlf_newline: false) | |
test("r", textmode: true) | |
test("r", textmode: false) | |
test("r:US-ASCII:UTF-8") | |
test("r:US-ASCII:UTF-8", universal_newline: false) | |
# ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x64-mingw-ucrt] | |
# R:\>ruby .\test_newline_conversion.rb | |
# universal newline "rt" | |
# no conversion "rt" with universal_newline: false | |
# DOS crlf+eof "rt" with crlf_newline: true | |
# DOS crlf+eof "r" | |
# universal newline "r" with universal_newline: true | |
# no conversion "r" with universal_newline: false | |
# no conversion "r" with crlf_newline: false | |
# universal newline "r" with textmode: true | |
# DOS crlf+eof "r" with textmode: false | |
# universal newline "r:US-ASCII:UTF-8" | |
# no conversion "r:US-ASCII:UTF-8" with universal_newline: false | |
# ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux] | |
# /mnt/r$ ruby ./test_newline_conversion.rb | |
# universal newline "rt" | |
# no conversion "rt" with universal_newline: false | |
# no conversion "rt" with crlf_newline: true | |
# no conversion "r" | |
# universal newline "r" with universal_newline: true | |
# no conversion "r" with universal_newline: false | |
# no conversion "r" with crlf_newline: false | |
# universal newline "r" with textmode: true | |
# no conversion "r" with textmode: false | |
# no conversion "r:US-ASCII:UTF-8" | |
# no conversion "r:US-ASCII:UTF-8" with universal_newline: false | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment