changeset 21494:8e9833b8791d

textscan: Prefer using std::string to char * * textscan.cc, textscan.h: Use std::string instead of char * arguments. Use std::string::find instead of strchr.
author Mike Miller <mtmiller@octave.org>
date Fri, 18 Mar 2016 20:29:15 -0700
parents a41e48ef2b99
children 82089c8ed7fa
files libinterp/corefcn/textscan.cc libinterp/corefcn/textscan.h
diffstat 2 files changed, 15 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/textscan.cc	Fri Mar 18 19:58:37 2016 -0700
+++ b/libinterp/corefcn/textscan.cc	Fri Mar 18 20:29:15 2016 -0700
@@ -329,7 +329,7 @@
 
       for (last = eob - longest; last - buf >= 0; last--)
         {
-          if (strchr (delims.c_str (), *last))
+          if (delims.find (*last) != std::string::npos)
             break;
         }
 
@@ -453,16 +453,16 @@
     literal_conversion = 2
   };
 
-  textscan_format_elt (const char *txt = 0, int w = 0, int p = -1,
+  textscan_format_elt (const std::string& txt, int w = 0, int p = -1,
                        int bw = 0, bool dis = false, char typ = '\0',
                        const std::string& ch_class = std::string ())
-    : text (strsave (txt)), width (w), prec (p), bitwidth (bw),
+    : text (txt), width (w), prec (p), bitwidth (bw),
       char_class (ch_class), type (typ), discard (dis),
       numeric (typ == 'd' || typ == 'u' || type == 'f' || type == 'n')
   { }
 
   textscan_format_elt (const textscan_format_elt& e)
-    : text (strsave (e.text)), width (e.width), prec (e.prec),
+    : text (e.text), width (e.width), prec (e.prec),
       bitwidth (e.bitwidth), char_class (e.char_class), type (e.type),
       discard (e.discard), numeric (e.numeric)
   { }
@@ -471,7 +471,7 @@
   {
     if (this != &e)
       {
-        text = strsave (e.text);
+        text = e.text;
         width = e.width;
         prec = e.prec;
         bitwidth = e.bitwidth;
@@ -484,10 +484,8 @@
     return *this;
   }
 
-  ~textscan_format_elt (void) { delete [] text; }
-
   // The C-style format string.
-  const char *text;
+  std::string text;
 
   // The maximum field width.
   unsigned int width;
@@ -730,8 +728,7 @@
       if (! text.empty ())
         {
           textscan_format_elt *elt
-            = new textscan_format_elt (text.c_str (), width, prec, bitwidth,
-                                       discard, type, char_class);
+            = new textscan_format_elt (text, width, prec, bitwidth, discard, type, char_class);
 
           if (! discard)
             output_container.push_back (val_type);
@@ -1587,9 +1584,8 @@
     }
 
   // look for exponent part in, e.g.,  6.023E+23
-  const char *ec = exp_chars.c_str ();
   bool used_exp = false;
-  if (valid && width_left > 1 && strchr (ec, ch))
+  if (valid && width_left > 1 && exp_chars.find (ch) != std::string::npos)
     {
       int ch1 = is.peek ();
       if (ch1 == '-' || ch1 == '+' || (ch1 >= '0' && ch1 <= '9'))
@@ -1820,7 +1816,7 @@
 // Return in VAL the run of characters from IS NOT contained in PATTERN.
 
 int
-textscan::scan_caret (delimited_stream& is, const char *pattern,
+textscan::scan_caret (delimited_stream& is, const std::string& pattern,
                       std::string& val) const
 {
   int c1 = std::istream::traits_type::eof ();
@@ -1830,7 +1826,7 @@
                  ? is.get_undelim ()
                  : std::istream::traits_type::eof ())
                 != std::istream::traits_type::eof ())
-         && ! strchr (pattern, c1))
+         && pattern.find (c1) == std::string::npos)
     obuf << static_cast<char> (c1);
 
   val = obuf.str ();
@@ -1927,13 +1923,13 @@
 // Return in VAL the run of characters from IS contained in PATTERN.
 
 int
-textscan::scan_bracket (delimited_stream& is, const char *pattern,
+textscan::scan_bracket (delimited_stream& is, const std::string& pattern,
                         std::string& val) const
 {
   int c1 = std::istream::traits_type::eof ();
   std::ostringstream obuf;              // Is this optimised for growing?
 
-  while (is && strchr (pattern, (c1 = is.get_undelim ())))
+  while (is && pattern.find (c1 = is.get_undelim ()) != std::string::npos)
     obuf << static_cast<char> (c1);
 
   val = obuf.str ();
--- a/libinterp/corefcn/textscan.h	Fri Mar 18 19:58:37 2016 -0700
+++ b/libinterp/corefcn/textscan.h	Fri Mar 18 20:29:15 2016 -0700
@@ -144,10 +144,11 @@
   void scan_complex (delimited_stream& is, const textscan_format_elt& fmt,
                      Complex& val) const;
 
-  int scan_bracket (delimited_stream& is, const char *pattern,
+  int scan_bracket (delimited_stream& is, const std::string& pattern,
                     std::string& val) const;
 
-  int scan_caret (delimited_stream& is, const char *, std::string& val) const;
+  int scan_caret (delimited_stream& is, const std::string& pattern,
+                  std::string& val) const;
 
   void scan_string (delimited_stream& is, const textscan_format_elt& fmt,
                     std::string& val) const;