1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-20 11:40:18 +02:00

(scm_print_symbol_name): handle more weird characters by escaping the

symbol name properly.  Thanks to Paul Jarc!
This commit is contained in:
Marius Vollmer 2003-07-27 16:31:11 +00:00
parent 2f4931bf1b
commit c6b49e8964

View file

@ -300,22 +300,29 @@ print_circref (SCM port, scm_print_state *pstate, SCM ref)
void void
scm_print_symbol_name (const char *str, size_t len, SCM port) scm_print_symbol_name (const char *str, size_t len, SCM port)
{ {
size_t pos; /* This points to the first character that has not yet been written to the
* port. */
size_t pos = 0;
/* This points to the character we're currently looking at. */
size_t end; size_t end;
int weird; /* If the name contains weird characters, we'll escape them with
int maybe_weird; * backslashes and set this flag; it indicates that we should surround the
* name with "#{" and "}#". */
int weird = 0;
/* Backslashes are not sufficient to make a name weird, but if a name is
* weird because of other characters, backslahes need to be escaped too.
* The first time we see a backslash, we set maybe_weird, and mw_pos points
* to the backslash. Then if the name turns out to be weird, we re-process
* everything starting from mw_pos. */
int maybe_weird = 0;
size_t mw_pos = 0; size_t mw_pos = 0;
/* If the name is purely numeric, then it's weird as a whole, even though
* none of the individual characters is weird. But we won't know this
* until we reach the end of the name. This flag describes the part of the
* name we've looked at so far. */
int all_digits = 1;
pos = 0; if (len == 0 || str[0] == '\'' || str[0] == ':' || str[len-1] == ':')
weird = 0;
maybe_weird = 0;
/* XXX - Lots of weird symbol names are missed, such as "12" or
"'a". */
if (len == 0)
scm_lfwrite ("#{}#", 4, port);
else if (str[0] == '#' || str[0] == ':' || str[len-1] == ':')
{ {
scm_lfwrite ("#{", 2, port); scm_lfwrite ("#{", 2, port);
weird = 1; weird = 1;
@ -332,8 +339,10 @@ scm_print_symbol_name (const char *str, size_t len, SCM port)
case ')': case ')':
case '"': case '"':
case ';': case ';':
case '#':
case SCM_WHITE_SPACES: case SCM_WHITE_SPACES:
case SCM_LINE_INCREMENTORS: case SCM_LINE_INCREMENTORS:
all_digits = 0;
weird_handler: weird_handler:
if (maybe_weird) if (maybe_weird)
{ {
@ -346,9 +355,7 @@ scm_print_symbol_name (const char *str, size_t len, SCM port)
weird = 1; weird = 1;
} }
if (pos < end) if (pos < end)
{
scm_lfwrite (str + pos, end - pos, port); scm_lfwrite (str + pos, end - pos, port);
}
{ {
char buf[2]; char buf[2];
buf[0] = '\\'; buf[0] = '\\';
@ -358,6 +365,7 @@ scm_print_symbol_name (const char *str, size_t len, SCM port)
pos = end + 1; pos = end + 1;
break; break;
case '\\': case '\\':
all_digits = 0;
if (weird) if (weird)
goto weird_handler; goto weird_handler;
if (!maybe_weird) if (!maybe_weird)
@ -366,14 +374,18 @@ scm_print_symbol_name (const char *str, size_t len, SCM port)
mw_pos = pos; mw_pos = pos;
} }
break; break;
case '}': case '0': case '1': case '2': case '3': case '4':
case '#': case '5': case '6': case '7': case '8': case '9':
if (weird)
goto weird_handler;
break; break;
default: default:
all_digits = 0;
break; break;
} }
if (all_digits)
{
scm_lfwrite ("#{", 2, port);
weird = 1;
}
if (pos < end) if (pos < end)
scm_lfwrite (str + pos, end - pos, port); scm_lfwrite (str + pos, end - pos, port);
if (weird) if (weird)