Skip to content

Commit

Permalink
Util::Private::RUBY_REJECT_NAME_RE
Browse files Browse the repository at this point in the history
  • Loading branch information
notEthan committed May 30, 2023
1 parent 630a6f3 commit e4b1adb
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/jsi/util/private.rb
Expand Up @@ -42,14 +42,16 @@ module Util::Private
127..159, # C1 control chars
].inject(Set[], &:merge).freeze

RUBY_REJECT_NAME_RE = Regexp.new('[' + Regexp.escape(RUBY_REJECT_NAME_CODEPOINTS.to_a.pack('U*')) + ']').freeze

# is the given name ok to use as a ruby method name?
def ok_ruby_method_name?(name)
# must be a string
return false unless name.respond_to?(:to_str)
# must not begin with a digit
return false if name =~ /\A[0-9]/
# must not contain special or control characters
return false if name.each_codepoint.any? { |c| RUBY_REJECT_NAME_CODEPOINTS.include?(c) }
return false if name =~ RUBY_REJECT_NAME_RE

return true
end
Expand All @@ -59,7 +61,8 @@ def const_name_from_parts(parts)
part = part.dup
part[/\A[^a-zA-Z]*/] = ''
part[0] = part[0].upcase if part[0]
part.each_char.map { |c| RUBY_REJECT_NAME_CODEPOINTS.include?(c.ord) ? '_' : c }.join
part.gsub!(RUBY_REJECT_NAME_RE, '_')
part
end
if !parts.all?(&:empty?)
parts.join.freeze
Expand Down

0 comments on commit e4b1adb

Please sign in to comment.