}
EAPI char *
-eina_unicode_unicode_to_utf8(const Eina_Unicode *uni, int *_len)
+eina_unicode_unicode_to_utf8_range(const Eina_Unicode *uni, int ulen, int *_len)
{
char *buf, *buf2;
const Eina_Unicode *uind;
char *ind;
- int ulen, len;
+ int i, len;
EINA_SAFETY_ON_NULL_RETURN_VAL(uni, NULL);
- ulen = eina_unicode_strlen(uni);
buf = malloc((ulen + 1) * EINA_UNICODE_UTF8_BYTES_PER_CHAR);
if (!buf) return NULL;
len = 0;
- for (uind = uni, ind = buf ; *uind ; uind++)
+ for (uind = uni, ind = buf, i = 0 ; *uind && (i < ulen) ; uind++, i++)
{
if (*uind <= 0x7F) /* 1 byte char */
{
return buf2;
}
+EAPI char *
+eina_unicode_unicode_to_utf8(const Eina_Unicode *uni, int *_len)
+{
+ int len = eina_unicode_strlen(uni);
-
+ return eina_unicode_unicode_to_utf8_range(uni, len, _len);
+}
EAPI Eina_Unicode *eina_unicode_utf8_to_unicode(const char *utf, int *_len) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1) EINA_MALLOC;
/**
+ * Converts an Eina_Unicode string to a newly allocated utf-8 substring at given length.
+ *
+ * @param uni the Eina_Unicode string
+ * @param ulen the length in the unicode string to convert.
+ * @param _len the length byte length of the return utf8 substring.
+ * @return the newly allocated utf-8 substring.
+ * @since 1.17
+ */
+EAPI char * eina_unicode_unicode_to_utf8_range(const Eina_Unicode *uni, int ulen, int *_len) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1) EINA_MALLOC;
+
+/**
* Converts an Eina_Unicode string to a newly allocated utf-8 string.
*
* @param uni the Eina_Unicode string
char c_in[] = "\xD7\x90""\xEF\xB7\xB6""\x80""\xF0\x9F\x91\x99"
"\xFB\xBF\xBF\xBF\xBF""\xFD\xBF\xBF\xBF\xBF\xBF""abc";
char *c_out;
+
+ /* Substring of c_in (offset = 2, length = 3) */
+ char c_sub[] = "\x80""\xF0\x9F\x91\x99""\xFB\xBF\xBF\xBF\xBF";
+ char *c_sub_out;
int len;
eina_init();
fail_if((len != 24) || strcmp(c_in, c_out));
free(c_out);
+ /* Range conversion */
+ c_sub_out = eina_unicode_unicode_to_utf8_range(uni_in + 2, 3, &len);
+ ck_assert_int_eq(len, 10);
+ ck_assert_str_eq(c_sub, c_sub_out);
+
+ c_sub_out = eina_unicode_unicode_to_utf8_range(uni_in, 100, &len);
+ ck_assert_int_eq(len, 24);
+ ck_assert_str_eq(c_in, c_sub_out);
+
+ c_sub_out = eina_unicode_unicode_to_utf8_range(uni_in, 0, &len);
+ ck_assert_int_eq(len, 0);
+ ck_assert_str_eq("", c_sub_out);
+
eina_shutdown();
}
END_TEST