changeset 23812:057a894914df

Use C++11 string fcns back() and pop_back() to simplify code. * error.cc (maybe_extract_message): Use "back()" rather than "[length () -1]". * input.cc (base_reader::octave_gets): Use "back()" rather than "[length () -1]". * ls-hdf5.cc (read_hdf5_data)): Use "clear()" rather than "resize (0)" to erase string. * ls-mat-ascii.cc (get_lines_and_columns): Reformat comment. * ls-oct-binary.cc (read_binary_data): Use "clear()" rather than "resize (0)" to erase string. * oct-stream.cc (delimited_stream::getline): Declare and initialize only one variable per line as per Octave coding convention. * oct-stream.cc (textscan_format_list::read_first_row): Use back() and pop_back() to potentially remove last character from string. * strfns.cc (Fstrvcat): Use "empty()" rather than "length() > 0". * sysdep.cc (w32_set_octav_home): Use back() and push_back() to potentially add dir separator to string. * gzip.cc (uchar_array): Space out code for readability. * file-ops.cc (tilde_expand): Call string_vector constructor with size n rather than resize. * file-ops.cc (concat): Use "back()" rather than "[length () -1]". * file-stat.cc (update_internal): Use "back()" rather than "[length () -1]". Use pop_back() rather than resize() to remove last character. * oct-env.cc (do_chdir): Use pop_back() rather than resize() to remove last character. * cmd-edit.cc (do_generate_filename_completions): Reformat comment. * cmd-hist.cc (): Use back() and pop_back() to potentially remove "\n" from end of string. * data-conv.cc (strip_spaces): Initialize string with null character rather than space. * kpse.cc (kpse_tilde_expand): Use "back()" rather than "[length () -1]". * kpse.cc (kpse_expand_kpse_dot, kpse_brace_expand, kpse_path_expand): Use pop_back() to potentially remove last character. * kpse.cc (kpse_brace_expand_element, kpse_element_dir): Use pop_back() to remove last character. * lo-utils.cc (octave_fgetl): Use "back()" rather than "[length () -1]". Use pop_back() rather than resize() to remove last character.
author Rik <rik@octave.org>
date Sun, 30 Jul 2017 09:21:58 -0700
parents e8eb1b86e935
children ccc4e85762ac
files libinterp/corefcn/error.cc libinterp/corefcn/input.cc libinterp/corefcn/ls-hdf5.cc libinterp/corefcn/ls-mat-ascii.cc libinterp/corefcn/ls-oct-binary.cc libinterp/corefcn/oct-stream.cc libinterp/corefcn/strfns.cc libinterp/corefcn/sysdep.cc libinterp/dldfcn/gzip.cc liboctave/system/file-ops.cc liboctave/system/file-stat.cc liboctave/system/oct-env.cc liboctave/util/cmd-edit.cc liboctave/util/cmd-hist.cc liboctave/util/data-conv.cc liboctave/util/kpse.cc liboctave/util/lo-utils.cc
diffstat 17 files changed, 38 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/error.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/libinterp/corefcn/error.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -1080,15 +1080,14 @@
     {
       std::string arg1 = args(0).string_value ();
 
-      // For compatibility with Matlab, an identifier must contain
-      // ':', but not at the beginning or the end, and it must not
-      // contain '%' (even if it is not a valid conversion
-      // operator) or whitespace.
+      // For compatibility with Matlab, an identifier must contain ':',
+      // but not at the beginning or the end, and it must not contain '%'
+      // (even if it is not a valid conversion operator) or whitespace.
 
       if (arg1.find_first_of ("% \f\n\r\t\v") == std::string::npos
           && arg1.find (':') != std::string::npos
           && arg1[0] != ':'
-          && arg1[arg1.length ()-1] != ':')
+          && arg1.back () != ':')
         {
           if (nargin > 1)
             {
--- a/libinterp/corefcn/input.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/libinterp/corefcn/input.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -260,7 +260,7 @@
 
         octave_diary << retval;
 
-        if (retval[retval.length () - 1] != '\n')
+        if (retval.back () != '\n')
           octave_diary << "\n";
       }
     else
--- a/libinterp/corefcn/ls-hdf5.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/libinterp/corefcn/ls-hdf5.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -730,7 +730,7 @@
 
   std::string retval;
 
-  doc.resize (0);
+  doc.clear ();
 
   hdf5_ifstream& hs = dynamic_cast<hdf5_ifstream&> (is);
   hdf5_callback_data d;
--- a/libinterp/corefcn/ls-mat-ascii.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/libinterp/corefcn/ls-mat-ascii.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -170,9 +170,8 @@
               if (beg == std::string::npos
                   || (buf[beg] == '\r' && beg == buf.length () - 1))
                 {
-                  // We had a line with trailing spaces and
-                  // ending with a CRLF, so this should look like EOL,
-                  // not a new colum.
+                  // We had a line with trailing spaces and ending with a CRLF,
+                  // so this should look like EOL, not a new column.
                   break;
                 }
             }
