Ejemplo n.º 1
0
int BLI_str_cursor_step_next_utf8(const char *str, size_t maxlen, int *pos)
{
	const char *str_end = str + (maxlen + 1);
	const char *str_pos = str + (*pos);
	const char *str_next = BLI_str_find_next_char_utf8(str_pos, str_end);
	if (str_next) {
		(*pos) += (str_next - str_pos);
		if ((*pos) > maxlen) {
			(*pos) = maxlen;
		}
		return TRUE;
	}

	return FALSE;
}
Ejemplo n.º 2
0
/* another variant that steps over the index,
 * note, currently this also falls back to latin1 for text drawing. */
unsigned int BLI_str_utf8_as_unicode_step(const char *p, size_t *index)
{
	int i, mask = 0, len;
	unsigned int result;
	unsigned char c;

	p += *index;
	c= (unsigned char) *p;

	UTF8_COMPUTE (c, mask, len);
	if (len == -1) {
		/* when called with NULL end, result will never be NULL,
		 * checks for a NULL character */
		char *p_next= BLI_str_find_next_char_utf8(p, NULL);
		/* will never return the same pointer unless '\0',
		 * eternal loop is prevented */
		*index += (size_t)(p_next - p);
		return BLI_UTF8_ERR;
	}

	/* this is tricky since there are a few ways we can bail out of bad unicode
	 * values, 3 possible solutions. */
#if 0
	UTF8_GET (result, p, i, mask, len, BLI_UTF8_ERR);
#elif 1
	/* WARNING: this is NOT part of glib, or supported by similar functions.
	 * this is added for text drawing because some filepaths can have latin1
	 * characters */
	UTF8_GET (result, p, i, mask, len, BLI_UTF8_ERR);
	if (result == BLI_UTF8_ERR) {
		len= 1;
		result= *p;
	}
	/* end warning! */
#else
	/* without a fallback like '?', text drawing will stop on this value */
	UTF8_GET (result, p, i, mask, len, '?');
#endif

	*index += len;
	return result;
}
Ejemplo n.º 3
0
size_t BLI_strncpy_wchar_from_utf8(wchar_t *dst_w, const char *src_c, const size_t maxcpy)
{
	int len=0;

	if (dst_w==NULL || src_c==NULL) return(0);

	while (*src_c && len < maxcpy) {
		size_t step= 0;
		unsigned int unicode= BLI_str_utf8_as_unicode_and_size(src_c, &step);
		if (unicode != BLI_UTF8_ERR) {
			*dst_w= (wchar_t)unicode;
			src_c += step;
		}
		else {
			*dst_w = '?';
			src_c= BLI_str_find_next_char_utf8(src_c, NULL);
		}
		dst_w++;
		len++;
	}
	return len;
}