return check_utf_8 (&coding) != -1;
}
+/* Like make_string, but always returns a multibyte Lisp string, and
+ avoids decoding if TEXT encoded in UTF-8. */
+
Lisp_Object
-make_utf8_string (const char *data, ptrdiff_t size)
+make_string_from_utf8 (const char *text, ptrdiff_t nbytes)
{
ptrdiff_t chars, bytes;
- parse_str_as_multibyte ((const unsigned char *) data, size, &chars, &bytes);
- /* If DATA is a valid UTF-8 string, we can convert it to a Lisp
+ parse_str_as_multibyte ((const unsigned char *) text, nbytes,
+ &chars, &bytes);
+ /* If TEXT is a valid UTF-8 string, we can convert it to a Lisp
string directly. Otherwise, we need to decode it. */
- if (chars == size || bytes == size)
- return make_specified_string (data, chars, size, true);
+ if (chars == nbytes || bytes == nbytes)
+ return make_specified_string (text, chars, nbytes, true);
else
{
struct coding_system coding;
setup_coding_system (Qutf_8_unix, &coding);
coding.mode |= CODING_MODE_LAST_BLOCK;
- coding.source = (const unsigned char *) data;
- decode_coding_object (&coding, Qnil, 0, 0, size, size, Qt);
+ coding.source = (const unsigned char *) text;
+ decode_coding_object (&coding, Qnil, 0, 0, nbytes, nbytes, Qt);
return coding.dst_object;
}
}
extern bool raw_text_coding_system_p (struct coding_system *);
extern Lisp_Object coding_inherit_eol_type (Lisp_Object, Lisp_Object);
extern Lisp_Object complement_process_encoding_system (Lisp_Object);
-extern Lisp_Object make_utf8_string (const char *, ptrdiff_t);
+extern Lisp_Object make_string_from_utf8 (const char *, ptrdiff_t);
extern void decode_coding_gap (struct coding_system *,
ptrdiff_t, ptrdiff_t);
return 0x10000 + (low - 0xDC00) + ((high - 0xD800) * 0x400);
}
-/* Create a multibyte Lisp string from the NUL-terminated UTF-8 string
- beginning at DATA. If the string is not a valid UTF-8 string, an
- unspecified string is returned. */
+/* Like build_string, but always returns a multibyte string, and is
+ optimized for speed when STR is a UTF-8 encoded text string. */
INLINE Lisp_Object
-build_utf8_string (const char *data)
+build_string_from_utf8 (const char *str)
{
- return make_utf8_string (data, strlen (data));
+ return make_string_from_utf8 (str, strlen (str));
}
function->data = data;
if (documentation)
- function->documentation = build_utf8_string (documentation);
+ function->documentation = build_string_from_utf8 (documentation);
Lisp_Object result;
XSET_MODULE_FUNCTION (result, function);
MODULE_FUNCTION_BEGIN (NULL);
if (! (0 <= length && length <= STRING_BYTES_BOUND))
overflow_error ();
- Lisp_Object lstr = make_utf8_string (str, length);
+ Lisp_Object lstr = make_string_from_utf8 (str, length);
return lisp_to_value (env, lstr);
}
#endif
-/* Note that all callers of make_utf8_string and build_utf8_string
+/* Note that all callers of make_string_from_utf8 and build_string_from_utf8
below either pass only value UTF-8 strings or use the functionf for
formatting error messages; in the latter case correctness isn't
critical. */
symbol = Qjson_parse_error;
#endif
xsignal (symbol,
- list5 (build_utf8_string (error->text),
- build_utf8_string (error->source), INT_TO_INTEGER (error->line),
- INT_TO_INTEGER (error->column), INT_TO_INTEGER (error->position)));
+ list5 (build_string_from_utf8 (error->text),
+ build_string_from_utf8 (error->source),
+ INT_TO_INTEGER (error->line),
+ INT_TO_INTEGER (error->column),
+ INT_TO_INTEGER (error->position)));
}
static void
json_out_of_memory ();
record_unwind_protect_ptr (json_free, string);
- return unbind_to (count, build_utf8_string (string));
+ return unbind_to (count, build_string_from_utf8 (string));
}
struct json_buffer_and_size
case JSON_REAL:
return make_float (json_real_value (json));
case JSON_STRING:
- return make_utf8_string (json_string_value (json),
- json_string_length (json));
+ return make_string_from_utf8 (json_string_value (json),
+ json_string_length (json));
case JSON_ARRAY:
{
if (++lisp_eval_depth > max_lisp_eval_depth)
json_t *value;
json_object_foreach (json, key_str, value)
{
- Lisp_Object key = build_utf8_string (key_str);
+ Lisp_Object key = build_string_from_utf8 (key_str);
EMACS_UINT hash;
ptrdiff_t i = hash_lookup (h, key, &hash);
/* Keys in JSON objects are unique, so the key can't
json_t *value;
json_object_foreach (json, key_str, value)
{
- Lisp_Object key = Fintern (build_utf8_string (key_str), Qnil);
+ Lisp_Object key
+ = Fintern (build_string_from_utf8 (key_str), Qnil);
result
= Fcons (Fcons (key, json_to_lisp (value, conf)),
result);