From a38b3307b6dd7ea8770a46f9485b0ffe4de1b40c Mon Sep 17 00:00:00 2001
From: nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
Date: Mon, 5 Feb 2018 00:52:20 +0000
Subject: [PATCH] transform_mjit_header.rb: scan by regexp

* tool/transform_mjit_header.rb (find_decl): scan by regexp
  instead of char-by-char loop.  return nil when finished.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62213 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
---
 tool/transform_mjit_header.rb | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/tool/transform_mjit_header.rb b/tool/transform_mjit_header.rb
index 0de7e0af81..7ce139f92a 100644
--- a/tool/transform_mjit_header.rb
+++ b/tool/transform_mjit_header.rb
@@ -24,24 +24,28 @@ module MJITHeader
   # Return start..stop of last decl in CODE ending STOP
   def self.find_decl(code, stop)
     level = 0
-    stop.downto(0) do |i|
-      if level == 0 && stop != i && decl_found?(code, i)
-        return decl_start(code, i)..stop
-      elsif code[i] == '}'
+    i = stop
+    while i = code.rindex(/[;{}]/, i)
+      if level == 0 && stop != i && decl_found?($&, i)
+        return decl_start($&, i)..stop
+      end
+      case $&
+      when '}'
         level += 1
-      elsif code[i] == '{'
+      when '{'
         level -= 1
       end
+      i -= 1
     end
-    0..-1
+    nil
   end
 
   def self.decl_found?(code, i)
-    i == 0 || code[i] == ';' || code[i] == '}'
+    i == 0 || code == ';' || code == '}'
   end
 
   def self.decl_start(code, i)
-    if i == 0 && code[i] != ';' && code[i] != '}'
+    if i == 0 && code != ';' && code != '}'
       0
     else
       i + 1
@@ -139,14 +143,11 @@ end
 puts "\nTransforming external functions to static:"
 
 code = MJITHeader.separate_macro_and_code(code) # note: this does not work on MinGW
-stop_pos     = code.match(/^#/).begin(0) # See `separate_macro_and_code`. This ignores proprocessors.
+stop_pos     = -1
 extern_names = []
 
 # This loop changes function declarations to static inline.
-loop do
-  decl_range = MJITHeader.find_decl(code, stop_pos)
-  break if decl_range.end < 0
-
+while (decl_range = MJITHeader.find_decl(code, stop_pos))
   stop_pos = decl_range.begin - 1
   decl = code[decl_range]
   decl_name = MJITHeader.decl_name_of(decl)