+ Simple examples for encoding="UTF-8", max_len=6:
+
+ head="abc", tail="xyz" => 3
+ head="abcd", tail="xyz" => 3 ("d" dropped).
+ head="abc", tail="uvwxyz" => 0 ("abc" dropped).
+ head="abc", tail="tuvwxyz" => 0 ("abc" dropped).
+
+ Examples for encoding="ISO-8859-1", max_len=6:
+
+ head="éèä", tail="xyz" => 6
+ (each letter in head is only 1 byte in ISO-8859-1 even though they
+ each take 2 bytes in UTF-8 encoding)
+*/
+static size_t
+utf8_encoding_concat__ (const char *head, size_t head_len,
+ const char *tail, size_t tail_len,
+ const char *encoding, size_t max_len,
+ char **resultp)
+{
+ *resultp = NULL;
+ if (head_len == 0)
+ return 0;
+ else if (encoding == NULL || !c_strcasecmp (encoding, "UTF-8"))
+ {
+ if (head_len + tail_len <= max_len)
+ return head_len;
+ else if (tail_len >= max_len)
+ return 0;
+ else
+ {
+ size_t copy_len;
+ size_t prev;
+ size_t ofs;
+ int mblen;
+
+ copy_len = 0;
+ for (ofs = u8_mbtouc (&prev, CHAR_CAST (const uint8_t *, head),
+ head_len);
+ ofs <= max_len - tail_len;
+ ofs += mblen)
+ {
+ ucs4_t next;
+
+ mblen = u8_mbtouc (&next,
+ CHAR_CAST (const uint8_t *, head + ofs),
+ head_len - ofs);
+ if (uc_is_grapheme_break (prev, next))
+ copy_len = ofs;
+
+ prev = next;
+ }
+
+ return copy_len;
+ }
+ }
+ else
+ {
+ char *result;
+
+ result = (tail_len > 0
+ ? xconcat2 (head, head_len, tail, tail_len)
+ : CONST_CAST (char *, head));
+ if (recode_string_len (encoding, "UTF-8", result,
+ head_len + tail_len) <= max_len)
+ {
+ *resultp = result != head ? result : NULL;
+ return head_len;
+ }
+ else
+ {
+ bool correct_result = false;
+ size_t copy_len;
+ size_t prev;
+ size_t ofs;
+ int mblen;
+
+ copy_len = 0;
+ for (ofs = u8_mbtouc (&prev, CHAR_CAST (const uint8_t *, head),
+ head_len);
+ ofs <= head_len;
+ ofs += mblen)
+ {
+ ucs4_t next;
+
+ mblen = u8_mbtouc (&next,
+ CHAR_CAST (const uint8_t *, head + ofs),
+ head_len - ofs);
+ if (uc_is_grapheme_break (prev, next))
+ {
+ if (result != head)
+ {
+ memcpy (result, head, ofs);
+ memcpy (result + ofs, tail, tail_len);
+ result[ofs + tail_len] = '\0';
+ }
+
+ if (recode_string_len (encoding, "UTF-8", result,
+ ofs + tail_len) <= max_len)
+ {
+ correct_result = true;
+ copy_len = ofs;
+ }
+ else
+ correct_result = false;
+ }
+
+ prev = next;
+ }
+
+ if (result != head)
+ {
+ if (correct_result)
+ *resultp = result;
+ else
+ free (result);
+ }
+
+ return copy_len;
+ }