summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <[email protected]>2024-03-17 19:09:37 +0900
committerNobuyoshi Nakada <[email protected]>2024-03-17 19:09:37 +0900
commit5fd6b461c777654493c32c03c19f0b75362b966f (patch)
treeb4464088a542ab5af156ac83ec3d5b83ca05b5fd
parente670892497f2eed07983617104ea8d38da2706b1 (diff)
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.
-rw-r--r--template/encdb.h.tmpl16
-rw-r--r--template/transdb.h.tmpl32
2 files changed, 22 insertions, 26 deletions
diff --git a/template/encdb.h.tmpl b/template/encdb.h.tmpl
index 81d27afa1b..fe6af95747 100644
--- a/template/encdb.h.tmpl
+++ b/template/encdb.h.tmpl
@@ -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
diff --git a/template/transdb.h.tmpl b/template/transdb.h.tmpl
index 6ba159ea47..22b5960cd8 100644
--- a/template/transdb.h.tmpl
+++ b/template/transdb.h.tmpl
@@ -1,4 +1,4 @@
-<%
+<% #-*- mode: ruby -*-
#
# static const rb_transcoder
# rb_from_US_ASCII = {
@@ -22,30 +22,27 @@ 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
@@ -53,7 +50,6 @@ transdirs.each do |transdir|
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