- length = ds_length (lex_tokstr (lexer));
- if (length > MV_MAX_STRING)
- {
- msg (SE, _("Truncating missing value to maximum "
- "acceptable length (%d bytes)."),
- MV_MAX_STRING);
- length = MV_MAX_STRING;
- }
- memset (value, ' ', MV_MAX_STRING);
- memcpy (value, ds_data (lex_tokstr (lexer)), length);
-
- if (!mv_add_str (&mv, value))
- deferred_errors = true;
+ /* Truncate the string to fit in 8 bytes in the dictionary
+ encoding. */
+ utf8_s = lex_tokcstr (lexer);
+ utf8_len = ss_length (lex_tokss (lexer));
+ utf8_trunc_len = utf8_encoding_trunc_len (utf8_s, encoding,
+ MV_MAX_STRING);
+ if (utf8_trunc_len < utf8_len)
+ msg (SE, _("Truncating missing value to maximum "
+ "acceptable length (%d bytes)."),
+ MV_MAX_STRING);
+
+ /* Recode to dictionary encoding and add. */
+ raw_s = recode_string (encoding, "UTF-8",
+ utf8_s, utf8_trunc_len);
+ if (!mv_add_str (&mv, CHAR_CAST (const uint8_t *, raw_s),
+ strlen (raw_s)))
+ ok = false;
+ free (raw_s);