Browse Source

ob-core: Fix `org-babel--string-to-number'

* lisp/ob-core.el (org-babel--string-to-number): Fix the regular expression.
* testing/lisp/test-ob.el (test-ob/string-to-number): Test cases.

If people write the data in the form "0001"", it means that he wants to
treat it as a string.

TINYCHANGE

> #+name: TBL
> |   id | name   | age |
> |------|--------|-----|
> | 0001 | Apollo |  16 |
> | 0002 | Bmw    |  16 |
>
> #+BEGIN_SRC emacs-lisp :results value pp :var tbl=TBL
> (mapc 'print tbl)
> #+END_SRC
>
> #+RESULTS:
> : (("0001" "Apollo" 16)
> :  ("0002" "Bmw" 16))

TINYCHANGE
Gong Qijian 6 năm trước cách đây
mục cha
commit
2b96fc9171
3 tập tin đã thay đổi với 13 bổ sung1 xóa
  1. 2 0
      etc/ORG-NEWS
  2. 1 1
      lisp/ob-core.el
  3. 10 0
      testing/lisp/test-ob.el

+ 2 - 0
etc/ORG-NEWS

@@ -62,6 +62,8 @@ the corresponding direction by swapping with the adjacent cell.
 
 It was unused throughout the code base.
 
+** Miscellaneous
+*** Org Table reads numbers starting with 0 as strings
 * Version 9.2
 ** Incompatible changes
 *** Removal of OrgStruct mode mode and radio lists

+ 1 - 1
lisp/ob-core.el

@@ -2915,7 +2915,7 @@ situations in which is it not appropriate."
 (defun org-babel--string-to-number (string)
   "If STRING represents a number return its value.
 Otherwise return nil."
-  (and (string-match-p "\\`-?[0-9]*\\.?[0-9]*\\'" string)
+  (and (string-match-p "\\`-?\\([0-9]\\|\\([1-9]\\|[0-9]*\\.\\)[0-9]*\\)\\'" string)
        (string-to-number string)))
 
 (defun org-babel-import-elisp-from-file (file-name &optional separator)

+ 10 - 0
testing/lisp/test-ob.el

@@ -2034,6 +2034,16 @@ abc
 	(let ((org-coderef-label-format "#(ref:%s)"))
 	  (org-babel-execute-src-block))))))
 
+(ert-deftest test-ob/string-to-number ()
+    (should (=  0   (org-babel--string-to-number "0")))
+    (should (=  1   (org-babel--string-to-number "1")))
+    (should (eq nil (org-babel--string-to-number "000")))
+    (should (eq nil (org-babel--string-to-number "001")))
+    (should (eq nil (org-babel--string-to-number "010")))
+    (should (=  100 (org-babel--string-to-number "100")))
+    (should (=  0.1 (org-babel--string-to-number "0.1")))
+    (should (=  1.0 (org-babel--string-to-number "1.0"))))
+
 (provide 'test-ob)
 
 ;;; test-ob ends here