Skip to content

Commit

Permalink
Merge pull request fluent#4210 from Garfield96/refactoring-2
Browse files Browse the repository at this point in the history
Refactoring: Improve string processing
  • Loading branch information
ashie committed Jun 27, 2023
2 parents badef56 + 6047e1c commit 77d7229
Show file tree
Hide file tree
Showing 19 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions lib/fluent/command/ctl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def usage(msg = nil)

def call
if Fluent.windows?
if @pid_or_svcname =~ /^[0-9]+$/
if /^[0-9]+$/.match?(@pid_or_svcname)
# Use as PID
return call_windows_event(@command, "fluentd_#{@pid_or_svcname}")
end
Expand Down Expand Up @@ -172,7 +172,7 @@ def parse_options!
usage("PID or SVCNAME isn't specified!") if @pid_or_svcname.nil? || @pid_or_svcname.empty?
else
usage("PID isn't specified!") if @pid_or_svcname.nil? || @pid_or_svcname.empty?
usage("Invalid PID: #{pid}") unless @pid_or_svcname =~ /^[0-9]+$/
usage("Invalid PID: #{pid}") unless /^[0-9]+$/.match?(@pid_or_svcname)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/command/plugin_config_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def call
@plugin.class.ancestors.reverse_each do |plugin_class|
next unless plugin_class.respond_to?(:dump_config_definition)
unless @verbose
next if plugin_class.name =~ /::PluginHelper::/
next if /::PluginHelper::/.match?(plugin_class.name)
end
dumped_config_definition = plugin_class.dump_config_definition
dumped_config[plugin_class.name] = dumped_config_definition unless dumped_config_definition.empty?
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/config/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def method_missing(name, *args, &block)

def include(*args)
::Kernel.raise ::ArgumentError, "#{name} block requires arguments for include path" if args.nil? || args.size != 1
if args.first =~ /\.rb$/
if /\.rb$/.match?(args.first)
path = File.expand_path(args.first)
data = File.read(path)
self.instance_eval(data, path)
Expand Down
4 changes: 2 additions & 2 deletions lib/fluent/config/v1_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ def parse_include(attrs, elems)
def eval_include(attrs, elems, uri)
# replace space(s)(' ') with '+' to prevent invalid uri due to space(s).
# See: https://github.com/fluent/fluentd/pull/2780#issuecomment-576081212
u = URI.parse(uri.gsub(/ /, '+'))
if u.scheme == 'file' || (!u.scheme.nil? && u.scheme.length == 1) || u.path == uri.gsub(/ /, '+') # file path
u = URI.parse(uri.tr(' ', '+'))
if u.scheme == 'file' || (!u.scheme.nil? && u.scheme.length == 1) || u.path == uri.tr(' ', '+') # file path
# When the Windows absolute path then u.scheme.length == 1
# e.g. C:
path = URI.decode_www_form_component(u.path)
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/counter/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Server
DEFAULT_PORT = 24321

def initialize(name, opt = {})
raise 'Counter server name is invalid' unless Validator::VALID_NAME =~ name
raise 'Counter server name is invalid' unless Validator::VALID_NAME.match?(name)
@name = name
@opt = opt
@addr = @opt[:addr] || DEFAULT_ADDR
Expand Down
6 changes: 3 additions & 3 deletions lib/fluent/counter/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def validate_key!(name)
raise Fluent::Counter::InvalidParams.new('The type of `key` should be String')
end

unless VALID_NAME =~ name
unless VALID_NAME.match?(name)
raise Fluent::Counter::InvalidParams.new('`key` is the invalid format')
end
end
Expand All @@ -92,7 +92,7 @@ def validate_scope!(name)
raise Fluent::Counter::InvalidParams.new('The type of `scope` should be String')
end

unless VALID_SCOPE_NAME =~ name
unless VALID_SCOPE_NAME.match?(name)
raise Fluent::Counter::InvalidParams.new('`scope` is the invalid format')
end
end
Expand All @@ -109,7 +109,7 @@ def validate_name!(hash)
raise Fluent::Counter::InvalidParams.new('The type of `name` should be String')
end

unless VALID_NAME =~ name
unless VALID_NAME.match?(name)
raise Fluent::Counter::InvalidParams.new("`name` is the invalid format")
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def log
end

def parse_config(io, fname, basepath = Dir.pwd, v1_config = false)
if fname =~ /\.rb$/
if /\.rb$/.match?(fname)
require 'fluent/config/dsl'
Config::DSL::Parser.parse(io, File.join(basepath, fname))
else
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def initialize(pat)
stack.last << regex.pop
regex.push ''

elsif c =~ /[a-zA-Z0-9_]/
elsif /[a-zA-Z0-9_]/.match?(c)
regex.last << c

else
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def called_in_test?
# Thread::Backtrace::Location#path returns base filename or absolute path.
# #absolute_path returns absolute_path always.
# https://bugs.ruby-lang.org/issues/12159
if location.absolute_path =~ /\/test_[^\/]+\.rb$/ # location.path =~ /test_.+\.rb$/
if /\/test_[^\/]+\.rb$/.match?(location.absolute_path) # location.path =~ /test_.+\.rb$/
return true
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin/filter_record_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def expand(__str_to_eval__, tag, time, record, tag_parts, tag_prefix, tag_suffix
end

(Object.instance_methods).each do |m|
undef_method m unless m.to_s =~ /^__|respond_to_missing\?|object_id|public_methods|instance_eval|method_missing|define_singleton_method|respond_to\?|new_ostruct_member|^class$/
undef_method m unless /^__|respond_to_missing\?|object_id|public_methods|instance_eval|method_missing|define_singleton_method|respond_to\?|new_ostruct_member|^class$/.match?(m.to_s)
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions lib/fluent/plugin/in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def on_headers_complete(headers)
@content_type = ""
@content_encoding = ""
headers.each_pair {|k,v|
@env["HTTP_#{k.gsub('-','_').upcase}"] = v
@env["HTTP_#{k.tr('-','_').upcase}"] = v
case k
when /\AExpect\z/i
expect = v
Expand All @@ -439,9 +439,9 @@ def on_headers_complete(headers)
when /\AContent-Encoding\Z/i
@content_encoding = v
when /\AConnection\Z/i
if v =~ /close/i
if /close/i.match?(v)
@keep_alive = false
elsif v =~ /Keep-alive/i
elsif /Keep-alive/i.match?(v)
@keep_alive = true
end
when /\AOrigin\Z/i
Expand Down Expand Up @@ -566,16 +566,16 @@ def on_message_complete

if @format_name != 'default'
params[EVENT_RECORD_PARAMETER] = @body
elsif @content_type =~ /^application\/x-www-form-urlencoded/
elsif /^application\/x-www-form-urlencoded/.match?(@content_type)
params.update WEBrick::HTTPUtils.parse_query(@body)
elsif @content_type =~ /^multipart\/form-data; boundary=(.+)/
boundary = WEBrick::HTTPUtils.dequote($1)
params.update WEBrick::HTTPUtils.parse_form_data(@body, boundary)
elsif @content_type =~ /^application\/json/
elsif /^application\/json/.match?(@content_type)
params['json'] = @body
elsif @content_type =~ /^application\/msgpack/
elsif /^application\/msgpack/.match?(@content_type)
params['msgpack'] = @body
elsif @content_type =~ /^application\/x-ndjson/
elsif /^application\/x-ndjson/.match?(@content_type)
params['ndjson'] = @body
end
path_info = uri.path
Expand All @@ -585,7 +585,7 @@ def on_message_complete
query_params = WEBrick::HTTPUtils.parse_query(uri.query)

query_params.each_pair {|k,v|
params["QUERY_#{k.gsub('-','_').upcase}"] = v
params["QUERY_#{k.tr('-','_').upcase}"] = v
}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin/in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ def initialize(target_info, pe, log, read_from_head, follow_inodes, update_watch
attr_accessor :group_watcher

def tag
@parsed_tag ||= @path.tr('/', '.').gsub(/\.+/, '.').gsub(/^\./, '')
@parsed_tag ||= @path.tr('/', '.').squeeze('.').gsub(/^\./, '')
end

def register_watcher(watcher)
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin/out_exec_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def configure(conf)
0
elsif (@child_respawn == 'inf') || (@child_respawn == '-1')
-1
elsif @child_respawn =~ /^\d+$/
elsif /^\d+$/.match?(@child_respawn)
@child_respawn.to_i
else
raise ConfigError, "child_respawn option argument invalid: none(or 0), inf(or -1) or positive number"
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ def extract_placeholders(str, chunk)
if str.include?('${tag}')
rvalue = rvalue.gsub('${tag}', metadata.tag)
end
if str =~ CHUNK_TAG_PLACEHOLDER_PATTERN
if CHUNK_TAG_PLACEHOLDER_PATTERN.match?(str)
hash = {}
tag_parts = metadata.tag.split('.')
tag_parts.each_with_index do |part, i|
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin/parser_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def configure_json_parser(name)
rescue LoadError => ex
name = :yajl
if log
if /\boj\z/ =~ ex.message
if /\boj\z/.match?(ex.message)
log.info "Oj is not installed, and failing back to Yajl for json parser"
else
log.warn ex.message
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin_helper/record_accessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def self.parse_dot_notation(param)
def self.validate_dot_keys(keys)
keys.each { |key|
next unless key.is_a?(String)
if /\s+/.match(key)
if /\s+/.match?(key)
raise Fluent::ConfigError, "whitespace character is not allowed in dot notation. Use bracket notation: #{key}"
end
}
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def plugin_id_for_test?
# Thread::Backtrace::Location#path returns base filename or absolute path.
# #absolute_path returns absolute_path always.
# https://bugs.ruby-lang.org/issues/12159
if location.absolute_path =~ /\/test_[^\/]+\.rb$/ # location.path =~ /test_.+\.rb$/
if /\/test_[^\/]+\.rb$/.match?(location.absolute_path) # location.path =~ /test_.+\.rb$/
return true
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/plugin/test_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class FluentPluginBaseTest::DummyPlugin2 < Fluent::Plugin::TestBase
@p.extend m
assert_equal [], logger.logs

ret = @p.string_safe_encoding("abc\xff.\x01f"){|s| s.split(/\./) }
ret = @p.string_safe_encoding("abc\xff.\x01f"){|s| s.split(".") }
assert_equal ['abc?', "\u0001f"], ret
assert_equal 1, logger.logs.size
assert{ logger.logs.first.include?("invalid byte sequence is replaced in ") }
Expand Down
2 changes: 1 addition & 1 deletion test/plugin_helper/test_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ def assert_certificate(cert, expected_extensions)
# OpenSSL 1.1.1: "TLSv1.2"
if tls_version == "TLSv1/SSLv3" || tls_version == "TLSv1.2"
matched = true
unless cipher_name.match(/#{conf.ciphers}/)
unless cipher_name.match?(/#{conf.ciphers}/)
matched = false
break
end
Expand Down

0 comments on commit 77d7229

Please sign in to comment.