changeset 11887:84fb5019fb38 release-3-0-x

Fast return case for file_ops::tilde_expand
author David Bateman <dbateman@free.fr>
date Thu, 20 Nov 2008 08:41:56 +0100
parents 53e846af744d
children 8dc2fa08600c
files liboctave/file-ops.cc
diffstat 1 files changed, 36 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/file-ops.cc	Thu Nov 20 08:41:35 2008 +0100
+++ b/liboctave/file-ops.cc	Thu Nov 20 08:41:56 2008 +0100
@@ -746,53 +746,58 @@
 std::string
 file_ops::tilde_expand (const std::string& name)
 {
-  std::string result;
-
-  size_t name_len = name.length ();
-
-  // Scan through S expanding tildes as we come to them.
-
-  size_t pos = 0;
-
-  while (1)
+  if (name.find ('~') == std::string::npos)
+    return name;
+  else
     {
-      if (pos > name_len)
-	break;
+      std::string result;
+
+      size_t name_len = name.length ();
 
-      size_t len;
+      // Scan through S expanding tildes as we come to them.
+
+      size_t pos = 0;
 
-      // Make START point to the tilde which starts the expansion.
-
-      size_t start = tilde_find_prefix (name.substr (pos), len);
+      while (1)
+	{
+	  if (pos > name_len)
+	    break;
 
-      result.append (name.substr (pos, start));
+	  size_t len;
+
+	  // Make START point to the tilde which starts the expansion.
 
-      // Advance STRING to the starting tilde.
+	  size_t start = tilde_find_prefix (name.substr (pos), len);
+
+	  result.append (name.substr (pos, start));
 
-      pos += start;
+	  // Advance STRING to the starting tilde.
 
-      // Make FINI be the index of one after the last character of the
-      // username.
+	  pos += start;
+
+	  // Make FINI be the index of one after the last character of the
+	  // username.
 
-      size_t fini = tilde_find_suffix (name.substr (pos));
+	  size_t fini = tilde_find_suffix (name.substr (pos));
 
-      // If both START and FINI are zero, we are all done.
+	  // If both START and FINI are zero, we are all done.
 
-      if (! (start || fini))
-	break;
+	  if (! (start || fini))
+	    break;
 
-      // Expand the entire tilde word, and copy it into RESULT.
+	  // Expand the entire tilde word, and copy it into RESULT.
 
-      std::string tilde_word = name.substr (pos, fini);
+	  std::string tilde_word = name.substr (pos, fini);
 
-      pos += fini;
+	  pos += fini;
 
-      std::string expansion = tilde_expand_word (tilde_word);
+	  std::string expansion = tilde_expand_word (tilde_word);
 
-      result.append (expansion);
+	  result.append (expansion);
+	}
+
+      return result;
     }
-
-  return result;
 }
 
 // A vector version of the above.