/* Returns # of chars written. This can be better in char.c, but to do so, we'd better to clean up public interface for ScmWriteContext. TODO: It would be nice to have a mode to print character in unicode character name. */ static size_t write_char(ScmChar ch, ScmPort *port, ScmWriteContext *ctx) { if (SCM_WRITE_MODE(ctx) == SCM_WRITE_DISPLAY) { Scm_PutcUnsafe(ch, port); return 1; } else { const char *cname = NULL; char buf[SPBUFSIZ]; Scm_PutzUnsafe("#\\", -1, port); if (ch <= 0x20) cname = char_names[ch]; else if (ch == 0x7f) cname = "del"; else { switch (Scm_CharGeneralCategory(ch)) { case SCM_CHAR_CATEGORY_Mn: case SCM_CHAR_CATEGORY_Mc: case SCM_CHAR_CATEGORY_Me: case SCM_CHAR_CATEGORY_Zs: case SCM_CHAR_CATEGORY_Zl: case SCM_CHAR_CATEGORY_Zp: case SCM_CHAR_CATEGORY_Cc: case SCM_CHAR_CATEGORY_Cf: case SCM_CHAR_CATEGORY_Cs: case SCM_CHAR_CATEGORY_Co: case SCM_CHAR_CATEGORY_Cn: /* NB: Legacy Gauche uses native character code for #\xNNNN notation, while R7RS uses Unicode codepoint. We eventually need a write mode (legacy or r7rs) and switch the output accordingly---the safe bet is to use #\uNNNN for legacy mode and #\xNNNN for R7RS mode. */ snprintf(buf, SPBUFSIZ, "x%04x", (unsigned int)ch); cname = buf; break; } } if (cname) { Scm_PutzUnsafe(cname, -1, port); return strlen(cname)+2; /* +2 for '#\' */ } else { Scm_PutcUnsafe(ch, port); return 3; /* +2 for '#\' */ } } }
static void charset_print_ch(ScmPort *out, ScmChar ch, int firstp) { if (ch != 0 && ch < 0x80 && (strchr("[]-\\", ch) != NULL || (ch == '^' && firstp))) { Scm_Printf(out, "\\%C", ch); } else { switch (Scm_CharGeneralCategory(ch)) { case SCM_CHAR_CATEGORY_Mn: case SCM_CHAR_CATEGORY_Mc: case SCM_CHAR_CATEGORY_Me: case SCM_CHAR_CATEGORY_Cc: case SCM_CHAR_CATEGORY_Cf: case SCM_CHAR_CATEGORY_Cs: case SCM_CHAR_CATEGORY_Co: case SCM_CHAR_CATEGORY_Cn: if (ch < 0x10000) Scm_Printf(out, "\\u%04lx", ch); else Scm_Printf(out, "\\U%08lx", ch); break; default: Scm_Putc(ch, out); } } }
int Scm_CharNumericP(ScmChar ch) { return (Scm_CharGeneralCategory(ch) == SCM_CHAR_CATEGORY_Nd); }
int Scm_CharTitlecaseP(ScmChar ch) { return (Scm_CharGeneralCategory(ch) == SCM_CHAR_CATEGORY_Lt); }