From 5df3fde95e0c837fefb26567b47d3e9019c0b669 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Thu, 28 Jul 2022 15:48:21 +0200
Subject: [PATCH] Bytecode MEX: streamline function for adding tildes below FP
 error

---
 mex/sources/bytecode/ErrorHandling.hh | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)

diff --git a/mex/sources/bytecode/ErrorHandling.hh b/mex/sources/bytecode/ErrorHandling.hh
index 889a6f5d01..884541547b 100644
--- a/mex/sources/bytecode/ErrorHandling.hh
+++ b/mex/sources/bytecode/ErrorHandling.hh
@@ -214,32 +214,31 @@ private:
     (materialized by an operator between braces), returns a string spanning two
     lines, the first line containing the original string without the braces,
     the second line containing tildes (~) under the faulty operator. */
-  string
+  static string
   add_underscore_to_fpe(const string &str)
   {
-    string temp;
-    int pos1 = -1, pos2 = -1;
-    string tmp_n(str.length(), ' ');
-    for (const char & i : str)
+    string line1;
+    optional<size_t> pos1, pos2;
+    string line2(str.length(), ' ');
+    for (char i : str)
       {
         if (i != '{' && i != '}')
-          temp += i;
+          line1 += i;
         else
           {
             if (i == '{')
-              pos1 = static_cast<int>(temp.length());
+              pos1 = line1.length();
             else
-              pos2 = static_cast<int>(temp.length());
-            if (pos1 >= 0 && pos2 >= 0)
+              pos2 = line1.length();
+            if (pos1 && pos2)
               {
-                tmp_n.erase(pos1, pos2-pos1+1);
-                tmp_n.insert(pos1, pos2-pos1, '~');
-                pos1 = pos2 = -1;
+                line2.replace(*pos1, *pos2-*pos1, *pos2-*pos1, '~');
+                pos1.reset();
+                pos2.reset();
               }
           }
       }
-    temp += "\n" + tmp_n;
-    return temp;
+    return line1 + "\n" + line2;
   }
 
   void
-- 
GitLab