Code cleanup

This commit is contained in:
Alexander Simonov 2011-11-17 22:53:04 +02:00
parent 7cb60f5a37
commit 3418d31b44
10 changed files with 87 additions and 144 deletions

View File

@ -1,5 +1,3 @@
require 'zip/constants'
module Zip
class Compressor #:nodoc:all
def finish

View File

@ -8,13 +8,13 @@ module Zip
end
def sysread(numberOfBytes = nil, buf = nil)
readEverything = (numberOfBytes == nil)
while (readEverything || @outputBuffer.length < numberOfBytes)
readEverything = numberOfBytes.nil?
while (readEverything || @outputBuffer.bytesize < numberOfBytes)
break if internal_input_finished?
@outputBuffer << internal_produce_input(buf)
end
return value_when_finished if @outputBuffer.length==0 && input_finished?
endIndex= numberOfBytes==nil ? @outputBuffer.length : numberOfBytes
return value_when_finished if @outputBuffer.bytesize == 0 && input_finished?
endIndex = numberOfBytes.nil? ? @outputBuffer.bytesize : numberOfBytes
return @outputBuffer.slice!(0...endIndex)
end
@ -53,7 +53,7 @@ module Zip
# TODO: Specialize to handle different behaviour in ruby > 1.7.0 ?
def value_when_finished # mimic behaviour of ruby File object.
return nil if @hasReturnedEmptyString
return if @hasReturnedEmptyString
@hasReturnedEmptyString = true
return ""
end

View File

@ -45,11 +45,11 @@ module IOExtras #:nodoc:
def read(numberOfBytes = nil, buf = nil)
tbuf = nil
if @outputBuffer.length > 0
if numberOfBytes <= @outputBuffer.length
if @outputBuffer.bytesize > 0
if numberOfBytes <= @outputBuffer.bytesize
tbuf = @outputBuffer.slice!(0, numberOfBytes)
else
numberOfBytes -= @outputBuffer.length if (numberOfBytes)
numberOfBytes -= @outputBuffer.bytesize if (numberOfBytes)
rbuf = sysread(numberOfBytes, buf)
tbuf = @outputBuffer
tbuf << rbuf if (rbuf)
@ -73,23 +73,23 @@ module IOExtras #:nodoc:
def readlines(aSepString = $/)
retVal = []
each_line(aSepString) { |line| retVal << line }
return retVal
retVal
end
def gets(aSepString = $/)
@lineno = @lineno.next
return read if aSepString == nil
aSepString="#{$/}#{$/}" if aSepString == ""
return read if aSepString.nil?
aSepString = "#{$/}#{$/}" if aSepString.empty?
bufferIndex = 0
while ((matchIndex = @outputBuffer.index(aSepString, bufferIndex)) == nil)
bufferIndex=@outputBuffer.length
bufferIndex = @outputBuffer.bytesize
if input_finished?
return @outputBuffer.empty? ? nil : flush
end
@outputBuffer << produce_input
end
sepIndex=matchIndex + aSepString.length
sepIndex = matchIndex + aSepString.bytesize
return @outputBuffer.slice!(0...sepIndex)
end
@ -102,7 +102,7 @@ module IOExtras #:nodoc:
def readline(aSepString = $/)
retVal = gets(aSepString)
raise EOFError if retVal == nil
return retVal
retVal
end
def each_line(aSepString = $/)
@ -123,7 +123,7 @@ module IOExtras #:nodoc:
def write(data)
self << data
data.to_s.length
data.to_s.bytesize
end
@ -146,12 +146,11 @@ module IOExtras #:nodoc:
def puts(*params)
params << "\n" if params.empty?
params.flatten.each {
|element|
params.flatten.each do |element|
val = element.to_s
self << val
self << "\n" unless val[-1,1] == "\n"
}
end
end
end

View File

@ -13,7 +13,7 @@ module Zip
hasReturnedEmptyStringVal = @hasReturnedEmptyString
@hasReturnedEmptyString = true
return "" unless hasReturnedEmptyStringVal
return nil
return
end
if (numberOfBytes == nil || @readSoFar + numberOfBytes > @charsToRead)

View File

@ -1,12 +1,3 @@
unless Enumerable.method_defined?(:inject)
module Enumerable #:nodoc:all
def inject(n = 0)
each { |value| n = yield(n, value) }
n
end
end
end
module Enumerable #:nodoc:all
# returns a new array of all the return values not equal to nil
# This implementation could be faster
@ -15,34 +6,10 @@ module Enumerable #:nodoc:all
end
end
unless Object.method_defined?(:object_id)
class Object #:nodoc:all
# Using object_id which is the new thing, so we need
# to make that work in versions prior to 1.8.0
alias object_id id
end
end
unless File.respond_to?(:read)
class File # :nodoc:all
# singleton method read does not exist in 1.6.x
def self.read(fileName)
open(fileName) { |f| f.read }
end
end
end
class String #:nodoc:all
def starts_with(aString)
rindex(aString, 0) == 0
end
def ends_with(aString)
index(aString, -aString.size)
end
def ensure_end(aString)
ends_with(aString) ? self : self + aString
end_with?(aString) ? self : self + aString
end
def lchop
@ -97,10 +64,9 @@ end
class Module #:nodoc:all
def forward_message(forwarder, *messagesToForward)
methodDefs = messagesToForward.map {
|msg|
methodDefs = messagesToForward.map do |msg|
"def #{msg}; #{forwarder}(:#{msg}); end"
}
end
module_eval(methodDefs.join("\n"))
end
end

View File

@ -37,12 +37,14 @@ module Zip
@comment ? @comment.length : 0 ].pack('VvvvvVVv')
io << @comment
end
private :write_e_o_c_d
def cdir_size #:nodoc:
# does not include eocd
@entrySet.inject(0) { |value, entry| entry.cdir_header_size + value }
end
private :cdir_size
def read_e_o_c_d(io) #:nodoc:

View File

@ -84,7 +84,7 @@ module Zip
compression_method = ZipEntry::DEFLATED, size = 0,
time = Time.now)
super()
if name.starts_with("/")
if name.start_with?("/")
raise ZipEntryNameError, "Illegal ZipEntry name '#{name}', name must not start with /"
end
@localHeaderOffset = 0

View File

@ -588,7 +588,7 @@ module Zip
end
def expand_path(aPath)
expanded = aPath.starts_with("/") ? aPath : @pwd.ensure_end("/") + aPath
expanded = aPath.start_with?("/") ? aPath : @pwd.ensure_end("/") + aPath
expanded.gsub!(/\/\.(\/|$)/, "")
expanded.gsub!(/[^\/]+\/\.\.(\/|$)/, "")
expanded.empty? ? "/" : expanded

View File

@ -17,28 +17,6 @@ end
class StringExtensionsTest < Test::Unit::TestCase
def test_starts_with
assert("hello".starts_with(""))
assert("hello".starts_with("h"))
assert("hello".starts_with("he"))
assert(! "hello".starts_with("hello there"))
assert(! "hello".starts_with(" he"))
assert_raise(TypeError, "type mismatch: NilClass given") {
"hello".starts_with(nil)
}
end
def test_ends_with
assert("hello".ends_with("o"))
assert("hello".ends_with("lo"))
assert("hello".ends_with("hello"))
assert(!"howdy".ends_with("o"))
assert(!"howdy".ends_with("oy"))
assert(!"howdy".ends_with("howdy doody"))
assert(!"howdy".ends_with("doody howdy"))
end
def test_ensure_end
assert_equal("hello!", "hello!".ensure_end("!"))
assert_equal("hello!", "hello!".ensure_end("o!"))