/* _agstrcanon: * Canonicalize an ordinary string if necessary. */ static char* _agstrcanon (char* arg, char* buf) { char *s = arg; unsigned char uc; char *p = buf; int cnt = 0; int has_special = FALSE; int maybe_num; if (ISEMPTYSTR(arg)) return "\"\""; *p++ = '\"'; uc = *(unsigned char *) s++; maybe_num = (isdigit(uc) || (uc == '.')); while (uc) { if (uc == '\"') { *p++ = '\\'; has_special = TRUE; } else { if (!ISALNUM(uc)) has_special = TRUE; else if (maybe_num && (!isdigit(uc) && (uc != '.'))) has_special = TRUE; } *p++ = (char) uc; uc = *(unsigned char *) s++; cnt++; if (cnt % SMALLBUF == 0) { *p++ = '\\'; *p++ = '\n'; has_special = TRUE; } } *p++ = '\"'; *p = '\0'; if (has_special) return buf; /* use quotes to protect tokens (example, a node named "node") */ if (agtoken(arg) >= 0) return buf; return arg; }
/* _agstrcanon: * Canonicalize an ordinary string if necessary. */ static char* _agstrcanon (char* arg, char* buf) { char *s = arg; unsigned char uc; char *p = buf; int cnt = 0, dotcnt = 0; int has_special = FALSE; int maybe_num; int backslash_pending = FALSE; if (ISEMPTYSTR(arg)) return "\"\""; *p++ = '\"'; uc = *(unsigned char *) s++; maybe_num = _is_number_char(uc) || (uc == '-'); while (uc) { if (uc == '\"') { *p++ = '\\'; has_special = TRUE; } else if (maybe_num) { if (uc == '-') { if (cnt) { maybe_num = FALSE; has_special = TRUE; } } else if (uc == '.') { if (dotcnt++) { maybe_num = FALSE; has_special = TRUE; } } else if (!isdigit(uc)) { maybe_num = FALSE; has_special = TRUE; } } else if (!ISALNUM(uc)) has_special = TRUE; *p++ = (char) uc; uc = *(unsigned char *) s++; cnt++; if (uc && backslash_pending && !((_is_number_char(p[-1]) || isalpha(p[-1]) || (p[-1] == '\\')) && (_is_number_char(uc) || isalpha(uc)))) { *p++ = '\\'; *p++ = '\n'; has_special = TRUE; backslash_pending = FALSE; } else if (uc && cnt % SMALLBUF == 0) { if (!((_is_number_char(p[-1]) || isalpha(p[-1]) || (p[-1] == '\\')) && (_is_number_char(uc) || isalpha(uc)))) { *p++ = '\\'; *p++ = '\n'; has_special = TRUE; } else { backslash_pending = TRUE; } } } *p++ = '\"'; *p = '\0'; if (has_special || ((cnt == 1) && ((*arg == '.') || (*arg == '-')))) return buf; /* use quotes to protect tokens (example, a node named "node") */ if (agtoken(arg) >= 0) return buf; return arg; }