emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 865535a: Make `number-at-point' work for more hex numbers


From: Lars Ingebrigtsen
Subject: master 865535a: Make `number-at-point' work for more hex numbers
Date: Fri, 16 Jul 2021 05:47:44 -0400 (EDT)

branch: master
commit 865535a24cd07efee3c2d323de6e9baae8bc817d
Author: Remington Furman <remington@remcycles.net>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Make `number-at-point' work for more hex numbers
    
    * lisp/thingatpt.el (number-at-point): Rewrite to actually catch
    the hex numbers (bug#49588).
    
    Copyright-paperwork-exempt: yes
---
 lisp/thingatpt.el            | 16 ++++++++--------
 test/lisp/thingatpt-tests.el | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/lisp/thingatpt.el b/lisp/thingatpt.el
index 8ca0f42..4c2470f 100644
--- a/lisp/thingatpt.el
+++ b/lisp/thingatpt.el
@@ -677,14 +677,14 @@ Signal an error if the entire string was not used."
   "Return the number at point, or nil if none is found.
 Decimal numbers like \"14\" or \"-14.5\", as well as hex numbers
 like \"0xBEEF09\" or \"#xBEEF09\", are recognized."
-  (when (thing-at-point-looking-at
-         "\\(-?[0-9]+\\.?[0-9]*\\)\\|\\(0x\\|#x\\)\\([a-zA-Z0-9]+\\)" 500)
-    (if (match-beginning 1)
-        (string-to-number
-         (buffer-substring (match-beginning 1) (match-end 1)))
-      (string-to-number
-       (buffer-substring (match-beginning 3) (match-end 3))
-       16))))
+  (cond
+   ((thing-at-point-looking-at "\\(0x\\|#x\\)\\([a-fA-F0-9]+\\)" 500)
+    (string-to-number
+     (buffer-substring (match-beginning 2) (match-end 2))
+     16))
+   ((thing-at-point-looking-at "-?[0-9]+\\.?[0-9]*" 500)
+    (string-to-number
+     (buffer-substring (match-beginning 0) (match-end 0))))))
 
 (put 'number 'thing-at-point 'number-at-point)
 ;;;###autoload
diff --git a/test/lisp/thingatpt-tests.el b/test/lisp/thingatpt-tests.el
index 07eb8bb..fba6f21 100644
--- a/test/lisp/thingatpt-tests.el
+++ b/test/lisp/thingatpt-tests.el
@@ -190,4 +190,37 @@ position to retrieve THING.")
     (goto-char 2)
     (should (eq (symbol-at-point) nil))))
 
+(defun test--number (number pos)
+  (with-temp-buffer
+    (insert (format "%s\n" number))
+    (goto-char (point-min))
+    (forward-char pos)
+    (number-at-point)))
+
+(ert-deftest test-numbers-none ()
+  (should (equal (test--number "foo" 0) nil)))
+
+(ert-deftest test-numbers-decimal ()
+  (should (equal (test--number "42" 0) 42))
+  (should (equal (test--number "42" 1) 42))
+  (should (equal (test--number "42" 2) 42)))
+
+(ert-deftest test-numbers-hex-lisp ()
+  (should (equal (test--number "#x42" 0) 66))
+  (should (equal (test--number "#x42" 1) 66))
+  (should (equal (test--number "#x42" 2) 66))
+  (should (equal (test--number "#xf00" 0) 3840))
+  (should (equal (test--number "#xf00" 1) 3840))
+  (should (equal (test--number "#xf00" 2) 3840))
+  (should (equal (test--number "#xf00" 3) 3840)))
+
+(ert-deftest test-numbers-hex-c ()
+  (should (equal (test--number "0x42" 0) 66))
+  (should (equal (test--number "0x42" 1) 66))
+  (should (equal (test--number "0x42" 2) 66))
+  (should (equal (test--number "0xf00" 0) 3840))
+  (should (equal (test--number "0xf00" 1) 3840))
+  (should (equal (test--number "0xf00" 2) 3840))
+  (should (equal (test--number "0xf00" 3) 3840)))
+
 ;;; thingatpt.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]