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:
Nobuyoshi Nakada 2024-03-17 19:09:37 +09:00
Родитель e670892497
Коммит 5fd6b461c7
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 3582D74E1FEE4465
2 изменённых файлов: 22 добавлений и 26 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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