--- a/libinterp/corefcn/ls-oct-binary.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/libinterp/corefcn/ls-oct-binary.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -129,7 +129,7 @@
   int32_t name_len = 0;
   int32_t doc_len = 0;
 
-  doc.resize (0);
+  doc.clear ();
 
   // We expect to fail here, at the beginning of a record, so not
   // being able to read another name should not result in an error.
--- a/libinterp/corefcn/oct-stream.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/libinterp/corefcn/oct-stream.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -1600,7 +1600,8 @@
   int
   delimited_stream::getline (std::string& out, char delim)
   {
-    int len = out.length (), used = 0;
+    int len = out.length ();
+    int used = 0;
     int ch;
     while ((ch = get_undelim ()) != delim
            && ch != std::istream::traits_type::eof ())
@@ -2473,9 +2474,8 @@
 
     is.getline (first_line, static_cast<char> (ts.eol2));
 
-    if (! first_line.empty ()
-        && first_line[first_line.length () - 1] == ts.eol1)
-      first_line.resize (first_line.length () - 1);
+    if (! first_line.empty () && first_line.back () == ts.eol1)
+      first_line.pop_back ();
 
     std::istringstream strstr (first_line);
     delimited_stream ds (strstr, is);
--- a/libinterp/corefcn/strfns.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/libinterp/corefcn/strfns.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -221,7 +221,7 @@
         {
           for (size_t j = 0; j < n; j++)
             {
-              if (s[j].length () > 0)
+              if (! s[j].empty ())
                 n_elts++;
             }
         }
--- a/libinterp/corefcn/sysdep.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/libinterp/corefcn/sysdep.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -136,8 +136,8 @@
               if (mod_name.find ("octinterp") != std::string::npos)
                 {
                   bin_dir = mod_info.szExePath;
-                  if (bin_dir[bin_dir.length () - 1] != '\\')
-                    bin_dir.append (1, '\\');
+                  if (! bin_dir.empty () && bin_dir.back () != '\\')
+                    bin_dir.push_back ('\\');
                   break;
                 }
             }
--- a/libinterp/dldfcn/gzip.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/libinterp/dldfcn/gzip.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -266,7 +266,7 @@
 
       uchar_array (const std::string& str)
       {
-        p = new Bytef[str.length () +1];
+        p = new Bytef[str.length () + 1];
         std::strcpy (reinterpret_cast<char *> (p), str.c_str ());
       }
 
