Refactor encdb and transdb templates
- Simplify globbed file names. - Prefer `File.open` over `Kernel#open`. - Swallow initializer blocks instead of line by line with flip-flop. - Re-structure converter list.
This commit is contained in:
parent
e670892497
commit
5fd6b461c7
@ -33,25 +33,25 @@ encdirs << 'enc' if encdirs.empty?
|
|||||||
files = {}
|
files = {}
|
||||||
encdirs.each do |encdir|
|
encdirs.each do |encdir|
|
||||||
next unless File.directory?(encdir)
|
next unless File.directory?(encdir)
|
||||||
Dir.open(encdir) {|d| d.grep(/.+\.[ch]\z/)}.sort_by {|e|
|
Dir.glob("*.[ch]", base: encdir).sort_by {|e|
|
||||||
e.scan(/(\d+)|(\D+)/).map {|n,a| a||[n.size,n.to_i]}.flatten
|
e.scan(/(\d+)|(\D+)/).map {|n,a| a||[n.size,n.to_i]}.flatten
|
||||||
}.each do |fn|
|
}.each do |fn|
|
||||||
next if files[fn]
|
next if files[fn]
|
||||||
files[fn] = true
|
files[fn] = true
|
||||||
open(File.join(encdir,fn)) do |f|
|
File.open(File.join(encdir, fn)) do |f|
|
||||||
name = nil
|
name = nil
|
||||||
f.each_line do |line|
|
f.each_line do |line|
|
||||||
if (/^#ifndef RUBY/ =~ line)..(/^#endif/ =~ line)
|
if (/^#ifndef RUBY/ =~ line)..(/^#endif/ =~ line)
|
||||||
elsif (/^OnigEncodingDefine/ =~ line)..(/"(.*?)"/ =~ line)
|
elsif /^OnigEncodingDefine/.match?(line)
|
||||||
if $1
|
if (n = f.gets("\n\};")[/"(.*?)"/, 1]) # swallow the initializer block
|
||||||
if name
|
if name
|
||||||
lines << %[ENC_SET_BASE("#$1", "#{name}");]
|
lines << %[ENC_SET_BASE("#{n}", "#{name}");]
|
||||||
else
|
else
|
||||||
name = $1
|
name = n
|
||||||
end
|
end
|
||||||
check_duplication(defs, $1, fn, f.lineno)
|
check_duplication(defs, n, fn, f.lineno)
|
||||||
next if BUILTIN_ENCODINGS[name]
|
next if BUILTIN_ENCODINGS[name]
|
||||||
encodings << $1
|
encodings << n
|
||||||
count += 1
|
count += 1
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
<%
|
<% #-*- mode: ruby -*-
|
||||||
#
|
#
|
||||||
# static const rb_transcoder
|
# static const rb_transcoder
|
||||||
# rb_from_US_ASCII = {
|
# rb_from_US_ASCII = {
|
||||||
@ -22,30 +22,27 @@ transdirs = transdirs.sort_by {|td|
|
|||||||
|
|
||||||
files = {}
|
files = {}
|
||||||
names_t = []
|
names_t = []
|
||||||
converter_list = []
|
|
||||||
transdirs.each do |transdir|
|
transdirs.each do |transdir|
|
||||||
names = Dir.entries(transdir)
|
names = Dir.entries(transdir)
|
||||||
names_t += names.map {|n| /(?!\A)\.trans\z/ =~ n ? $` : nil }.compact
|
names_t += names.map {|n| n[/.+(?=\.trans\z)/]}.compact
|
||||||
names_c = names.map {|n| /(?!\A)\.c\z/ =~ n ? $` : nil }.compact
|
names_c = names.map {|n| n[/.+(?=\.c\z)/]}.compact
|
||||||
(names_t & names_c).map {|n|
|
(names_t & names_c).sort_by {|e|
|
||||||
"#{n}.c"
|
|
||||||
}.sort_by {|e|
|
|
||||||
e.scan(/(\d+)|(\D+)/).map {|n,a| a||[n.size,n.to_i]}.flatten
|
e.scan(/(\d+)|(\D+)/).map {|n,a| a||[n.size,n.to_i]}.flatten
|
||||||
}.each do |fn|
|
}.each do |fn|
|
||||||
next if files[fn]
|
next if files[fn]
|
||||||
files[fn] = true
|
files[fn] = true
|
||||||
path = File.join(transdir,fn)
|
path = File.join(transdir, "#{fn}.c")
|
||||||
open(path) do |f|
|
File.open(path) do |f|
|
||||||
f.each_line do |line|
|
f.each_line do |line|
|
||||||
if (/^static const rb_transcoder/ =~ line)..(/"(.*?)"\s*,\s*"(.*?)"/ =~ line)
|
if (/^static const rb_transcoder/ =~ line)
|
||||||
if $1 && $2
|
if (/"(.*?)"\s*,\s*"(.*?)"/ =~ f.gets("\n\};")) # swallow the initializer block
|
||||||
from_to = "%s to %s" % [$1, $2]
|
from_to = [$1.freeze, $2.freeze].freeze
|
||||||
if converters[from_to]
|
if converters[from_to]
|
||||||
raise ArgumentError, '%s:%d: transcode "%s" is already registered at %s:%d' %
|
raise ArgumentError,
|
||||||
[path, f.lineno, from_to, *converters[from_to].values_at(3, 4)]
|
'%s:%d: transcode "%s to %s" is already registered at %s:%d' %
|
||||||
|
[path, f.lineno, *from_to, *converters[from_to].values_at(3, 4)]
|
||||||
else
|
else
|
||||||
converters[from_to] = [$1, $2, fn[0..-3], path, f.lineno]
|
converters[from_to] = [fn, path, f.lineno]
|
||||||
converter_list << from_to
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -53,7 +50,6 @@ transdirs.each do |transdir|
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
converter_list.each do |from_to|
|
converters.each do |(from, to), (fn)|
|
||||||
from, to, fn = *converters[from_to]
|
|
||||||
%>rb_declare_transcoder("<%=from%>", "<%=to%>", "<%=fn%>");
|
%>rb_declare_transcoder("<%=from%>", "<%=to%>", "<%=fn%>");
|
||||||
% end
|
% end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user