Skip to content

Commit

Permalink
Refactor encdb and transdb templates
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
nobu committed Mar 17, 2024
1 parent e670892 commit 5fd6b46
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
16 changes: 8 additions & 8 deletions template/encdb.h.tmpl
Expand Up @@ -33,25 +33,25 @@ encdirs << 'enc' if encdirs.empty?
files = {}
encdirs.each do |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
}.each do |fn|
next if files[fn]
files[fn] = true
open(File.join(encdir,fn)) do |f|
File.open(File.join(encdir, fn)) do |f|
name = nil
f.each_line do |line|
if (/^#ifndef RUBY/ =~ line)..(/^#endif/ =~ line)
elsif (/^OnigEncodingDefine/ =~ line)..(/"(.*?)"/ =~ line)
if $1
elsif /^OnigEncodingDefine/.match?(line)
if (n = f.gets("\n\};")[/"(.*?)"/, 1]) # swallow the initializer block
if name
lines << %[ENC_SET_BASE("#$1", "#{name}");]
lines << %[ENC_SET_BASE("#{n}", "#{name}");]
else
name = $1
name = n
end
check_duplication(defs, $1, fn, f.lineno)
check_duplication(defs, n, fn, f.lineno)
next if BUILTIN_ENCODINGS[name]
encodings << $1
encodings << n
count += 1
end
else
Expand Down
32 changes: 14 additions & 18 deletions template/transdb.h.tmpl
@@ -1,4 +1,4 @@
<%
<% #-*- mode: ruby -*-
#
# static const rb_transcoder
# rb_from_US_ASCII = {
Expand All @@ -22,38 +22,34 @@ transdirs = transdirs.sort_by {|td|

files = {}
names_t = []
converter_list = []
transdirs.each do |transdir|
names = Dir.entries(transdir)
names_t += names.map {|n| /(?!\A)\.trans\z/ =~ n ? $` : nil }.compact
names_c = names.map {|n| /(?!\A)\.c\z/ =~ n ? $` : nil }.compact
(names_t & names_c).map {|n|
"#{n}.c"
}.sort_by {|e|
names_t += names.map {|n| n[/.+(?=\.trans\z)/]}.compact
names_c = names.map {|n| n[/.+(?=\.c\z)/]}.compact
(names_t & names_c).sort_by {|e|
e.scan(/(\d+)|(\D+)/).map {|n,a| a||[n.size,n.to_i]}.flatten
}.each do |fn|
next if files[fn]
files[fn] = true
path = File.join(transdir,fn)
open(path) do |f|
path = File.join(transdir, "#{fn}.c")
File.open(path) do |f|
f.each_line do |line|
if (/^static const rb_transcoder/ =~ line)..(/"(.*?)"\s*,\s*"(.*?)"/ =~ line)
if $1 && $2
from_to = "%s to %s" % [$1, $2]
if (/^static const rb_transcoder/ =~ line)
if (/"(.*?)"\s*,\s*"(.*?)"/ =~ f.gets("\n\};")) # swallow the initializer block
from_to = [$1.freeze, $2.freeze].freeze
if converters[from_to]
raise ArgumentError, '%s:%d: transcode "%s" is already registered at %s:%d' %
[path, f.lineno, from_to, *converters[from_to].values_at(3, 4)]
raise ArgumentError,
'%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
converters[from_to] = [$1, $2, fn[0..-3], path, f.lineno]
converter_list << from_to
converters[from_to] = [fn, path, f.lineno]
end
end
end
end
end
end
end
converter_list.each do |from_to|
from, to, fn = *converters[from_to]
converters.each do |(from, to), (fn)|
%>rb_declare_transcoder("<%=from%>", "<%=to%>", "<%=fn%>");
% end

0 comments on commit 5fd6b46

Please sign in to comment.