Created
February 22, 2014 21:06
-
-
Save anonymous/9162419 to your computer and use it in GitHub Desktop.
A comparison of how some scripting languages handle embedded nuls '\0' when iterating over lines. Note: Tcl does not return the end-of-line character.
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
$ python io.py | |
12 hello.world. | |
12 hello.world. | |
12 hello.world. | |
$ ruby io.rb | |
12 hello.world. | |
12 hello.world. | |
12 hello.world. | |
$ tclsh io.tcl | |
11 hello.world | |
11 hello.world | |
11 hello.world | |
$ perl io.perl | |
12 hello.world. | |
12 hello.world. | |
12 hello.world. | |
$ lua io.lua | |
15 hellohellohello | |
$ cat io.py | |
def printable(c): | |
return c if 32 < ord(c) < 128 else '.' | |
f = open('foo', 'r') | |
for l in f.readlines(): | |
print len(l), ''.join(printable(c) for c in l) | |
$ cat io.rb | |
File.open('foo').each do |l| | |
l = l.gsub(/[\x00-\x1f]/, '.') | |
print l.length, " #{l}\n" | |
end | |
$ cat io.tcl | |
set f [open foo] | |
while {1} { | |
set line [gets $f] | |
if {[eof $f]} { | |
close $f | |
break | |
} | |
regsub -all {[^\w()]} $line "." line | |
puts "[string length $line] $line" | |
} | |
$ cat io.perl | |
open (MYFILE, 'foo'); | |
while (<MYFILE>) { | |
$l = $_; | |
$l=~s/[\x00-\x1F]+/./g; | |
$len = length $l; | |
print "$len $l\n"; | |
} | |
close (MYFILE); | |
$ cat io.lua | |
function to_printable(c) | |
local b = string.byte(c) | |
if b > 32 and b < 128 then return c else return '.' end | |
end | |
f = io.open('foo') | |
for l in f:lines() do | |
print(#l, (l:gsub('(.)', to_printable ))) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment