optimize regex matchers

This commit is contained in:
pavel 2019-01-03 01:46:38 +01:00
parent 07d272daa4
commit dc6d9ec9e0
3 changed files with 8 additions and 6 deletions

View File

@ -49,7 +49,7 @@ class DmsfStateController < ApplicationController
private
def format_valid?(format)
format.blank? || ((format =~ /%(t|d|v|i|r)/) && format.length < 256)
format.blank? || ((/%(t|d|v|i|r)/.match?(format)) && format.length < 256)
end
end

View File

@ -48,11 +48,13 @@ module DmsfHelper
# get only the filename, not the whole path
just_filename = File.basename(filename.gsub('\\\\', '/'))
# replace all non alphanumeric, hyphens or periods with underscore
just_filename = just_filename.gsub(/[^\w\.\-]/,'_')
unless just_filename =~ %r{^[a-zA-Z0-9_\.\-]*$}
just_filename.gsub!(/[^\w\.\-]/, '_')
unless %r{^[a-zA-Z0-9_\.\-]*$}.match?(just_filename)
# keep the extension if any
extension = $1 if just_filename =~ %r{(\.[a-zA-Z0-9]+)$}
just_filename = Digest::SHA256.hexdigest(just_filename) << extension
if just_filename =~ %r{(\.[a-zA-Z0-9]+)$}
extension = $1
just_filename = Digest::SHA256.hexdigest(just_filename) << extension
end
end
just_filename
end

View File

@ -22,7 +22,7 @@
class DmsfFileNameValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value =~ /\A[^#{DmsfFolder::INVALID_CHARACTERS}]*\z/
unless /\A[^#{DmsfFolder::INVALID_CHARACTERS}]*\z/.match?(value)
record.errors.add attribute, :error_contains_invalid_character
end
end