diff libinterp/octave-value/ov-complex.cc @ 20653:c16947991354

avoid fixed-size buffers in index exception code * lo-array-gripes.h, lo-array-gripes.cc (index_exception::expression): Use std::ostringstream instead of fixed size buffer. (gripe_invalid_index): Likewise. (out_of_range::details): Likewise. (index_exception::index_exception): Set default dim value to -1. * ov-complex.h, ov-complex.cc (gripe_complex_index): Now static. Pass arg by const reference instead of value. Use std::ostringstream instead of fixed size buffer. (octave_complex::index_vector): Move definition out of header file.
author John W. Eaton <jwe@octave.org>
date Thu, 22 Oct 2015 12:48:49 -0400
parents 7a8096f8df5d
children 384ff5aa9437
line wrap: on
line diff
--- a/libinterp/octave-value/ov-complex.cc	Wed Oct 21 23:43:46 2015 -0400
+++ b/libinterp/octave-value/ov-complex.cc	Thu Oct 22 12:48:49 2015 -0400
@@ -25,6 +25,7 @@
 #endif
 
 #include <iostream>
+#include <sstream>
 
 #include "lo-ieee.h"
 #include "lo-specfun.h"
@@ -55,6 +56,39 @@
 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_complex,
                                      "complex scalar", "double");
 
+// Complain if a complex value is used as a subscript.
+
+class complex_index_exception : public index_exception
+{
+public:
+
+  complex_index_exception (const std::string& value)
+    : index_exception (value) { }
+
+  ~complex_index_exception (void) { }
+
+  std::string details (void) const
+  {
+    return "subscripts must be real (forgot to initialize i or j?)";
+  }
+
+  // ID of error to throw.
+  const char *err_id (void) const
+  {
+    return error_id_invalid_index;
+  }
+};
+
+static void
+gripe_complex_index (const Complex& idx)
+{
+  std::ostringstream buf;
+  buf << std::real (idx) << std::showpos << std::imag (idx) << "i";
+  complex_index_exception e (buf.str ());
+
+  throw e;
+}
+
 static octave_base_value *
 default_numeric_demotion_function (const octave_base_value& a)
 {
@@ -102,6 +136,14 @@
   return tmp.do_index_op (idx, resize_ok);
 }
 
+idx_vector
+octave_complex::index_vector (bool) const
+{
+  gripe_complex_index (scalar);
+
+  return idx_vector ();
+}
+
 double
 octave_complex::double_value (bool force_conversion) const
 {
@@ -481,40 +523,3 @@
       return octave_base_value::map (umap);
     }
 }
-
-class complex_index_exception : public index_exception
-{
-public:
-
-  complex_index_exception (const std::string& value)
-    : index_exception (value) { }
-
-  ~complex_index_exception (void) { }
-
-  std::string details (void) const
-  {
-    return "subscripts must be real (forgot to initialize i or j?)";
-  }
-
-  // ID of error to throw.
-  const char *err_id (void) const
-  {
-    return error_id_invalid_index;
-  }
-};
-
-// Complain if a complex value is used as a subscript
-
-void
-gripe_complex_index (Complex idx)
-{
-  // FIXME: don't use a fixed size buffer!
-
-  char buf [100];
-
-  sprintf (buf, "%g%+gi", std::real(idx), std::imag(idx));
-
-  complex_index_exception e (buf);
-
-  throw e;
-}