Module: BitClust::NameUtils

Overview

Utilities for conversion among names, ids and URL fragments.

Constant Summary collapse

LIBNAME_RE =
%r<[\w\-]+(/[\w\-]+)*>
CONST_RE =
/[A-Z]\w*/
CONST_PATH_RE =
/#{CONST_RE}(?:::#{CONST_RE})*/
CLASS_NAME_RE =
/(?:#{CONST_RE}(?:::compatible)?|fatal|ARGF.class|main)/
CLASS_PATH_RE =
/(?:#{CONST_PATH_RE}(?:::compatible)?|fatal|ARGF.class|main)/
METHOD_NAME_RE =
/\w+[?!=]?|===|==|=~|<=>|<=|>=|!=|!~|[email protected]|!|\[\]=|\[\]|\*\*|>>|<<|\[email protected]|\[email protected]|[~+\-*\/%&|^<>`]/
TYPEMARK_RE =
/(?:\.|\#|\.\#|::|\$)/
METHOD_SPEC_RE =
/#{CLASS_PATH_RE}#{TYPEMARK_RE}#{METHOD_NAME_RE}/
GVAR_RE =
/\$(?:\w+|-.|\S)/
MID =
/\A#{METHOD_NAME_RE}\z/
NAME_TO_MARK =
{
  :singleton_method => '.',
  :instance_method  => '#',
  :module_function  => '.#',
  :constant         => '::',
  :special_variable => '$'
}
MARK_TO_NAME =
NAME_TO_MARK.invert
NAME_TO_CHAR =
{
  :singleton_method => 's',
  :instance_method  => 'i',
  :module_function  => 'm',
  :constant         => 'c',
  :special_variable => 'v'
}
CHAR_TO_NAME =
NAME_TO_CHAR.invert
MARK_TO_CHAR =
{
  '.'  => 's',
  '#'  => 'i',
  '.#' => 'm',
  '::' => 'c',
  '$'  => 'v'
}
CHAR_TO_MARK =
MARK_TO_CHAR.invert

Class Method Summary collapse

Class Method Details

.build_method_id(libid, cid, t, name) ⇒ Object


119
120
121
# File 'lib/bitclust/nameutils.rb', line 119

def build_method_id(libid, cid, t, name)
  "#{cid}/#{typename2char(t)}.#{encodename_url(name)}.#{libid}"
end

.classid2name(id) ⇒ Object


49
50
51
# File 'lib/bitclust/nameutils.rb', line 49

def classid2name(id)
  id.gsub(/=/, '::')
end

.classname2id(name) ⇒ Object


45
46
47
# File 'lib/bitclust/nameutils.rb', line 45

def classname2id(name)
  name.gsub(/::/, '=')
end

.classname?(str) ⇒ Boolean

Returns:

  • (Boolean)

41
42
43
# File 'lib/bitclust/nameutils.rb', line 41

def classname?(str)
  (/\A#{CLASS_PATH_RE}\z/o =~ str) ? true : false
end

.decodeid(str) ⇒ Object

encoded string -> case-sensitive ID (decode only [A-Z])


229
230
231
# File 'lib/bitclust/nameutils.rb', line 229

def decodeid(str)
  str.gsub(/-[a-z]/ni) {|s| s[1,1].upcase }
end

.decodename_fs(str) ⇒ Object


239
240
241
242
243
# File 'lib/bitclust/nameutils.rb', line 239

def decodename_fs(str)
  str.gsub(/=[\da-h]{2}|-[a-z]/ni) {|s|
    (/\A-/ =~ s) ? s[1,1].upcase : s[1,2].hex.chr
  }
end

.decodename_url(str) ⇒ Object

case-sensitive ID -> string


211
212
213
# File 'lib/bitclust/nameutils.rb', line 211

def decodename_url(str)
  str.gsub(/=[\da-h]{2}/ni) {|s| s[1,2].hex.chr }
end

.encodeid(str) ⇒ Object

case-sensitive ID -> encoded string (encode only [A-Z])


224
225
226
# File 'lib/bitclust/nameutils.rb', line 224

def encodeid(str)
  str.gsub(/[A-Z]/n) {|ch| "-#{ch}" }.downcase
end

.encodename_fs(str) ⇒ Object


233
234
235
236
237
# File 'lib/bitclust/nameutils.rb', line 233

def encodename_fs(str)
  str.gsub(/[^a-z0-9_]/n) {|ch|
    (/[A-Z]/n =~ ch) ? "-#{ch}" : sprintf('=%02x', ch[0].ord)
  }.downcase
end

.encodename_rdocurl(str) ⇒ Object

string -> encoded string in a rdoc way


216
217
218
219
220
221
# File 'lib/bitclust/nameutils.rb', line 216

def encodename_rdocurl(str)
  str = str.gsub(/[^A-Za-z0-9_.]/n) {|ch|
    sprintf('-%02X', ch[0].ord)
  }
  str.sub(/\A-/, '')
end

.encodename_url(str) ⇒ Object

string -> case-sensitive ID


206
207
208
# File 'lib/bitclust/nameutils.rb', line 206

def encodename_url(str)
  str.gsub(/[^A-Za-z0-9_]/n) {|ch| sprintf('=%02x', ch[0].ord) }
end

.functionname?(n) ⇒ Boolean

Returns:

  • (Boolean)

201
202
203
# File 'lib/bitclust/nameutils.rb', line 201

def functionname?(n)
  /\A\w+\z/ =~ n ? true : false
end

.gvarname?(str) ⇒ Boolean

Returns:

  • (Boolean)

109
110
111
# File 'lib/bitclust/nameutils.rb', line 109

def gvarname?(str)
  GVAR_RE =~ str ? true : false
end

.html_filename(basename, suffix) ⇒ Object


245
246
247
# File 'lib/bitclust/nameutils.rb', line 245

def html_filename(basename, suffix)
  "#{basename}#{suffix}"
end

.libid2name(id) ⇒ Object


37
38
39
# File 'lib/bitclust/nameutils.rb', line 37

def libid2name(id)
  id.split('.').map {|ent| decodename_url(ent) }.join('/')
end

.libname2id(name) ⇒ Object


33
34
35
# File 'lib/bitclust/nameutils.rb', line 33

def libname2id(name)
  name.split('/').map {|ent| encodename_url(ent) }.join('.')
end

.libname?(str) ⇒ Boolean

Returns:

  • (Boolean)

29
30
31
# File 'lib/bitclust/nameutils.rb', line 29

def libname?(str)
  (/\A#{LIBNAME_RE}\z/o =~ str) ? true : false
end

.method_spec?(str) ⇒ Boolean

Returns:

  • (Boolean)

53
54
55
56
# File 'lib/bitclust/nameutils.rb', line 53

def method_spec?(str)
  return false if str == "ARGF.class"
  (/\A#{METHOD_SPEC_RE}\z/o =~ str) ? true : false
end

.methodid2classid(id) ⇒ Object


84
85
86
87
# File 'lib/bitclust/nameutils.rb', line 84

def methodid2classid(id)
  c, t, m, lib = *split_method_id(id)
  c
end

.methodid2libid(id) ⇒ Object


79
80
81
82
# File 'lib/bitclust/nameutils.rb', line 79

def methodid2libid(id)
  c, t, m, lib = *split_method_id(id)
  lib
end

.methodid2mname(id) ⇒ Object


104
105
106
107
# File 'lib/bitclust/nameutils.rb', line 104

def methodid2mname(id)
  c, t, m, lib = *split_method_id(id)
  decodename_url(m)
end

.methodid2specparts(id) ⇒ Object


74
75
76
77
# File 'lib/bitclust/nameutils.rb', line 74

def methodid2specparts(id)
  c, t, m, lib = *split_method_id(id)
  return classid2name(c), typechar2mark(t), decodename_url(m), libid2name(lib)
end

.methodid2specstring(id) ⇒ Object


69
70
71
72
# File 'lib/bitclust/nameutils.rb', line 69

def methodid2specstring(id)
  c, t, m, lib = *split_method_id(id)
  classid2name(c) + typechar2mark(t) + decodename_url(m)
end

.methodid2typechar(id) ⇒ Object


89
90
91
92
# File 'lib/bitclust/nameutils.rb', line 89

def methodid2typechar(id)
  c, t, m, lib = *split_method_id(id)
  t
end

.methodid2typemark(id) ⇒ Object


99
100
101
102
# File 'lib/bitclust/nameutils.rb', line 99

def methodid2typemark(id)
  c, t, m, lib = *split_method_id(id)
  typechar2mark(t)
end

.methodid2typename(id) ⇒ Object


94
95
96
97
# File 'lib/bitclust/nameutils.rb', line 94

def methodid2typename(id)
  c, t, m, lib = *split_method_id(id)
  typechar2name(t)
end

.methodname?(str) ⇒ Boolean

Returns:

  • (Boolean)

115
116
117
# File 'lib/bitclust/nameutils.rb', line 115

def methodname?(str)
  (MID =~ str) ? true : false
end

.split_method_id(id) ⇒ Object

private module function


124
125
126
127
# File 'lib/bitclust/nameutils.rb', line 124

def split_method_id(id)
  c, rest = id.split("/")
  return *[c, *rest.split(%r<[/\.]>, 3)]
end

.split_method_spec(spec) ⇒ Object


58
59
60
61
62
63
64
65
66
67
# File 'lib/bitclust/nameutils.rb', line 58

def split_method_spec(spec)
  case spec
  when /\AKernel\$/
    return 'Kernel', '$', $'
  else
    m = /\A(#{CLASS_PATH_RE})(#{TYPEMARK_RE})(#{METHOD_NAME_RE})\z/o.match(spec) or
        raise ArgumentError, "wrong method spec: #{spec.inspect}"
    return *m.captures
  end
end

.typechar2mark(char) ⇒ Object


191
192
193
194
# File 'lib/bitclust/nameutils.rb', line 191

def typechar2mark(char)
  CHAR_TO_MARK[char] or
      raise "must not happen: #{char.inspect}"
end

.typechar2name(char) ⇒ Object


172
173
174
175
# File 'lib/bitclust/nameutils.rb', line 172

def typechar2name(char)
  CHAR_TO_NAME[char] or
      raise "must not happen: #{char.inspect}"
end

.typechar?(c) ⇒ Boolean

Returns:

  • (Boolean)

163
164
165
# File 'lib/bitclust/nameutils.rb', line 163

def typechar?(c)
  CHAR_TO_NAME.key?(c)
end

.typemark2char(mark) ⇒ Object


196
197
198
199
# File 'lib/bitclust/nameutils.rb', line 196

def typemark2char(mark)
  MARK_TO_CHAR[mark] or
      raise "must not happen: #{mark.inspect}"
end

.typemark2name(mark) ⇒ Object


148
149
150
151
# File 'lib/bitclust/nameutils.rb', line 148

def typemark2name(mark)
  MARK_TO_NAME[mark] or
      raise "must not happen: #{mark.inspect}"
end

.typemark?(m) ⇒ Boolean

Returns:

  • (Boolean)

187
188
189
# File 'lib/bitclust/nameutils.rb', line 187

def typemark?(m)
  MARK_TO_CHAR.key?(m)
end

.typename2char(name) ⇒ Object


167
168
169
170
# File 'lib/bitclust/nameutils.rb', line 167

def typename2char(name)
  NAME_TO_CHAR[name] or
      raise "must not happen: #{name.inspect}"
end

.typename2mark(name) ⇒ Object


143
144
145
146
# File 'lib/bitclust/nameutils.rb', line 143

def typename2mark(name)
  NAME_TO_MARK[name] or
      raise "must not happen: #{name.inspect}"
end

.typename?(n) ⇒ Boolean

Returns:

  • (Boolean)

139
140
141
# File 'lib/bitclust/nameutils.rb', line 139

def typename?(n)
  NAME_TO_MARK.key?(n)
end