Skip to content

Instantly share code, notes, and snippets.

Created February 22, 2014 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9162419 to your computer and use it in GitHub Desktop.
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.
$ 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