diff options
| author | Eli Zaretskii | 2018-04-28 10:27:53 +0300 |
|---|---|---|
| committer | Eli Zaretskii | 2018-04-28 10:27:53 +0300 |
| commit | a7a3918a16c85f89d797d48b09e751ab30e0f032 (patch) | |
| tree | a48809d67e91a60cbdb622f6a4145c8081d4bb1c | |
| parent | c6e6503900534d939dd94b812563c27f22c49b7d (diff) | |
| download | emacs-a7a3918a16c85f89d797d48b09e751ab30e0f032.tar.gz emacs-a7a3918a16c85f89d797d48b09e751ab30e0f032.zip | |
Fix documentation and tests for 'string-distance'
* src/fns.c (Fstring_distance): Doc fix.
* doc/lispref/strings.texi (Text Comparison): Document
'string-distance'.
* etc/NEWS: Fix wording and mark as documented in the manuals.
* test/src/fns-tests.el (test-string-distance): Move from
subr-tests.el and rename.
| -rw-r--r-- | doc/lispref/strings.texi | 22 | ||||
| -rw-r--r-- | etc/NEWS | 3 | ||||
| -rw-r--r-- | src/fns.c | 8 | ||||
| -rw-r--r-- | test/lisp/subr-tests.el | 18 | ||||
| -rw-r--r-- | test/src/fns-tests.el | 18 |
5 files changed, 47 insertions, 22 deletions
diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi index 8a9e27d00ec..70ba1aa613e 100644 --- a/doc/lispref/strings.texi +++ b/doc/lispref/strings.texi | |||
| @@ -673,6 +673,28 @@ of the two strings. The sign is negative if @var{string1} (or its | |||
| 673 | specified portion) is less. | 673 | specified portion) is less. |
| 674 | @end defun | 674 | @end defun |
| 675 | 675 | ||
| 676 | @cindex Levenshtein distance | ||
| 677 | @cindex distance between strings | ||
| 678 | @cindex edit distance between strings | ||
| 679 | @defun string-distance string1 string2 &optional bytecompare | ||
| 680 | This function returns the @dfn{Levenshtein distance} between the | ||
| 681 | source string @var{string1} and the target string @var{string2}. The | ||
| 682 | Levenshtein distance is the number of single-character | ||
| 683 | changes---deletions, insertions, or replacements---required to | ||
| 684 | transform the source string into the target string; it is one possible | ||
| 685 | definition of the @dfn{edit distance} between strings. | ||
| 686 | |||
| 687 | Letter-case of the strings is significant for the computed distance, | ||
| 688 | but their text properties are ignored. If the optional argument | ||
| 689 | @var{bytecompare} is non-@code{nil}, the function calculates the | ||
| 690 | distance in terms of bytes instead of characters. The byte-wise | ||
| 691 | comparison uses the internal Emacs representation of characters, so it | ||
| 692 | will produce inaccurate results for multibyte strings that include raw | ||
| 693 | bytes (@pxref{Text Representations}); make the strings unibyte by | ||
| 694 | encoding them (@pxref{Explicit Encoding}) if you need accurate results | ||
| 695 | with raw bytes. | ||
| 696 | @end defun | ||
| 697 | |||
| 676 | @defun assoc-string key alist &optional case-fold | 698 | @defun assoc-string key alist &optional case-fold |
| 677 | This function works like @code{assoc}, except that @var{key} must be a | 699 | This function works like @code{assoc}, except that @var{key} must be a |
| 678 | string or symbol, and comparison is done using @code{compare-strings}. | 700 | string or symbol, and comparison is done using @code{compare-strings}. |
| @@ -534,7 +534,8 @@ manual for more details. | |||
| 534 | +++ | 534 | +++ |
| 535 | ** New function assoc-delete-all. | 535 | ** New function assoc-delete-all. |
| 536 | 536 | ||
| 537 | ** New function string-distance to calculate Levenshtein distance | 537 | +++ |
| 538 | ** New function 'string-distance' to calculate the Levenshtein distance | ||
| 538 | between two strings. | 539 | between two strings. |
| 539 | 540 | ||
| 540 | ** 'print-quoted' now defaults to t, so if you want to see | 541 | ** 'print-quoted' now defaults to t, so if you want to see |
| @@ -155,9 +155,11 @@ If STRING is multibyte, this may be greater than the length of STRING. */) | |||
| 155 | 155 | ||
| 156 | DEFUN ("string-distance", Fstring_distance, Sstring_distance, 2, 3, 0, | 156 | DEFUN ("string-distance", Fstring_distance, Sstring_distance, 2, 3, 0, |
| 157 | doc: /* Return Levenshtein distance between STRING1 and STRING2. | 157 | doc: /* Return Levenshtein distance between STRING1 and STRING2. |
| 158 | If BYTECOMPARE is nil, compare character of strings. | 158 | The distance is the number of deletions, insertions, and substitutions |
| 159 | If BYTECOMPARE is t, compare byte of strings. | 159 | required to transform STRING1 into STRING2. |
| 160 | Case is significant, but text properties are ignored. */) | 160 | If BYTECOMPARE is nil or omitted, compute distance in terms of characters. |
| 161 | If BYTECOMPARE is non-nil, compute distance in terms of bytes. | ||
| 162 | Letter-case is significant, but text properties are ignored. */) | ||
| 161 | (Lisp_Object string1, Lisp_Object string2, Lisp_Object bytecompare) | 163 | (Lisp_Object string1, Lisp_Object string2, Lisp_Object bytecompare) |
| 162 | 164 | ||
| 163 | { | 165 | { |
diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el index 6b80c743a05..52b61d9fb97 100644 --- a/test/lisp/subr-tests.el +++ b/test/lisp/subr-tests.el | |||
| @@ -281,24 +281,6 @@ indirectly `mapbacktrace'." | |||
| 281 | (should (equal (string-match-p "\\`[[:blank:]]\\'" "\u3000") 0)) | 281 | (should (equal (string-match-p "\\`[[:blank:]]\\'" "\u3000") 0)) |
| 282 | (should-not (string-match-p "\\`[[:blank:]]\\'" "\N{LINE SEPARATOR}"))) | 282 | (should-not (string-match-p "\\`[[:blank:]]\\'" "\N{LINE SEPARATOR}"))) |
| 283 | 283 | ||
| 284 | (ert-deftest subr-tests--string-distance () | ||
| 285 | "Test `string-distance' behavior." | ||
| 286 | ;; ASCII characters are always fine | ||
| 287 | (should (equal 1 (string-distance "heelo" "hello"))) | ||
| 288 | (should (equal 2 (string-distance "aeelo" "hello"))) | ||
| 289 | (should (equal 0 (string-distance "ab" "ab" t))) | ||
| 290 | (should (equal 1 (string-distance "ab" "abc" t))) | ||
| 291 | |||
| 292 | ;; string containing hanzi character, compare by byte | ||
| 293 | (should (equal 6 (string-distance "ab" "ab我她" t))) | ||
| 294 | (should (equal 3 (string-distance "ab" "a我b" t))) | ||
| 295 | (should (equal 3 (string-distance "我" "她" t))) | ||
| 296 | |||
| 297 | ;; string containing hanzi character, compare by character | ||
| 298 | (should (equal 2 (string-distance "ab" "ab我她"))) | ||
| 299 | (should (equal 1 (string-distance "ab" "a我b"))) | ||
| 300 | (should (equal 1 (string-distance "我" "她")))) | ||
| 301 | |||
| 302 | (ert-deftest subr-tests--dolist--wrong-number-of-args () | 284 | (ert-deftest subr-tests--dolist--wrong-number-of-args () |
| 303 | "Test that `dolist' doesn't accept wrong types or length of SPEC, | 285 | "Test that `dolist' doesn't accept wrong types or length of SPEC, |
| 304 | cf. Bug#25477." | 286 | cf. Bug#25477." |
diff --git a/test/src/fns-tests.el b/test/src/fns-tests.el index f8554636bac..0301ceaad52 100644 --- a/test/src/fns-tests.el +++ b/test/src/fns-tests.el | |||
| @@ -575,4 +575,22 @@ | |||
| 575 | :type 'wrong-type-argument) | 575 | :type 'wrong-type-argument) |
| 576 | '(wrong-type-argument plistp (:foo 1 . :bar))))) | 576 | '(wrong-type-argument plistp (:foo 1 . :bar))))) |
| 577 | 577 | ||
| 578 | (ert-deftest test-string-distance () | ||
| 579 | "Test `string-distance' behavior." | ||
| 580 | ;; ASCII characters are always fine | ||
| 581 | (should (equal 1 (string-distance "heelo" "hello"))) | ||
| 582 | (should (equal 2 (string-distance "aeelo" "hello"))) | ||
| 583 | (should (equal 0 (string-distance "ab" "ab" t))) | ||
| 584 | (should (equal 1 (string-distance "ab" "abc" t))) | ||
| 585 | |||
| 586 | ;; string containing hanzi character, compare by byte | ||
| 587 | (should (equal 6 (string-distance "ab" "ab我她" t))) | ||
| 588 | (should (equal 3 (string-distance "ab" "a我b" t))) | ||
| 589 | (should (equal 3 (string-distance "我" "她" t))) | ||
| 590 | |||
| 591 | ;; string containing hanzi character, compare by character | ||
| 592 | (should (equal 2 (string-distance "ab" "ab我她"))) | ||
| 593 | (should (equal 1 (string-distance "ab" "a我b"))) | ||
| 594 | (should (equal 1 (string-distance "我" "她")))) | ||
| 595 | |||
| 578 | (provide 'fns-tests) | 596 | (provide 'fns-tests) |