+/* Returns true if a variable named NAME may be inserted in DICT;
+ that is, if there is not already a variable with that name in
+ DICT and if NAME is not a reserved word. (The caller's checks
+ have already verified that NAME is otherwise acceptable as a
+ variable name.) */
+static bool
+var_name_is_insertable (const struct dictionary *dict, const char *name)
+{
+ return (dict_lookup_var (dict, name) == NULL
+ && lex_id_to_token (ss_cstr (name)) == T_ID);
+}
+
+static char *
+make_hinted_name (const struct dictionary *dict, const char *hint)
+{
+ size_t hint_len = strlen (hint);
+ bool dropped = false;
+ char *root, *rp;
+ size_t ofs;
+ int mblen;
+
+ /* The allocation size here is OK: characters that are copied directly fit
+ OK, and characters that are not copied directly are replaced by a single
+ '_' byte. If u8_mbtouc() replaces bad input by 0xfffd, then that will get
+ replaced by '_' too. */
+ root = rp = xmalloc (hint_len + 1);
+ for (ofs = 0; ofs < hint_len; ofs += mblen)
+ {
+ ucs4_t uc;
+
+ mblen = u8_mbtouc (&uc, CHAR_CAST (const uint8_t *, hint + ofs),
+ hint_len - ofs);
+ if (rp == root
+ ? lex_uc_is_id1 (uc) && uc != '$'
+ : lex_uc_is_idn (uc))
+ {
+ if (dropped)
+ {
+ *rp++ = '_';
+ dropped = false;
+ }
+ rp += u8_uctomb (CHAR_CAST (uint8_t *, rp), uc, 6);
+ }
+ else if (rp != root)
+ dropped = true;
+ }
+ *rp = '\0';
+
+ if (root[0] != '\0')
+ {
+ unsigned long int i;
+
+ if (var_name_is_insertable (dict, root))
+ return root;
+
+ for (i = 0; i < ULONG_MAX; i++)
+ {
+ char suffix[INT_BUFSIZE_BOUND (i) + 1];
+ char *name;
+
+ suffix[0] = '_';
+ if (!str_format_26adic (i + 1, true, &suffix[1], sizeof suffix - 1))
+ NOT_REACHED ();
+
+ name = utf8_encoding_concat (root, suffix, dict->encoding, 64);
+ if (var_name_is_insertable (dict, name))
+ {
+ free (root);
+ return name;
+ }
+ free (name);
+ }
+ }
+
+ free (root);
+
+ return NULL;
+}
+
+static char *
+make_numeric_name (const struct dictionary *dict, unsigned long int *num_start)
+{
+ unsigned long int number;
+
+ for (number = num_start != NULL ? MAX (*num_start, 1) : 1;
+ number < ULONG_MAX;
+ number++)
+ {
+ char name[3 + INT_STRLEN_BOUND (number) + 1];
+
+ sprintf (name, "VAR%03lu", number);
+ if (dict_lookup_var (dict, name) == NULL)
+ {
+ if (num_start != NULL)
+ *num_start = number + 1;
+ return xstrdup (name);
+ }
+ }
+
+ NOT_REACHED ();
+}
+
+
+/* Devises and returns a variable name unique within DICT. The variable name
+ is owned by the caller, which must free it with free() when it is no longer
+ needed.
+
+ HINT, if it is non-null, is used as a suggestion that will be
+ modified for suitability as a variable name and for
+ uniqueness.
+
+ If HINT is null or entirely unsuitable, a name in the form
+ "VAR%03d" will be generated, where the smallest unused integer
+ value is used. If NUM_START is non-null, then its value is
+ used as the minimum numeric value to check, and it is updated
+ to the next value to be checked.
+*/
+char *
+dict_make_unique_var_name (const struct dictionary *dict, const char *hint,
+ unsigned long int *num_start)
+{
+ if (hint != NULL)
+ {
+ char *hinted_name = make_hinted_name (dict, hint);
+ if (hinted_name != NULL)
+ return hinted_name;
+ }
+ return make_numeric_name (dict, num_start);