--- a/liboctave/system/file-ops.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/liboctave/system/file-ops.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -332,11 +332,9 @@
 
       string_vector tilde_expand (const string_vector& names)
       {
-        string_vector retval;
-
         int n = names.numel ();
 
-        retval.resize (n);
+        string_vector retval (n);
 
         for (int i = 0; i < n; i++)
           retval[i] = tilde_expand (names[i]);
@@ -348,7 +346,7 @@
       {
         return dir.empty ()
           ? file
-          : (is_dir_sep (dir[dir.length ()-1])
+          : (is_dir_sep (dir.back ())
              ? dir + file
              : dir + dir_sep_char () + file);
       }
--- a/liboctave/system/file-stat.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/liboctave/system/file-stat.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -188,10 +188,10 @@
 
 #if defined (__WIN32__)
           // Remove trailing slash.
-          if (sys::file_ops::is_dir_sep (full_file_name[full_file_name.length () - 1])
-              && full_file_name.length () != 1
+          if (full_file_name.length () > 1
+              && sys::file_ops::is_dir_sep (full_file_name.back ())
               && ! (full_file_name.length () == 3 && full_file_name[1] == ':'))
-            full_file_name.resize (full_file_name.length () - 1);
+            full_file_name.pop_back ();
 #endif
 
           const char *cname = full_file_name.c_str ();
--- a/liboctave/system/oct-env.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/liboctave/system/oct-env.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -557,14 +557,8 @@
             tmp = do_make_absolute (newdir, current_directory);
 
           // Get rid of trailing directory separator.
-
-          size_t len = tmp.length ();
-
-          if (len > 1)
-            {
-              if (sys::file_ops::is_dir_sep (tmp[--len]))
-                tmp.resize (len);
-            }
+          if (tmp.length () > 1 && sys::file_ops::is_dir_sep (tmp.back ()))
+            tmp.pop_back ();
 
           if (! sys::chdir (tmp))
             {
--- a/liboctave/util/cmd-edit.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/liboctave/util/cmd-edit.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -616,7 +616,6 @@
                 // Famous last words:  Most large directories will not
                 // have more than a few hundred files, so we should not
                 // resize too many times even if the growth is linear...
-
                 n += 100;
                 retval.resize (n);
               }
--- a/liboctave/util/cmd-hist.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/liboctave/util/cmd-hist.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -196,9 +196,8 @@
 
         // Strip newline before adding to list
         std::string stmp = s;
-        int stmp_len = stmp.length ();
-        if (stmp[stmp_len - 1] == '\n')
-          stmp.resize (stmp_len - 1);
+        if (stmp.back () == '\n')
+          stmp.pop_back ();
 
         int added = ::octave_add_history (stmp.c_str (), history_control);
         lines_this_session += added;
--- a/liboctave/util/data-conv.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/liboctave/util/data-conv.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -135,7 +135,7 @@
 
   size_t k = 0;
 
-  std::string s (n, ' ');
+  std::string s (n, '\0');
 
   for (size_t i = 0; i < n; i++)
     if (! isspace (str[i]))
--- a/liboctave/util/kpse.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/liboctave/util/kpse.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -724,7 +724,7 @@
         home = home.substr (1);
 
       /* If HOME ends in /, omit the / after ~user. */
-      if (name.length () > c && IS_DIR_SEP (home[home.length () - 1]))
+      if (name.length () > c && IS_DIR_SEP (home.back ()))
         c++;
 
       expansion = (name.length () > c ? home : home + name.substr (c));
@@ -782,9 +782,8 @@
         ret += kpse_dot + DIR_SEP_STRING + elt + ENV_SEP_STRING;
     }
 
-  int len = ret.length ();
-  if (len > 0)
-    ret.resize (len-1);
+  if (! ret.empty ())
+    ret.pop_back ();
 
   return ret;
 }
@@ -818,7 +817,7 @@
       ret += x + ENV_SEP_STRING;
     }
 
-  ret.resize (ret.length () - 1);
+  ret.pop_back ();
 
   return ret;
 }
@@ -849,9 +848,8 @@
       ret += expansion + ENV_SEP_STRING;
     }
 
-  size_t len = ret.length ();
-  if (len > 0)
-    ret.resize (len-1);
+  if (! ret.empty ())
+    ret.pop_back ();
 
   return kpse_expand_kpse_dot (ret);
 }
@@ -924,8 +922,8 @@
         }
     }
 
-  if (len > 0)
-    ret.resize (len-1);
+  if (! ret.empty ())
+    ret.pop_back ();
 
   return ret;
 }
@@ -1214,7 +1212,7 @@
     {
       ret = elt;
 
-      char last_char = ret[ret.length () - 1];
+      char last_char = ret.back ();
 
       if (! (IS_DIR_SEP (last_char) || IS_DEVICE_SEP (last_char)))
         ret += DIR_SEP_STRING;
--- a/liboctave/util/lo-utils.cc	Sun Jul 30 09:21:39 2017 -0700
+++ b/liboctave/util/lo-utils.cc	Sun Jul 30 09:21:58 2017 -0700
@@ -193,10 +193,8 @@
 {
   std::string retval = octave_fgets (f, eof);
 
-  size_t len = retval.length ();
-
-  if (retval[len-1] == '\n')
-    retval.resize (len-1);
+  if (! retval.empty () && retval.back () == '\n')
+    retval.pop_back ();
 
   return retval;
 }