summaryrefslogtreecommitdiff
path: root/src/coding.c
diff options
context:
space:
mode:
authorPhilipp Stephani <phst@google.com>2019-04-28 12:28:27 +0200
committerPhilipp Stephani <phst@google.com>2019-04-28 12:28:27 +0200
commit75ee20364c5ed4c175b13debaa53a2ba14168999 (patch)
tree3bdff98355f169ddf8fe20cc7c60aa13ba682139 /src/coding.c
parentdbe81e16583adcae664871206694573209540286 (diff)
downloademacs-75ee20364c5ed4c175b13debaa53a2ba14168999.tar.gz
emacs-75ee20364c5ed4c175b13debaa53a2ba14168999.tar.bz2
emacs-75ee20364c5ed4c175b13debaa53a2ba14168999.zip
Refactoring: move UTF-8 decoding functions into coding.h.
json_make_string and json_build_string are generally useful and not JSON-specific. Move them to coding.[ch]. * src/coding.h (build_utf8_string): Move from json.c. * src/coding.c (make_utf8_string): Move from json.c. * src/json.c (json_make_string, json_build_string): Move to coding.[ch]. Split out JSON-specific comment. (json_parse_error, Fjson_serialize, json_to_lisp): Fix callers. * src/emacs-module.c (module_make_function, module_make_string): Use new functions. (module_decode, module_decode_copy): Remove.
Diffstat (limited to 'src/coding.c')
-rw-r--r--src/coding.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/coding.c b/src/coding.c
index 2c6b2c4d051..71f687a14e3 100644
--- a/src/coding.c
+++ b/src/coding.c
@@ -6353,6 +6353,25 @@ utf8_string_p (Lisp_Object string)
return check_utf_8 (&coding) != -1;
}
+Lisp_Object
+make_utf8_string (const char *data, ptrdiff_t size)
+{
+ 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
+ string directly. Otherwise, we need to decode it. */
+ if (chars == size || bytes == size)
+ return make_specified_string (data, chars, size, 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);
+ return coding.dst_object;
+ }
+}
/* Detect how end-of-line of a text of length SRC_BYTES pointed by
SOURCE is encoded. If CATEGORY is one of