changeset 21867:0cdfd6d230e6

use std::list<std::string> instead of string_vector in pathsearch functions * kpse.cc, pathsearch.cc, pathsearch.h: Use std::list<std::string> instead of string_vector in pathsearch functions. * ls-mat5.cc, utils.cc, ov-fcn-handle.cc: Update to match.
author John W. Eaton <jwe@octave.org>
date Sat, 11 Jun 2016 18:44:07 -0400
parents 297d4fc3c563
children e2796ea8feac
files libinterp/corefcn/ls-mat5.cc libinterp/corefcn/utils.cc libinterp/octave-value/ov-fcn-handle.cc liboctave/util/kpse.cc liboctave/util/pathsearch.cc liboctave/util/pathsearch.h
diffstat 6 files changed, 148 insertions(+), 167 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/ls-mat5.cc	Sat Jun 11 16:37:05 2016 -0400
+++ b/libinterp/corefcn/ls-mat5.cc	Sat Jun 11 18:44:07 2016 -0400
@@ -899,10 +899,10 @@
                     else
                       {
                         // Next just search for it anywhere in the system path
-                        string_vector names(3);
-                        names(0) = fname + ".oct";
-                        names(1) = fname + ".mex";
-                        names(2) = fname + ".m";
+                        std::list<std::string> names;
+                        names.push_back (fname + ".oct");
+                        names.push_back (fname + ".mex");
+                        names.push_back (fname + ".m");
 
                         octave::directory_path p (load_path::system_path ());
 
--- a/libinterp/corefcn/utils.cc	Sat Jun 11 16:37:05 2016 -0400
+++ b/libinterp/corefcn/utils.cc	Sat Jun 11 18:44:07 2016 -0400
@@ -252,7 +252,7 @@
 {
   octave::directory_path p (path);
 
-  return octave::sys::env::make_absolute (p.find_first_of (names));
+  return octave::sys::env::make_absolute (p.find_first_of (names.std_list ()));
 }
 
 // Find all locations of the given file in the path.
@@ -262,7 +262,7 @@
 {
   octave::directory_path p (path);
 
-  string_vector sv = p.find_all_first_of (names);
+  string_vector sv = p.find_all_first_of (names.std_list ());
 
   octave_idx_type len = sv.numel ();
 
--- a/libinterp/octave-value/ov-fcn-handle.cc	Sat Jun 11 16:37:05 2016 -0400
+++ b/libinterp/octave-value/ov-fcn-handle.cc	Sat Jun 11 18:44:07 2016 -0400
@@ -27,6 +27,7 @@
 #endif
 
 #include <iostream>
+#include <list>
 #include <sstream>
 #include <vector>
 
@@ -287,10 +288,10 @@
       else
         {
           // Next just search for it anywhere in the system path
-          string_vector names(3);
-          names(0) = nm + ".oct";
-          names(1) = nm + ".mex";
-          names(2) = nm + ".m";
+          std::list<std::string> names;
+          names.push_back (nm + ".oct");
+          names.push_back (nm + ".mex");
+          names.push_back (nm + ".m");
 
           octave::directory_path p (load_path::system_path ());
 
--- a/liboctave/util/kpse.cc	Sat Jun 11 16:37:05 2016 -0400
+++ b/liboctave/util/kpse.cc	Sat Jun 11 18:44:07 2016 -0400
@@ -569,7 +569,7 @@
    record the filename(s) found in $TEXMFLOG.  */
 
 static void
-log_search (const string_vector& filenames)
+log_search (const std::list<std::string>& filenames)
 {
   static FILE *log_file = 0;
   static bool first_time = true; /* Need to open the log file?  */
@@ -593,10 +593,8 @@
   if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH) || log_file)
     {
       /* FILENAMES should never be null, but safety doesn't hurt.  */
-      for (int e = 0; e < filenames.numel () && ! filenames[e].empty (); e++)
+      for (const auto &filename : filenames)
         {
-          std::string filename = filenames[e];
-
           /* Only record absolute filenames, for privacy.  */
           if (log_file && kpse_absolute_p (filename.c_str (), false))
             gnulib::fprintf (log_file, "%lu %s\n",
@@ -622,12 +620,12 @@
    does seem cleaner.  (We do waste a bit of space in the return
    value, though, since we don't shrink it to the final size returned.)  */
 
-static string_vector
+static std::list<std::string>
 dir_list_search (str_llist_type *dirs, const std::string& name,
                  bool search_all)
 {
   str_llist_elt_type *elt;
-  string_vector ret;
+  std::list<std::string> ret;
 
   for (elt = *dirs; elt; elt = STR_LLIST_NEXT (*elt))
     {
@@ -639,7 +637,7 @@
 
       if (! tmp.empty ())
         {
-          ret.append (potential);
+          ret.push_back (potential);
 
           /* Move this element towards the top of the list.  */
           str_llist_float (dirs, elt);
@@ -655,15 +653,15 @@
 /* This is called when NAME is absolute or explicitly relative; if it's
    readable, return (a list containing) it; otherwise, return NULL.  */
 
-static string_vector
+static std::list<std::string>
 absolute_search (const std::string& name)
 {
-  string_vector ret_list;
+  std::list<std::string> ret_list;
   std::string found = kpse_readable_file (name);
 
   /* Add 'found' to the return list even if it's null; that tells
      the caller we didn't find anything.  */
-  ret_list.append (found);
+  ret_list.push_back (found);
 
   return ret_list;
 }
@@ -671,18 +669,18 @@
 /* This is the hard case -- look for NAME in PATH.  If ALL is false,
    return the first file found.  Otherwise, search all elements of PATH.  */
 
-static string_vector
+static std::list<std::string>
 path_search (const std::string& path, const std::string& name,
              bool /* must_exist */, bool all)
 {
-  string_vector ret_list;
+  std::list<std::string> ret_list;
   bool done = false;
 
   for (kpse_path_iterator pi (path); ! done && pi != std::string::npos; pi++)
     {
       std::string elt = *pi;
 
-      string_vector found;
+      std::list<std::string> found;
 
       /* Do not touch the device if present */
       if (NAME_BEGINS_WITH_DEVICE (elt))
@@ -706,7 +704,7 @@
       /* Try ls-R, unless we're searching for texmf.cnf.  Our caller
          (search), also tests first_search, and does the resetting.  */
       if (first_search)
-        found = string_vector ();
+        found = std::list<std::string> ();
 
       /* Search the filesystem.  */
 
@@ -722,10 +720,10 @@
       if (! found.empty ())
         {
           if (all)
-            ret_list.append (found);
+            ret_list.splice (ret_list.end (), found);
           else
             {
-              ret_list.append (found[0]);
+              ret_list.push_back (found.front ());
               done = true;
             }
         }
@@ -742,11 +740,11 @@
    contain just NULL.  If ALL is true, the list will be
    terminated with NULL.  */
 
-static string_vector
+static std::list<std::string>
 search (const std::string& path, const std::string& original_name,
         bool must_exist, bool all)
 {
-  string_vector ret_list;
+  std::list<std::string> ret_list;
   bool absolute_p;
 
   /* Make a leading ~ count as an absolute filename, and expand $FOO's.  */
@@ -805,9 +803,9 @@
 kpse_path_search (const std::string& path, const std::string& name,
                   bool must_exist)
 {
-  string_vector ret_list = search (path, name, must_exist, false);
-
-  return ret_list.empty () ? "" : ret_list[0];
+  std::list<std::string> ret_list = search (path, name, must_exist, false);
+
+  return ret_list.empty () ? "" : ret_list.front ();
 }
 
 /* Search all elements of PATH for files named NAME.  Not sure if it's
@@ -816,7 +814,7 @@
 /* Like 'kpse_path_search' with MUST_EXIST true, but return a list of
    all the filenames (or NULL if none), instead of taking the first.  */
 
-static string_vector
+static std::list<std::string>
 kpse_all_path_search (const std::string& path, const std::string& name)
 {
   return search (path, name, true, true);
@@ -826,11 +824,12 @@
    element of NAMES.  If ALL is false, return the first file found.
    Otherwise, search all elements of PATH.  */
 
-static string_vector
-path_find_first_of (const std::string& path, const string_vector& names,
+static std::list<std::string>
+path_find_first_of (const std::string& path,
+                    const std::list<std::string>& names,
                     bool /* must_exist */, bool all)
 {
-  string_vector ret_list;
+  std::list<std::string> ret_list;
   bool done = false;
 
   for (kpse_path_iterator pi (path); ! done && pi != std::string::npos; pi++)
@@ -839,7 +838,7 @@
 
       str_llist_type *dirs;
       str_llist_elt_type *dirs_elt;
-      string_vector found;
+      std::list<std::string> found;
 
       /* Do not touch the device if present */
 
@@ -867,16 +866,15 @@
         {
           const std::string dir = STR_LLIST (*dirs_elt);
 
-          int len = names.numel ();
-          for (int i = 0; i < len && ! done; i++)
+          for (auto it = names.cbegin (); it != names.cend () && ! done; it++)
             {
-              std::string name = names[i];
+              std::string name = *it;
 
               /* Try ls-R, unless we're searching for texmf.cnf.  Our caller
                  (find_first_of), also tests first_search, and does the
                  resetting.  */
               if (first_search)
-                found = string_vector ();
+                found = std::list<std::string> ();
 
               /* Search the filesystem.  */
 
@@ -900,10 +898,10 @@
               if (! found.empty ())
                 {
                   if (all)
-                    ret_list.append (found);
+                    ret_list.splice (ret_list.end (), found);
                   else
                     {
-                      ret_list.append (found[0]);
+                      ret_list.push_back (found.front ());
                       done = true;
                     }
                 }
@@ -914,34 +912,30 @@
   return ret_list;
 }
 
-static string_vector
-find_first_of (const std::string& path, const string_vector& names,
+static std::list<std::string>
+find_first_of (const std::string& path, const std::list<std::string>& names,
                bool must_exist, bool all)
 {
-  string_vector ret_list;
+  std::list<std::string> ret_list;
 
   if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH))
     {
       gnulib::fputs ("start find_first_of ((", stderr);
 
-      int len = names.numel ();
-
-      for (int i = 0; i < len; i++)
+      for (auto p = names.cbegin (); p != names.cend (); p++)
         {
-          if (i == 0)
-            gnulib::fputs (names[i].c_str (), stderr);
+          if (p == names.cbegin ())
+            gnulib::fputs (p->c_str (), stderr);
           else
-            gnulib::fprintf (stderr, ", %s", names[i].c_str ());
+            gnulib::fprintf (stderr, ", %s", p->c_str ());
         }
 
       gnulib::fprintf (stderr, "), path=%s, must_exist=%d).\n",
                        path.c_str (), must_exist);
     }
 
-  for (int i = 0; i < names.numel (); i++)
+  for (const auto &name : names)
     {
-      std::string name = names[i];
-
       if (kpse_absolute_p (name, true))
         {
           /* If the name is absolute or explicitly relative, no need
@@ -973,14 +967,12 @@
         {
           gnulib::fputs ("find_first_of (", stderr);
 
-          int len = names.numel ();
-
-          for (int i = 0; i < len; i++)
+          for (auto p = names.cbegin (); p != names.cend (); p++)
             {
-              if (i == 0)
-                gnulib::fputs (names[i].c_str (), stderr);
+              if (p == names.cbegin ())
+                gnulib::fputs (p->c_str (), stderr);
               else
-                gnulib::fprintf (stderr, ", %s", names[i].c_str ());
+                gnulib::fprintf (stderr, ", %s", p->c_str ());
             }
 
           gnulib::fputs (") =>", stderr);
@@ -1002,12 +994,14 @@
    Return the first one found.  */
 
 static std::string
-kpse_path_find_first_of (const std::string& path, const string_vector& names,
+kpse_path_find_first_of (const std::string& path,
+                         const std::list<std::string>& names,
                          bool must_exist)
 {
-  string_vector ret_list = find_first_of (path, names, must_exist, false);
-
-  return ret_list.empty () ? "" : ret_list[0];
+  std::list<std::string> ret_list
+    = find_first_of (path, names, must_exist, false);
+
+  return ret_list.empty () ? "" : ret_list.front ();
 }
 
 /* Search each element of PATH for each element of NAMES and return a
@@ -1017,9 +1011,9 @@
    list of all the filenames (or NULL if none), instead of taking the
    first.  */
 
-static string_vector
+static std::list<std::string>
 kpse_all_path_find_first_of (const std::string& path,
-                             const string_vector& names)
+                             const std::list<std::string>& names)
 {
   return find_first_of (path, names, true, true);
 }
@@ -1132,7 +1126,7 @@
 }
 
 /* Forward declarations of functions from the original expand.c  */
-static string_vector brace_expand (const std::string&);
+static std::list<std::string> brace_expand (const std::string&);
 
 /* If $KPSE_DOT is defined in the environment, prepend it to any relative
    path components. */
@@ -1183,14 +1177,14 @@
 {
   std::string ret;
 
-  string_vector expansions = brace_expand (elt);
-
-  for (int i = 0; i < expansions.numel (); i++)
+  std::list<std::string> expansions = brace_expand (elt);
+
+  for (const auto &expanded_elt : expansions)
     {
       /* Do $ and ~ expansion on each element.  */
-      std::string x = kpse_expand (expansions[i]);
-
-      if (x != expansions[i])
+      std::string x = kpse_expand (expanded_elt);
+
+      if (x != elt)
         {
           /* If we did any expansions, do brace expansion again.  Since
              recursive variable definitions are not allowed, this recursion
@@ -1348,10 +1342,11 @@
    are free ()'ed.  ARR1 can be NULL, in that case, a new version of ARR2
    is returned. */
 
-static string_vector
-array_concat (const string_vector& arr1, const string_vector& arr2)
+static std::list<std::string>
+array_concat (const std::list<std::string>& arr1,
+              const std::list<std::string>& arr2)
 {
-  string_vector result;
+  std::list<std::string> result;
 
   if (arr1.empty ())
     result = arr2;
@@ -1359,25 +1354,19 @@
     result = arr1;
   else
     {
-      int len1 = arr1.numel ();
-      int len2 = arr2.numel ();
-
-      result = string_vector (len1 * len2);
-
-      int k = 0;
-      for (int i = 0; i < len2; i++)
-        for (int j = 0; j < len1; j++)
-          result[k++] = arr1[j] + arr2[i];
+      for (const auto &elt_2 : arr2)
+        for (const auto &elt_1 : arr1)
+          result.push_back (elt_1 + elt_2);
     }
 
   return result;
 }
 
 static int brace_gobbler (const std::string&, int&, int);
-static string_vector expand_amble (const std::string&);
+static std::list<std::string> expand_amble (const std::string&);
 
 /* Return an array of strings; the brace expansion of TEXT. */
-static string_vector
+static std::list<std::string>
 brace_expand (const std::string& text)
 {
   /* Find the text of the preamble. */
@@ -1386,7 +1375,7 @@
 
   std::string preamble = text.substr (0, i);
 
-  string_vector result = string_vector (preamble);
+  std::list<std::string> result (1, preamble);
 
   if (c == '{')
     {
@@ -1401,7 +1390,7 @@
             ("Octave:pathsearch-syntax",
              "%s: Unmatched {", text.c_str ());
 
-          result = string_vector (text);
+          result = std::list<std::string> (1, text);
         }
       else
         {
@@ -1423,10 +1412,10 @@
    text at BRACE_ARG_SEPARATORs into separate strings.  We then brace
    expand each slot which needs it, until there are no more slots which
    need it. */
-static string_vector
+static std::list<std::string>
 expand_amble (const std::string& text)
 {
-  string_vector result;
+  std::list<std::string> result;
 
   size_t text_len = text.length ();
   size_t start;
@@ -1443,12 +1432,12 @@
 
       std::string tem = text.substr (start, i-start);
 
-      string_vector partial = brace_expand (tem);
+      std::list<std::string> partial = brace_expand (tem);
 
       if (result.empty ())
         result = partial;
       else
-        result.append (partial);
+        result.splice (result.end (), partial);
     }
 
   return result;
--- a/liboctave/util/pathsearch.cc	Sat Jun 11 16:37:05 2016 -0400
+++ b/liboctave/util/pathsearch.cc	Sat Jun 11 18:44:07 2016 -0400
@@ -32,7 +32,6 @@
 #include "oct-env.h"
 #include "pathsearch.h"
 #include "singleton-cleanup.h"
-#include "str-vec.h"
 
 #include "kpse.cc"
 
@@ -63,29 +62,22 @@
     return retval;
   }
 
-  string_vector
+  std::list<std::string>
   directory_path::elements (void)
   {
-    return initialized ? pv : string_vector ();
+    return m_initialized ? m_path_elements : std::list<std::string> ();
   }
 
-  string_vector
+  std::list<std::string>
   directory_path::all_directories (void)
   {
-    int count = 0;
-    string_vector retval;
-
-    if (initialized)
-      {
-        int len = pv.numel ();
+    std::list<std::string> retval;
 
-        int nmax = len > 32 ? len : 32;
-
-        retval.resize (len);
-
-        for (int i = 0; i < len; i++)
+    if (m_initialized)
+      {
+        for (const auto& elt : m_path_elements)
           {
-            str_llist_type *elt_dirs = kpse_element_dirs (pv[i]);
+            str_llist_type *elt_dirs = kpse_element_dirs (elt);
 
             if (elt_dirs)
               {
@@ -96,19 +88,10 @@
                     const std::string elt_dir = STR_LLIST (*dir);
 
                     if (! elt_dir.empty ())
-                      {
-                        if (count == nmax)
-                          nmax *= 2;
-
-                        retval.resize (nmax);
-
-                        retval[count++] = elt_dir;
-                      }
+                      retval.push_back (elt_dir);
                   }
               }
           }
-
-        retval.resize (count);
       }
 
     return retval;
@@ -117,27 +100,30 @@
   std::string
   directory_path::find_first (const std::string& nm)
   {
-    return initialized ? kpse_path_search (p, nm, true) : "";
+    return m_initialized ? kpse_path_search (m_expanded_path, nm, true) : "";
   }
 
-  string_vector
+  std::list<std::string>
   directory_path::find_all (const std::string& nm)
   {
-    return initialized ? kpse_all_path_search (p, nm) : string_vector ();
+    return (m_initialized
+            ? kpse_all_path_search (m_expanded_path, nm)
+            : std::list<std::string> ());
   }
 
   std::string
-  directory_path::find_first_of (const string_vector& names)
+  directory_path::find_first_of (const std::list<std::string>& names)
   {
-    return initialized
-      ? kpse_path_find_first_of (p, names, true) : "";
+    return (m_initialized
+            ? kpse_path_find_first_of (m_expanded_path, names, true) : "");
   }
 
-  string_vector
-  directory_path::find_all_first_of (const string_vector& names)
+  std::list<std::string>
+  directory_path::find_all_first_of (const std::list<std::string>& names)
   {
-    return initialized
-      ? kpse_all_path_find_first_of (p, names) : string_vector ();
+    return (m_initialized
+            ? kpse_all_path_find_first_of (m_expanded_path, names)
+            : std::list<std::string> ());
   }
 
   void
@@ -155,20 +141,14 @@
         octave_kpathsea_initialized = true;
       }
 
-    p = kpse_path_expand (p_default.empty ()
-                          ? p_orig : kpse_expand_default (p_orig, p_default));
-
-    int count = 0;
-    for (kpse_path_iterator pi (p); pi != std::string::npos; pi++)
-      count++;
+    m_expanded_path
+      = kpse_path_expand (m_default_path.empty ()
+                          ? m_orig_path
+                          : kpse_expand_default (m_orig_path, m_default_path));
 
-    pv.resize (count);
-
-    kpse_path_iterator pi (p);
+    for (kpse_path_iterator pi (m_expanded_path); pi != std::string::npos; pi++)
+      m_path_elements.push_back (*pi);
 
-    for (int i = 0; i < count; i++)
-      pv[i] = *pi++;
-
-    initialized = true;
+    m_initialized = true;
   }
 }
--- a/liboctave/util/pathsearch.h	Sat Jun 11 16:37:05 2016 -0400
+++ b/liboctave/util/pathsearch.h	Sat Jun 11 18:44:07 2016 -0400
@@ -25,10 +25,9 @@
 
 #include "octave-config.h"
 
+#include <list>
 #include <string>
 
-#include "str-vec.h"
-
 namespace octave
 {
   class
@@ -38,24 +37,32 @@
   public:
 
     directory_path (const std::string& s = "", const std::string& d = "")
-      : p_orig (s), p_default (d), initialized (false), p (), pv ()
+      : m_orig_path (s), m_default_path (d), m_initialized (false),
+        m_expanded_path (), m_path_elements ()
       {
-        if (! p_orig.empty ())
+        if (! m_orig_path.empty ())
           init ();
       }
 
     directory_path (const directory_path& dp)
-      : p_orig (dp.p_orig), p_default (dp.p_default),
-        initialized (dp.initialized), p (dp.p), pv (dp.pv)
+      : m_orig_path (dp.m_orig_path),
+        m_default_path (dp.m_default_path),
+        m_initialized (dp.m_initialized),
+        m_expanded_path (dp.m_expanded_path),
+        m_path_elements (dp.m_path_elements)
       { }
 
     directory_path& operator = (const directory_path& dp)
       {
-        p_orig = dp.p_orig;
-        p_default = dp.p_default;
-        initialized = dp.initialized;
-        p = dp.p;
-        pv = dp.pv;
+        if (this != &dp)
+          {
+            m_orig_path = dp.m_orig_path;
+            m_default_path = dp.m_default_path;
+            m_initialized = dp.m_initialized;
+            m_expanded_path = dp.m_expanded_path;
+            m_path_elements = dp.m_path_elements;
+          }
+
         return *this;
       }
 
@@ -63,25 +70,29 @@
 
     void set (const std::string& s)
     {
-      initialized = false;
-      p_orig = s;
+      m_initialized = false;
+      m_orig_path = s;
       init ();
     }
 
-    string_vector elements (void);
-    string_vector all_directories (void);
+    std::list<std::string> elements (void);
+
+    std::list<std::string> all_directories (void);
 
     std::string find_first (const std::string&);
+
     std::string find (const std::string& nm) { return find_first (nm); }
 
-    string_vector find_all (const std::string&);
+    std::list<std::string> find_all (const std::string&);
 
-    std::string find_first_of (const string_vector& names);
-    string_vector find_all_first_of (const string_vector& names);
+    std::string find_first_of (const std::list<std::string>& names);
+
+    std::list<std::string>
+    find_all_first_of (const std::list<std::string>& names);
 
     void rehash (void)
     {
-      initialized = false;
+      m_initialized = false;
       init ();
     }
 
@@ -105,21 +116,21 @@
   private:
 
     // The colon separated list that we were given.
-    std::string p_orig;
+    std::string m_orig_path;
 
     // The default path.  If specified, replaces leading, trailing, or
     // doubled colons in p_orig.
-    std::string p_default;
+    std::string m_default_path;
 
-    // TRUE means we've unpacked p.
-    bool initialized;
+    // TRUE means we've unpacked the path p.
+    bool m_initialized;
 
     // A version of the colon separate list on which we have performed
     // tilde, variable, and possibly default path expansion.
-    std::string p;
+    std::string m_expanded_path;
 
     // The elements of the list.
-    string_vector pv;
+    std::list<std::string> m_path_elements;
 
     void init (void);