/* convert integer to a string of length 1 */ static void char_cmd (SLwchar_Type *x) /*{{{*/ { SLuchar_Type buf[SLUTF8_MAX_MBLEN + 1]; int is_byte; is_byte = ((signed)*x < 0); if (is_byte) { buf[0] = (SLuchar_Type) (-(signed)*x); buf[1] = 0; } else if ((_pSLinterp_UTF8_Mode == 0) || (*x < 0x80)) { buf[0] = (SLuchar_Type) *x; buf[1] = 0; } else { SLuchar_Type *p; p = SLutf8_encode (*x, buf, SLUTF8_MAX_MBLEN); if (p == NULL) p = buf; *p = 0; } SLang_push_string ((char *)buf); }
/* UTF-8 Encode the bytes between b and bmax storing the results in the * buffer defined by u and umax, returning the position following the * last encoded character. Upon return, *np is set to the number of bytes * sucessfully encoded. */ SLuchar_Type *SLutf8_encode_bytes (unsigned char *b, unsigned char *bmax, SLuchar_Type *u, unsigned int ulen, unsigned int *np) { unsigned char *bstart = b; SLuchar_Type *umax = u + ulen; while (b < bmax) { SLuchar_Type *u1; if (0 == (*b & 0x80)) { if (u >= umax) break; *u++ = *b++; continue; } if (NULL == (u1 = SLutf8_encode (*b, u, umax - u))) break; u = u1; b++; } *np = b - bstart; if (u < umax) *u = 0; return u; }
/* Like SLutf8_encode, but null terminates the result. * At least SLUTF8_MAX_MBLEN+1 bytes assumed. */ SLuchar_Type *SLutf8_encode_null_terminate (SLwchar_Type w, SLuchar_Type *u) { SLuchar_Type *p; p = SLutf8_encode (w, u, SLUTF8_MAX_MBLEN); if (p != NULL) *p = 0; return p; }
void SLsmg_write_char (SLwchar_Type ch) { unsigned char u[SLUTF8_MAX_MBLEN]; unsigned char *umax; if ((ch < 0x80) || (UTF8_Mode == 0)) { u[0] = (unsigned char) ch; SLsmg_write_chars (u, u+1); return; } if (NULL == (umax = SLutf8_encode (ch, u, SLUTF8_MAX_MBLEN))) return; SLsmg_write_chars (u, umax); }
/* Returns an SLstring */ SLstr_Type *SLutf8_subst_wchar (SLuchar_Type *u, SLuchar_Type *umax, SLwchar_Type wch, unsigned int pos, int ignore_combining) { SLuchar_Type *a, *a1, *b; unsigned int dpos; SLuchar_Type buf[SLUTF8_MAX_MBLEN+1]; SLstr_Type *c; unsigned int n1, n2, n3, len; a = SLutf8_skip_chars (u, umax, pos, &dpos, ignore_combining); if ((dpos != pos) || (a == umax)) { _pSLang_verror (SL_INDEX_ERROR, "Specified character position is invalid for string"); return NULL; } a1 = SLutf8_skip_chars (a, umax, 1, NULL, ignore_combining); b = SLutf8_encode (wch, buf, SLUTF8_MAX_MBLEN); if (b == NULL) { _pSLang_verror (SL_UNICODE_ERROR, "Unable to encode wchar 0x%lX", (unsigned long)wch); return NULL; } n1 = (a-u); n2 = (b-buf); n3 = (umax-a1); len = n1 + n2 + n3; c = _pSLallocate_slstring (len); if (c == NULL) return NULL; memcpy (c, (char *)u, n1); memcpy (c+n1, (char *)buf, n2); memcpy (c+n1+n2, (char *)a1, n3); c[len] = 0; /* No need to worry about this failing-- it frees its argument */ return _pSLcreate_via_alloced_slstring (c, len); }
void SLsmg_fill_region (int r, int c, unsigned int dr, unsigned int dc, SLwchar_Type wch) { static unsigned char buf[16]; unsigned char ubuf[16*SLUTF8_MAX_MBLEN]; unsigned char *b, *bmax; int count; int dcmax, rmax; unsigned int wchlen; if (Smg_Inited == 0) return; SLsmg_gotorc (r, c); r = This_Row; c = This_Col; dcmax = Screen_Cols - This_Col; if (dcmax < 0) return; if (dc > (unsigned int) dcmax) dc = (unsigned int) dcmax; rmax = This_Row + (int)dr; if (rmax > (int)Screen_Rows) rmax = (int)Screen_Rows; if ((wch < 0x80) || (UTF8_Mode == 0)) { if (buf[0] != (unsigned char) wch) memset ((char *) buf, (unsigned char) wch, sizeof(buf)); b = buf; bmax = buf + sizeof (buf); wchlen = 1; } else { unsigned int i; b = ubuf; bmax = SLutf8_encode (wch, b, SLUTF8_MAX_MBLEN); if (bmax == NULL) { bmax = ubuf; *bmax++ = '?'; } wchlen = (unsigned int) (bmax - b); for (i = 1; i < 16; i++) { memcpy (bmax, b, wchlen); bmax += wchlen; } } for (This_Row = r; This_Row < rmax; This_Row++) { This_Col = c; count = dc / 16; SLsmg_write_chars (b, b + wchlen * (dc % 16)); while (count-- > 0) { SLsmg_write_chars (b, bmax); } } This_Row = r; }
static SLuchar_Type *xform_utf8 (SLuchar_Type *u, SLuchar_Type *umax, SLwchar_Type (*fun)(SLwchar_Type)) { SLuchar_Type *buf, *p; unsigned int malloced_len, len; if (umax < u) return NULL; len = 0; p = buf = NULL; malloced_len = 0; while (1) { SLwchar_Type w; SLuchar_Type *u1; unsigned int nconsumed; if (malloced_len <= len + SLUTF8_MAX_MBLEN) { SLuchar_Type *newbuf; malloced_len += 1 + (umax - u) + SLUTF8_MAX_MBLEN; newbuf = (SLuchar_Type *)SLrealloc ((char *)buf, malloced_len); if (newbuf == NULL) { SLfree ((char *)buf); return NULL; } buf = newbuf; p = buf + len; } if (u >= umax) { *p = 0; p = (SLuchar_Type *) SLang_create_nslstring ((char *)buf, len); SLfree ((char *)buf); return p; } if (NULL == (u1 = SLutf8_decode (u, umax, &w, &nconsumed))) { /* Invalid sequence */ memcpy ((char *) p, u, nconsumed); p += nconsumed; len += nconsumed; u1 = u + nconsumed; } else { SLuchar_Type *p1; p1 = SLutf8_encode ((*fun)(w), p, malloced_len); if (p1 == NULL) { SLfree ((char *)buf); _pSLang_verror (SL_INTERNAL_ERROR, "SLutf8_encode returned NULL"); return NULL; } len += p1 - p; p = p1; } u = u1; } }