From 8fabdd3fddbd222109976fddb0d38255a9490f1d Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Thu, 9 Jul 2015 08:39:40 -0700 Subject: fix lots of cops and add a few more to be ignored --- lib/ffi_yajl/benchmark.rb | 1 - lib/ffi_yajl/benchmark/encode.rb | 3 -- lib/ffi_yajl/benchmark/encode_profile.rb | 2 -- lib/ffi_yajl/benchmark/http.rb | 4 +-- lib/ffi_yajl/benchmark/parse.rb | 6 ++-- lib/ffi_yajl/benchmark/parse_json_and_marshal.rb | 4 +-- lib/ffi_yajl/benchmark/parse_json_and_yaml.rb | 4 +-- lib/ffi_yajl/benchmark/parse_profile.rb | 2 -- lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb | 2 -- lib/ffi_yajl/benchmark/parse_stream.rb | 4 +-- lib/ffi_yajl/ffi.rb | 27 ++++++++--------- lib/ffi_yajl/ffi/encoder.rb | 37 +++++++++++------------ lib/ffi_yajl/ffi/parser.rb | 11 +++---- lib/ffi_yajl/map_library_name.rb | 1 - lib/ffi_yajl/parser.rb | 2 +- 15 files changed, 46 insertions(+), 64 deletions(-) (limited to 'lib') diff --git a/lib/ffi_yajl/benchmark.rb b/lib/ffi_yajl/benchmark.rb index 6d50c0f..e8944ca 100644 --- a/lib/ffi_yajl/benchmark.rb +++ b/lib/ffi_yajl/benchmark.rb @@ -25,4 +25,3 @@ require 'ffi_yajl/benchmark/encode_profile.rb' require 'ffi_yajl/benchmark/parse.rb' require 'ffi_yajl/benchmark/parse_profile.rb' require 'ffi_yajl/benchmark/parse_profile_ruby_prof.rb' - diff --git a/lib/ffi_yajl/benchmark/encode.rb b/lib/ffi_yajl/benchmark/encode.rb index d3f969c..5b7dbb1 100644 --- a/lib/ffi_yajl/benchmark/encode.rb +++ b/lib/ffi_yajl/benchmark/encode.rb @@ -42,7 +42,6 @@ end module FFI_Yajl class Benchmark class Encode - def run #filename = ARGV[0] || 'benchmark/subjects/ohai.json' filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json")) @@ -51,7 +50,6 @@ module FFI_Yajl times = ARGV[1] ? ARGV[1].to_i : 1000 puts "Starting benchmark encoding #{filename} #{times} times\n\n" ::Benchmark.bmbm { |x| - x.report("FFI_Yajl::Encoder.encode (to a String)") { times.times { output = FFI_Yajl::Encoder.encode(hash) @@ -136,5 +134,4 @@ module FFI_Yajl end end end - end diff --git a/lib/ffi_yajl/benchmark/encode_profile.rb b/lib/ffi_yajl/benchmark/encode_profile.rb index ce98a3c..f7ac7c2 100644 --- a/lib/ffi_yajl/benchmark/encode_profile.rb +++ b/lib/ffi_yajl/benchmark/encode_profile.rb @@ -14,7 +14,6 @@ ENV['CPUPROFILE_FREQUENCY'] = "4000" module FFI_Yajl class Benchmark class EncodeProfile - def run if defined?(PerfTools) filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json")) @@ -32,7 +31,6 @@ module FFI_Yajl system("pprof.rb --text /tmp/ffi_yajl_encode_profile.out") end end - end end end diff --git a/lib/ffi_yajl/benchmark/http.rb b/lib/ffi_yajl/benchmark/http.rb index 551c987..4c61df5 100644 --- a/lib/ffi_yajl/benchmark/http.rb +++ b/lib/ffi_yajl/benchmark/http.rb @@ -15,7 +15,7 @@ uri = URI.parse('http://search.twitter.com/search.json?q=github') # uri = URI.parse('http://localhost/yajl-ruby.git/benchmark/subjects/contacts.json') times = ARGV[0] ? ARGV[0].to_i : 1 -puts "Starting benchmark parsing #{uri.to_s} #{times} times\n\n" +puts "Starting benchmark parsing #{uri} #{times} times\n\n" Benchmark.bmbm { |x| x.report { puts "Yajl::HttpStream.get" @@ -29,4 +29,4 @@ Benchmark.bmbm { |x| JSON.parse(Net::HTTP.get_response(uri).body, :max_nesting => false) } } -} \ No newline at end of file +} diff --git a/lib/ffi_yajl/benchmark/parse.rb b/lib/ffi_yajl/benchmark/parse.rb index 02ad740..ce22147 100644 --- a/lib/ffi_yajl/benchmark/parse.rb +++ b/lib/ffi_yajl/benchmark/parse.rb @@ -34,7 +34,6 @@ rescue LoadError end class FFI_Yajl::Benchmark::Parse - def run filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "item.json")) json = File.new(filename, 'r') @@ -65,7 +64,7 @@ class FFI_Yajl::Benchmark::Parse } } io_parser = Yajl::Parser.new - io_parser.on_parse_complete = lambda {|obj|} if times > 1 + io_parser.on_parse_complete = lambda { |obj| } if times > 1 x.report { puts "Yajl::Parser#parse (from an IO)" times.times { @@ -74,7 +73,7 @@ class FFI_Yajl::Benchmark::Parse } } string_parser = Yajl::Parser.new - string_parser.on_parse_complete = lambda {|obj|} if times > 1 + string_parser.on_parse_complete = lambda { |obj| } if times > 1 x.report { puts "Yajl::Parser#parse (from a String)" times.times { @@ -143,4 +142,3 @@ class FFI_Yajl::Benchmark::Parse end end - diff --git a/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb b/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb index 06fe70b..163f559 100644 --- a/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb +++ b/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb @@ -23,7 +23,7 @@ Benchmark.bmbm { |x| x.report { puts "Yajl::Parser#parse" yajl = Yajl::Parser.new - yajl.on_parse_complete = lambda {|obj|} if times > 1 + yajl.on_parse_complete = lambda { |obj| } if times > 1 times.times { json.rewind hash = yajl.parse(json) @@ -47,4 +47,4 @@ Benchmark.bmbm { |x| } } json.close -marshal_file.close \ No newline at end of file +marshal_file.close diff --git a/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb b/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb index 0cc509a..4e96e02 100644 --- a/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb +++ b/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb @@ -18,7 +18,7 @@ times = ARGV[0] ? ARGV[0].to_i : 1000 puts "Starting benchmark parsing #{File.size(filename)} bytes of JSON data #{times} times\n\n" Benchmark.bmbm { |x| parser = Yajl::Parser.new - parser.on_parse_complete = lambda {|obj|} if times > 1 + parser.on_parse_complete = lambda { |obj| } if times > 1 x.report { puts "Yajl::Parser#parse" times.times { @@ -52,4 +52,4 @@ Benchmark.bmbm { |x| } } } -yaml.close \ No newline at end of file +yaml.close diff --git a/lib/ffi_yajl/benchmark/parse_profile.rb b/lib/ffi_yajl/benchmark/parse_profile.rb index 5efb1e2..39343bc 100644 --- a/lib/ffi_yajl/benchmark/parse_profile.rb +++ b/lib/ffi_yajl/benchmark/parse_profile.rb @@ -14,7 +14,6 @@ ENV['CPUPROFILE_FREQUENCY'] = "4000" module FFI_Yajl class Benchmark class ParseProfile - def run if defined?(PerfTools) filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json")) @@ -31,7 +30,6 @@ module FFI_Yajl system("pprof.rb --text /tmp/ffi_yajl_encode_profile.out") end end - end end end diff --git a/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb b/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb index 5b9f73e..8d9dd5d 100644 --- a/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb +++ b/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb @@ -7,7 +7,6 @@ require 'ffi_yajl' module FFI_Yajl class Benchmark class ParseProfileRubyProf - def run begin require 'ruby-prof' @@ -33,7 +32,6 @@ module FFI_Yajl end end - end end end diff --git a/lib/ffi_yajl/benchmark/parse_stream.rb b/lib/ffi_yajl/benchmark/parse_stream.rb index 94c5ac3..955f22f 100644 --- a/lib/ffi_yajl/benchmark/parse_stream.rb +++ b/lib/ffi_yajl/benchmark/parse_stream.rb @@ -20,7 +20,7 @@ times = ARGV[0] ? ARGV[0].to_i : 100 puts "Starting benchmark parsing JSON stream (#{File.size(filename)} bytes of JSON data with 430 JSON separate strings) #{times} times\n\n" Benchmark.bmbm { |x| parser = Yajl::Parser.new - parser.on_parse_complete = lambda {|obj|} + parser.on_parse_complete = lambda { |obj| } x.report { puts "Yajl::Parser#parse" times.times { @@ -51,4 +51,4 @@ Benchmark.bmbm { |x| } end } -json.close \ No newline at end of file +json.close diff --git a/lib/ffi_yajl/ffi.rb b/lib/ffi_yajl/ffi.rb index c56ea10..094c51d 100644 --- a/lib/ffi_yajl/ffi.rb +++ b/lib/ffi_yajl/ffi.rb @@ -42,16 +42,16 @@ module FFI_Yajl class YajlCallbacks < ::FFI::Struct layout :yajl_null, :pointer, - :yajl_boolean, :pointer, - :yajl_integer, :pointer, - :yajl_double, :pointer, - :yajl_number, :pointer, - :yajl_string, :pointer, - :yajl_start_map, :pointer, - :yajl_map_key, :pointer, - :yajl_end_map, :pointer, - :yajl_start_array, :pointer, - :yajl_end_array, :pointer + :yajl_boolean, :pointer, + :yajl_integer, :pointer, + :yajl_double, :pointer, + :yajl_number, :pointer, + :yajl_string, :pointer, + :yajl_start_map, :pointer, + :yajl_map_key, :pointer, + :yajl_end_map, :pointer, + :yajl_start_array, :pointer, + :yajl_end_array, :pointer end enum :yajl_status, [ @@ -77,14 +77,14 @@ module FFI_Yajl :yajl_dont_validate_strings, 0x02, :yajl_allow_trailing_garbage, 0x04, :yajl_allow_multiple_values, 0x08, - :yajl_allow_partial_values, 0x10, + :yajl_allow_partial_values, 0x10 ] enum :yajl_gen_option, [ :yajl_gen_beautify, 0x01, :yajl_gen_indent_string, 0x02, :yajl_gen_print_callback, 0x04, - :yajl_gen_validate_utf8, 0x08, + :yajl_gen_validate_utf8, 0x08 ] typedef :pointer, :yajl_handle @@ -132,10 +132,9 @@ module FFI_Yajl attach_function :yajl_gen_array_open, [:yajl_gen], :int attach_function :yajl_gen_array_close, [:yajl_gen], :int # yajl_gen_status yajl_gen_get_buf (yajl_gen hand, const unsigned char **buf, unsigned int *len) - attach_function :yajl_gen_get_buf, [:yajl_gen, :pointer ,:pointer], :int + attach_function :yajl_gen_get_buf, [:yajl_gen, :pointer , :pointer], :int # void yajl_gen_clear (yajl_gen hand) attach_function :yajl_gen_clear, [:yajl_gen], :void - end require 'ffi_yajl/encoder' diff --git a/lib/ffi_yajl/ffi/encoder.rb b/lib/ffi_yajl/ffi/encoder.rb index 71b631e..94588d0 100644 --- a/lib/ffi_yajl/ffi/encoder.rb +++ b/lib/ffi_yajl/ffi/encoder.rb @@ -60,7 +60,6 @@ module FFI_Yajl return string end - end end end @@ -68,7 +67,7 @@ end class Hash def ffi_yajl(yajl_gen, state) if state[:processing_key] - str = self.to_s + str = to_s if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0 FFI_Yajl::Encoder.raise_error_for_status(status, str) end @@ -76,7 +75,7 @@ class Hash if ( status = FFI_Yajl.yajl_gen_map_open(yajl_gen) ) != 0 FFI_Yajl::Encoder.raise_error_for_status(status, '{') end - self.each do |key, value| + each do |key, value| # Perf Fix: mutate state hash rather than creating new copy state[:processing_key] = true key.ffi_yajl(yajl_gen, state) @@ -93,7 +92,7 @@ end class Array def ffi_yajl(yajl_gen, state) if state[:processing_key] - str = self.to_s + str = to_s if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0 FFI_Yajl::Encoder.raise_error_for_status(status, str) end @@ -101,7 +100,7 @@ class Array if ( status = FFI_Yajl.yajl_gen_array_open(yajl_gen) ) != 0 FFI_Yajl::Encoder.raise_error_for_status(status, '[') end - self.each do |value| + each do |value| value.ffi_yajl(yajl_gen, state) end if ( status = FFI_Yajl.yajl_gen_array_close(yajl_gen) ) != 0 @@ -113,7 +112,7 @@ end class NilClass def ffi_yajl(yajl_gen, state) - str = self.to_s + str = to_s if state[:processing_key] if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0 FFI_Yajl::Encoder.raise_error_for_status(status, str) @@ -128,7 +127,7 @@ end class TrueClass def ffi_yajl(yajl_gen, state) - str = self.to_s + str = to_s if state[:processing_key] if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0 FFI_Yajl::Encoder.raise_error_for_status(status, str) @@ -143,7 +142,7 @@ end class FalseClass def ffi_yajl(yajl_gen, state) - str = self.to_s + str = to_s if state[:processing_key] if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0 FFI_Yajl::Encoder.raise_error_for_status(status, str) @@ -158,7 +157,7 @@ end class Fixnum def ffi_yajl(yajl_gen, state) - str = self.to_s + str = to_s if str == "NaN" || str == "Infinity" || str == "-Infinity" raise ::FFI_Yajl::EncodeError.new("'#{str}' is an invalid number") end @@ -176,7 +175,7 @@ end class Bignum def ffi_yajl(yajl_gen, state) - str = self.to_s + str = to_s if str == "NaN" || str == "Infinity" || str == "-Infinity" raise ::FFI_Yajl::EncodeError.new("'#{str}' is an invalid number") end @@ -194,7 +193,7 @@ end class Float def ffi_yajl(yajl_gen, state) - str = self.to_s + str = to_s if str == "NaN" || str == "Infinity" || str == "-Infinity" raise ::FFI_Yajl::EncodeError.new("'#{str}' is an invalid number") end @@ -212,7 +211,7 @@ end class Symbol def ffi_yajl(yajl_gen, state) - str = self.to_s + str = to_s if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0 FFI_Yajl::Encoder.raise_error_for_status(status, str) end @@ -221,7 +220,7 @@ end class String def ffi_yajl(yajl_gen, state) - if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, self, self.bytesize) ) != 0 + if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, self, bytesize) ) != 0 FFI_Yajl::Encoder.raise_error_for_status(status, self) end end @@ -229,7 +228,7 @@ end class StringIO def ffi_yajl(yajl_gen, state) - str = self.read + str = read if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0 FFI_Yajl::Encoder.raise_error_for_status(status, str) end @@ -238,7 +237,7 @@ end class Date def ffi_yajl(yajl_gen, state) - str = self.to_s + str = to_s if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0 FFI_Yajl::Encoder.raise_error_for_status(status, str) end @@ -247,7 +246,7 @@ end class Time def ffi_yajl(yajl_gen, state) - str = self.strftime "%Y-%m-%d %H:%M:%S %z" + str = strftime "%Y-%m-%d %H:%M:%S %z" if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0 FFI_Yajl::Encoder.raise_error_for_status(status, str) end @@ -256,7 +255,7 @@ end class DateTime def ffi_yajl(yajl_gen, state) - str = self.to_s + str = to_s if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0 FFI_Yajl::Encoder.raise_error_for_status(status, str) end @@ -267,11 +266,11 @@ end class Object def ffi_yajl(yajl_gen, state) if !state[:processing_key] && self.respond_to?(:to_json) - str = self.to_json(state[:json_opts]) + str = to_json(state[:json_opts]) # #yajl_gen_number outputs a string without quotes around it status = FFI_Yajl.yajl_gen_number(yajl_gen, str, str.bytesize) else - str = self.to_s + str = to_s status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) end if ( status ) != 0 diff --git a/lib/ffi_yajl/ffi/parser.rb b/lib/ffi_yajl/ffi/parser.rb index 77d2389..d0d152a 100644 --- a/lib/ffi_yajl/ffi/parser.rb +++ b/lib/ffi_yajl/ffi/parser.rb @@ -23,7 +23,6 @@ module FFI_Yajl module FFI module Parser - def set_value(val) case stack.last when Hash @@ -50,10 +49,9 @@ module FFI_Yajl end def key_pop - @key = key_stack.pop() + @key = key_stack.pop end - def setup_callbacks @null_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx| set_value(nil) @@ -68,7 +66,7 @@ module FFI_Yajl 1 end @number_callback = ::FFI::Function.new(:int, [:pointer, :string, :size_t ]) do |ctx, stringval, stringlen| - s = stringval.slice(0,stringlen) + s = stringval.slice(0, stringlen) s.force_encoding('UTF-8') if defined? Encoding # XXX: I can't think of a better way to do this right now. need to call to_f if and only if its a float. v = ( s =~ /[\.eE]/ ) ? s.to_f : s.to_i @@ -80,7 +78,7 @@ module FFI_Yajl 1 end @string_callback = ::FFI::Function.new(:int, [:pointer, :string, :size_t]) do |ctx, stringval, stringlen| - s = stringval.slice(0,stringlen) + s = stringval.slice(0, stringlen) s.force_encoding('UTF-8') if defined? Encoding set_value(s) 1 @@ -91,7 +89,7 @@ module FFI_Yajl 1 end @map_key_callback = ::FFI::Function.new(:int, [:pointer, :string, :size_t]) do |ctx, key, keylen| - s = key.slice(0,keylen) + s = key.slice(0, keylen) s.force_encoding('UTF-8') if defined? Encoding self.key = @opts[:symbolize_keys] ? s.to_sym : s 1 @@ -113,7 +111,6 @@ module FFI_Yajl end end - def do_yajl_parse(str, yajl_opts = {}) setup_callbacks callback_ptr = ::FFI::MemoryPointer.new(::FFI_Yajl::YajlCallbacks) diff --git a/lib/ffi_yajl/map_library_name.rb b/lib/ffi_yajl/map_library_name.rb index 8723424..249116e 100644 --- a/lib/ffi_yajl/map_library_name.rb +++ b/lib/ffi_yajl/map_library_name.rb @@ -29,7 +29,6 @@ require 'libyajl2' module FFI_Yajl module MapLibraryName - private # Stub for tests to override the host_os diff --git a/lib/ffi_yajl/parser.rb b/lib/ffi_yajl/parser.rb index 9008a3e..da9b704 100644 --- a/lib/ffi_yajl/parser.rb +++ b/lib/ffi_yajl/parser.rb @@ -82,7 +82,7 @@ module FFI_Yajl return nil if str == "" if str.respond_to?(:read) - str = str.read() + str = str.read end # call either the ext or ffi hook -- cgit v1.2.1