aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/coding.c27
-rw-r--r--test/src/coding-tests.el11
2 files changed, 37 insertions, 1 deletions
diff --git a/src/coding.c b/src/coding.c
index 1049f1b755a..97a6eb949a8 100644
--- a/src/coding.c
+++ b/src/coding.c
@@ -9471,6 +9471,17 @@ not fully specified.) */)
9471 return code_convert_region (start, end, coding_system, destination, 1, 0); 9471 return code_convert_region (start, end, coding_system, destination, 1, 0);
9472} 9472}
9473 9473
9474/* Whether a string only contains chars in the 0..127 range. */
9475static bool
9476string_ascii_p (Lisp_Object str)
9477{
9478 ptrdiff_t nbytes = SBYTES (str);
9479 for (ptrdiff_t i = 0; i < nbytes; i++)
9480 if (SREF (str, i) > 127)
9481 return false;
9482 return true;
9483}
9484
9474Lisp_Object 9485Lisp_Object
9475code_convert_string (Lisp_Object string, Lisp_Object coding_system, 9486code_convert_string (Lisp_Object string, Lisp_Object coding_system,
9476 Lisp_Object dst_object, bool encodep, bool nocopy, 9487 Lisp_Object dst_object, bool encodep, bool nocopy,
@@ -9502,7 +9513,21 @@ code_convert_string (Lisp_Object string, Lisp_Object coding_system,
9502 chars = SCHARS (string); 9513 chars = SCHARS (string);
9503 bytes = SBYTES (string); 9514 bytes = SBYTES (string);
9504 9515
9505 if (BUFFERP (dst_object)) 9516 if (EQ (dst_object, Qt))
9517 {
9518 /* Fast path for ASCII-only input and an ASCII-compatible coding:
9519 act as identity. */
9520 Lisp_Object attrs = CODING_ID_ATTRS (coding.id);
9521 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))
9522 && (STRING_MULTIBYTE (string)
9523 ? (chars == bytes) : string_ascii_p (string)))
9524 return (nocopy
9525 ? string
9526 : (encodep
9527 ? make_unibyte_string (SDATA (string), bytes)
9528 : make_multibyte_string (SDATA (string), bytes, bytes)));
9529 }
9530 else if (BUFFERP (dst_object))
9506 { 9531 {
9507 struct buffer *buf = XBUFFER (dst_object); 9532 struct buffer *buf = XBUFFER (dst_object);
9508 ptrdiff_t buf_pt = BUF_PT (buf); 9533 ptrdiff_t buf_pt = BUF_PT (buf);
diff --git a/test/src/coding-tests.el b/test/src/coding-tests.el
index 110ff126964..93e6709d442 100644
--- a/test/src/coding-tests.el
+++ b/test/src/coding-tests.el
@@ -383,6 +383,17 @@
383 (should-not (eq (encode-coding-string s nil nil) s)) 383 (should-not (eq (encode-coding-string s nil nil) s))
384 (should (eq (encode-coding-string s nil t) s)))) 384 (should (eq (encode-coding-string s nil t) s))))
385 385
386(ert-deftest coding-nocopy-ascii ()
387 "Check that the NOCOPY parameter works for ASCII-only strings."
388 (let* ((uni (apply #'string (number-sequence 0 127)))
389 (multi (string-to-multibyte uni)))
390 (dolist (s (list uni multi))
391 (dolist (coding '(us-ascii iso-latin-1 utf-8))
392 (should-not (eq (decode-coding-string s coding nil) s))
393 (should-not (eq (encode-coding-string s coding nil) s))
394 (should (eq (decode-coding-string s coding t) s))
395 (should (eq (encode-coding-string s coding t) s))))))
396
386;; Local Variables: 397;; Local Variables:
387;; byte-compile-warnings: (not obsolete) 398;; byte-compile-warnings: (not obsolete)
388;; End: 399;; End: