diff src/variables.cc @ 4962:0ab18cbe8b5f

[project @ 2004-09-02 16:56:17 by jwe]
author jwe
date Thu, 02 Sep 2004 16:56:17 +0000 (2004-09-02)
parents ed0f3cb6d3d4
children 352d228d409b
line wrap: on
line diff
--- a/src/variables.cc
+++ b/src/variables.cc
@@ -662,14 +662,28 @@
   return retval;
 }
 
+#define GET_IDX(LEN) \
+  static_cast<int> ((LEN-1) * static_cast<double> (rand ()) / RAND_MAX)
+
 std::string
 unique_symbol_name (const std::string& basename)
 {
-  // XXX FIXME XXX Can we be smarter than just adding characters?
-  std::string name = basename;
-  while (symbol_exist (name, "any"))
-    name.append ("X");
-  return name;
+  static const std::string alpha
+    = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+  static size_t len = alpha.length ();
+
+  std::string nm = basename + alpha[GET_IDX (len)];
+
+  size_t pos = nm.length ();
+
+  if (nm.substr (0, 2) == "__")
+    nm.append ("__");
+
+  while (symbol_exist (nm, "any"))
+    nm.insert (pos++, 1, alpha[GET_IDX (len)]);
+
+  return nm;
 }
 
 DEFUN (exist, args, ,