示例#1
0
文件: charset.c 项目: Gaelan/neovim
/*
 * Translate a string into allocated memory, replacing special chars with
 * printable chars.  Returns NULL when out of memory.
 */
char_u *transstr(char_u *s)
{
  char_u      *res;
  char_u      *p;
  int l, len, c;
  char_u hexbuf[11];

  if (has_mbyte) {
    /* Compute the length of the result, taking account of unprintable
     * multi-byte characters. */
    len = 0;
    p = s;
    while (*p != NUL) {
      if ((l = (*mb_ptr2len)(p)) > 1) {
        c = (*mb_ptr2char)(p);
        p += l;
        if (vim_isprintc(c))
          len += l;
        else {
          transchar_hex(hexbuf, c);
          len += (int)STRLEN(hexbuf);
        }
      } else   {
        l = byte2cells(*p++);
        if (l > 0)
          len += l;
        else
          len += 4;             /* illegal byte sequence */
      }
    }
    res = alloc((unsigned)(len + 1));
  } else
    res = alloc((unsigned)(vim_strsize(s) + 1));
  if (res != NULL) {
    *res = NUL;
    p = s;
    while (*p != NUL) {
      if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) {
        c = (*mb_ptr2char)(p);
        if (vim_isprintc(c))
          STRNCAT(res, p, l);           /* append printable multi-byte char */
        else
          transchar_hex(res + STRLEN(res), c);
        p += l;
      } else
        STRCAT(res, transchar_byte(*p++));
    }
  }
  return res;
}
示例#2
0
/*
 * Translate any special characters in buf[bufsize] in-place.
 * The result is a string with only printable characters, but if there is not
 * enough room, not all characters will be translated.
 */
    void
trans_characters(
    char_u	*buf,
    int		bufsize)
{
    int		len;		/* length of string needing translation */
    int		room;		/* room in buffer after string */
    char_u	*trs;		/* translated character */
    int		trs_len;	/* length of trs[] */

    len = (int)STRLEN(buf);
    room = bufsize - len;
    while (*buf != 0)
    {
# ifdef FEAT_MBYTE
	/* Assume a multi-byte character doesn't need translation. */
	if (has_mbyte && (trs_len = (*mb_ptr2len)(buf)) > 1)
	    len -= trs_len;
	else
# endif
	{
	    trs = transchar_byte(*buf);
	    trs_len = (int)STRLEN(trs);
	    if (trs_len > 1)
	    {
		room -= trs_len - 1;
		if (room <= 0)
		    return;
		mch_memmove(buf + trs_len, buf + 1, (size_t)len);
	    }
	    mch_memmove(buf, trs, (size_t)trs_len);
	    --len;
	}
	buf += trs_len;
    }
}
示例#3
0
文件: charset.c 项目: Saneyan/neovim
/// Translate a string into allocated memory, replacing special chars with
/// printable chars.  Returns NULL when out of memory.
///
/// @param s
///
/// @return translated string
char_u *transstr(char_u *s)
{
  char_u *res;
  char_u *p;
  int l, c;
  char_u hexbuf[11];

  if (has_mbyte) {
    // Compute the length of the result, taking account of unprintable
    // multi-byte characters.
    size_t len = 0;
    p = s;

    while (*p != NUL) {
      if ((l = (*mb_ptr2len)(p)) > 1) {
        c = (*mb_ptr2char)(p);
        p += l;

        if (vim_isprintc(c)) {
          len += l;
        } else {
          transchar_hex(hexbuf, c);
          len += STRLEN(hexbuf);
        }
      } else {
        l = byte2cells(*p++);

        if (l > 0) {
          len += l;
        } else {
          // illegal byte sequence
          len += 4;
        }
      }
    }
    res = xmallocz(len);
  } else {
    res = xmallocz(vim_strsize(s));
  }

  *res = NUL;
  p = s;

  while (*p != NUL) {
    if (has_mbyte && ((l = (*mb_ptr2len)(p)) > 1)) {
      c = (*mb_ptr2char)(p);

      if (vim_isprintc(c)) {
        // append printable multi-byte char
        STRNCAT(res, p, l);
      } else {
        transchar_hex(res + STRLEN(res), c);
      }
      p += l;
    } else {
      STRCAT(res, transchar_byte(*p++));
    }
  }

  return res;
}