From c1e912caf966bc29b346c193f1b74e23e0f66781 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Thu, 9 Jul 2015 08:45:46 -0700 Subject: remaining autofixable cops --- lib/ffi_yajl/benchmark/encode.rb | 16 +++++++------- lib/ffi_yajl/benchmark/parse.rb | 17 +++++++------- lib/ffi_yajl/encoder.rb | 2 +- lib/ffi_yajl/ffi.rb | 22 +++++++++--------- lib/ffi_yajl/ffi/encoder.rb | 8 +++---- lib/ffi_yajl/ffi/parser.rb | 48 +++++++++++++++++++--------------------- lib/ffi_yajl/parser.rb | 8 +++---- 7 files changed, 57 insertions(+), 64 deletions(-) (limited to 'lib') diff --git a/lib/ffi_yajl/benchmark/encode.rb b/lib/ffi_yajl/benchmark/encode.rb index 5b7dbb1..eda4fe7 100644 --- a/lib/ffi_yajl/benchmark/encode.rb +++ b/lib/ffi_yajl/benchmark/encode.rb @@ -43,7 +43,7 @@ module FFI_Yajl class Benchmark class Encode def run - #filename = ARGV[0] || 'benchmark/subjects/ohai.json' + # filename = ARGV[0] || 'benchmark/subjects/ohai.json' filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json")) hash = File.open(filename, 'rb') { |f| FFI_Yajl::Parser.parse(f.read) } @@ -123,13 +123,13 @@ module FFI_Yajl } end end -# if defined?(ActiveSupport::JSON) -# x.report("ActiveSupport::JSON.encode") { -# times.times { -# ActiveSupport::JSON.encode(hash) -# } -# } -# end + # if defined?(ActiveSupport::JSON) + # x.report("ActiveSupport::JSON.encode") { + # times.times { + # ActiveSupport::JSON.encode(hash) + # } + # } + # end } end end diff --git a/lib/ffi_yajl/benchmark/parse.rb b/lib/ffi_yajl/benchmark/parse.rb index ce22147..7e1f3a6 100644 --- a/lib/ffi_yajl/benchmark/parse.rb +++ b/lib/ffi_yajl/benchmark/parse.rb @@ -48,14 +48,14 @@ class FFI_Yajl::Benchmark::Parse FFI_Yajl::Parser.parse(json_str) } } -# ffi_parser = FFI_Yajl::Parser.new -# x.report { -# puts "FFI_Yajl::Parser#parse (from a String)" -# times.times { -# json.rewind -# ffi_parser.parse(json.read) -# } -# } + # ffi_parser = FFI_Yajl::Parser.new + # x.report { + # puts "FFI_Yajl::Parser#parse (from a String)" + # times.times { + # json.rewind + # ffi_parser.parse(json.read) + # } + # } if defined?(Yajl::Parser) x.report { puts "Yajl::Parser.parse (from a String)" @@ -139,6 +139,5 @@ class FFI_Yajl::Benchmark::Parse end } json.close - end end diff --git a/lib/ffi_yajl/encoder.rb b/lib/ffi_yajl/encoder.rb index a678221..9f2e564 100644 --- a/lib/ffi_yajl/encoder.rb +++ b/lib/ffi_yajl/encoder.rb @@ -54,7 +54,7 @@ module FFI_Yajl @opts ||= {} end - def self.raise_error_for_status(status, token=nil) + def self.raise_error_for_status(status, token = nil) # scrub token to valid utf-8 since we may be issuing an exception on an invalid utf-8 token token = token.to_s.encode("utf-8", "binary", :undef => :replace) case status diff --git a/lib/ffi_yajl/ffi.rb b/lib/ffi_yajl/ffi.rb index 094c51d..3b83bae 100644 --- a/lib/ffi_yajl/ffi.rb +++ b/lib/ffi_yajl/ffi.rb @@ -61,16 +61,16 @@ module FFI_Yajl :yajl_status_error, ] -# FFI::Enums are slow, should remove the rest -# enum :yajl_gen_status, [ -# :yajl_gen_status_ok, -# :yajl_gen_keys_must_be_strings, -# :yajl_max_depth_exceeded, -# :yajl_gen_in_error_state, -# :yajl_gen_generation_complete, -# :yajl_gen_invalid_number, -# :yajl_gen_no_buf, -# ] + # FFI::Enums are slow, should remove the rest + # enum :yajl_gen_status, [ + # :yajl_gen_status_ok, + # :yajl_gen_keys_must_be_strings, + # :yajl_max_depth_exceeded, + # :yajl_gen_in_error_state, + # :yajl_gen_generation_complete, + # :yajl_gen_invalid_number, + # :yajl_gen_no_buf, + # ] enum :yajl_option, [ :yajl_allow_comments, 0x01, @@ -132,7 +132,7 @@ 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 diff --git a/lib/ffi_yajl/ffi/encoder.rb b/lib/ffi_yajl/ffi/encoder.rb index 94588d0..250ed68 100644 --- a/lib/ffi_yajl/ffi/encoder.rb +++ b/lib/ffi_yajl/ffi/encoder.rb @@ -27,7 +27,7 @@ module FFI_Yajl module FFI module Encoder def do_yajl_encode(obj, yajl_gen_opts, opts) - yajl_gen = FFI_Yajl.yajl_gen_alloc(nil); + yajl_gen = FFI_Yajl.yajl_gen_alloc(nil) # configure the yajl encoder if yajl_gen_opts[:yajl_gen_beautify] @@ -58,7 +58,7 @@ module FFI_Yajl FFI_Yajl.yajl_gen_free(yajl_gen) - return string + string end end end @@ -273,8 +273,6 @@ class Object str = to_s status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) end - if ( status ) != 0 - FFI_Yajl::Encoder.raise_error_for_status(status, str) - end + FFI_Yajl::Encoder.raise_error_for_status(status, str) if ( status ) != 0 end end diff --git a/lib/ffi_yajl/ffi/parser.rb b/lib/ffi_yajl/ffi/parser.rb index d0d152a..e3226c0 100644 --- a/lib/ffi_yajl/ffi/parser.rb +++ b/lib/ffi_yajl/ffi/parser.rb @@ -23,34 +23,32 @@ module FFI_Yajl module FFI module Parser - def set_value(val) - case stack.last - when Hash - raise FFI_Yajl::ParseError.new("internal error: missing key in parse") if key.nil? - if @opts[:unique_key_checking] && stack.last.has_key?(key) - raise FFI_Yajl::ParseError.new("repeated key: #{key}") - end - stack.last[key] = val - when Array - stack.last.push(val) - else - stack.push(val) + def set_value(val) + case stack.last + when Hash + raise FFI_Yajl::ParseError.new("internal error: missing key in parse") if key.nil? + if @opts[:unique_key_checking] && stack.last.key?(key) + raise FFI_Yajl::ParseError.new("repeated key: #{key}") end + stack.last[key] = val + when Array + stack.last.push(val) + else + stack.push(val) end + end - def stack_pop - if stack.length > 1 - set_value( stack.pop ) - end - end + def stack_pop + set_value( stack.pop ) if stack.length > 1 + end - def key_push - key_stack.push(key) - end + def key_push + key_stack.push(key) + end - def key_pop - @key = key_stack.pop - end + def key_pop + @key = key_stack.pop + end def setup_callbacks @null_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx| @@ -85,7 +83,7 @@ module FFI_Yajl end @start_map_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx| key_push # for key => { } case, save the key - stack.push(Hash.new) + stack.push({}) 1 end @map_key_callback = ::FFI::Function.new(:int, [:pointer, :string, :size_t]) do |ctx, key, keylen| @@ -101,7 +99,7 @@ module FFI_Yajl end @start_array_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx| key_push # for key => [ ] case, save the key - stack.push(Array.new) + stack.push([]) 1 end @end_array_callback = ::FFI::Function.new(:int, [:pointer]) do |ctx| diff --git a/lib/ffi_yajl/parser.rb b/lib/ffi_yajl/parser.rb index da9b704..6029691 100644 --- a/lib/ffi_yajl/parser.rb +++ b/lib/ffi_yajl/parser.rb @@ -32,14 +32,14 @@ module FFI_Yajl # stack used to build up our complex object # def stack - @stack ||= Array.new + @stack ||= [] end # # stack to keep track of keys as we create nested hashes # def key_stack - @key_stack ||= Array.new + @key_stack ||= [] end def self.parse(obj, *args) @@ -81,9 +81,7 @@ module FFI_Yajl # XXX: bug-compat with ruby-yajl return nil if str == "" - if str.respond_to?(:read) - str = str.read - end + str = str.read if str.respond_to?(:read) # call either the ext or ffi hook do_yajl_parse(str, yajl_opts) -- cgit v1.2.1