[前][次][番号順一覧][スレッド一覧]

ruby-changes:62551

From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Tue, 11 Aug 2020 16:51:31 +0900 (JST)
Subject: [ruby-changes:62551] acd8ee8dbc (master): tool/prelude.c.tmpl: use RubyVM::CEscape

https://git.ruby-lang.org/ruby.git/commit/?id=acd8ee8dbc

From acd8ee8dbc0c0c5da8aa486db7dca169d8239196 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?=
 <shyouhei@r...>
Date: Tue, 11 Aug 2020 11:40:37 +0900
Subject: tool/prelude.c.tmpl: use RubyVM::CEscape

Do not repeat yourself.

diff --git a/template/prelude.c.tmpl b/template/prelude.c.tmpl
index a8b53d0..73d35fc 100644
--- a/template/prelude.c.tmpl
+++ b/template/prelude.c.tmpl
@@ -5,24 +5,12 @@ https://github.com/ruby/ruby/blob/trunk/template/prelude.c.tmpl#L5
 # Since $(BASERUBY) may be older than Ruby 1.9,
 # Ruby 1.9 feature should not be used.
 
+require_relative '../tool/ruby_vm/helpers/c_escape'
+
 class Prelude
+  include RubyVM::CEscape
   LINE_LIMIT = 509 # by C89
 
-  C_ESC = {
-    "/*" => "/\\*",
-    "*/" => "*\\/",
-    "\\" => "\\\\",
-    '"' => '\"',
-    "\n" => '\n',
-  }
-
-  0x00.upto(0x1f) {|ch| C_ESC[[ch].pack("C")] ||= "\\%03o" % ch }
-  0x7f.upto(0xff) {|ch| C_ESC[[ch].pack("C")] = "\\%03o" % ch }
-  C_ESC_PAT = Regexp.union(*C_ESC.keys)
-
-  def c_esc(str)
-    str.gsub(C_ESC_PAT) { C_ESC[$&] }
-  end
   def prelude_base(filename)
     filename.chomp(".rb")
   end
@@ -97,7 +85,7 @@ Prelude.new(output, ARGV, vpath).instance_eval do https://github.com/ruby/ruby/blob/trunk/template/prelude.c.tmpl#L85
 % preludes.each {|i, prelude, lines, sub|
 
 %   name = prelude_name(*prelude)
-static const char prelude_name<%=i%><%=%>[] = "<%=c_esc(name)%>";
+static const char prelude_name<%=i%><%=%>[] = <%=rstring2cstr(name)%>;
 static const struct {
 %   size = beg = 0
 %   lines.each_with_index {|(line, comment), n|
@@ -114,15 +102,15 @@ static const struct { https://github.com/ruby/ruby/blob/trunk/template/prelude.c.tmpl#L102
 %   end
 } prelude_code<%=i%><%=%> = {
 %   size = 0
-#line 1 "<%=c_esc(prelude)%>"
+#line 1 <%=rstring2cstr(prelude)%>
 %   lines.each_with_index {|(line, comment), n|
 %     if size + line.size >= Prelude::LINE_LIMIT
 %       size = 0
 ,
-#line <%=n+1%> "<%=c_esc(prelude)%>"
+#line <%=n+1%> <%=rstring2cstr(prelude)%>
 %     end
 %     size += line.size
-"<%=c_esc(line)%>"<%if comment%>/* <%=c_esc(comment)%> */<%end%>
+<%=rstring2cstr(line)%><%if comment%><%=commentify(comment)%><%end%>
 %   }
 #line <%=_erbout.count("\n")+2%> "<%=@output%>"
 };
diff --git a/tool/ruby_vm/helpers/c_escape.rb b/tool/ruby_vm/helpers/c_escape.rb
index a3368b8..e9a8da6 100644
--- a/tool/ruby_vm/helpers/c_escape.rb
+++ b/tool/ruby_vm/helpers/c_escape.rb
@@ -17,7 +17,7 @@ module RubyVM::CEscape https://github.com/ruby/ruby/blob/trunk/tool/ruby_vm/helpers/c_escape.rb#L17
 
   # generate comment, with escaps.
   def commentify str
-    return "/* #{str.strip.b.gsub '*/', '*\\/'} */"
+    return "/* #{str.b.gsub '*/', '*\\/'} */"
   end
 
   # Mimic gensym of CL.
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]