octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #65342] replace atoi with stoi


From: John W. Eaton
Subject: [Octave-bug-tracker] [bug #65342] replace atoi with stoi
Date: Fri, 22 Mar 2024 12:12:01 -0400 (EDT)

Follow-up Comment #4, bug #65342 (group octave):

I agree with Rik.  Switching to stoi will be more work than just replacing
atoi with std::stoi.  For example, in the Fdbtype function in debug.cc,
instead of


@@ -654,7 +654,7 @@ numbers.
           }
         else  // (dbtype fcn) || (dbtype lineno)
           {
-            int line = atoi (arg.c_str ());
+            int line = std::stoi (arg);
 
             if (line == 0)  // (dbtype fcn)
               fcn_name = arg;


we will probably want to do something like


@@ -654,18 +654,27 @@ numbers.
           }
         else  // (dbtype fcn) || (dbtype lineno)
           {
-            int line = atoi (arg.c_str ());
+            int line;
 
-            if (line == 0)  // (dbtype fcn)
-              fcn_name = arg;
-            else  // (dbtype lineno)
+            try
               {
+                line = std::stoi (arg);
+
                 if (line <= 0)
                   error ("dbtype: start and end lines must be >= 1\n");
 
                 start = line;
                 end = line;
               }
+            catch (const std::invalid_argument&)
+              {
+                // Not a number: dbtype fcn
+                fcn_name = arg;
+              }
+            catch (const std::out_of_range&)
+              {
+                error ("dbtype: line number out of range")
+              }
           }
       }
       break;


Although this will require a lot more thought and effort to do correctly, I
think it will be worth it to be able to distinguish between invalid input, out
of range values, and not confuse "0" with an invalid or out of range input.

I thought about the possibility of writing a wrapper around std::stoi, but the
action to take when an exception is thrown can vary in each case so it is not
clear to me that we can do better than using individual try/catch blocks for
each instance.  Fortunately, there are fewer than 20 instances in the Octave
sources, so the conversion should be manageable.


    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?65342>

_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]