emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: qijian gong <gongqijian@gmail.com>
To: Emacs-orgmode@gnu.org
Subject: [PATCH] ob-core.el: Fix org-babel--string-to-number
Date: Thu, 11 Oct 2018 19:27:55 +0800	[thread overview]
Message-ID: <CAPWNH1P=7TVu48WSp2b9tTiskGkfv02OHyyKgg4NA27EEBOqAA@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 538 bytes --]

Hello,

I've write a patch (see attachment) to fix the
`org-babel--string-to-number` function.

In my opinion, if people write the data in the form `0001`, it means that
he wants to treat it as a string:

    > #+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))

Qijian

[-- Attachment #1.2: Type: text/html, Size: 966 bytes --]

[-- Attachment #2: 0001-ob-core.el-Fix-org-babel-string-to-number.patch --]
[-- Type: application/octet-stream, Size: 2267 bytes --]

From 08949d98f7d16b2e8eb60476b9bd96b123c38366 Mon Sep 17 00:00:00 2001
From: Gong Qijian <gongqijian@gmail.com>
Date: Wed, 10 Oct 2018 20:28:10 +0800
Subject: [PATCH] ob-core.el: 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.

> #+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
---
 lisp/ob-core.el         |  2 +-
 testing/lisp/test-ob.el | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 19d58cd..349a761 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -2923,7 +2923,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)
diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el
index bc065d3..50af7e9 100644
--- a/testing/lisp/test-ob.el
+++ b/testing/lisp/test-ob.el
@@ -1985,6 +1985,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
-- 
2.19.0


             reply	other threads:[~2018-10-11 11:28 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-11 11:27 qijian gong [this message]
2018-10-13 21:23 ` [PATCH] ob-core.el: Fix org-babel--string-to-number Nicolas Goaziou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAPWNH1P=7TVu48WSp2b9tTiskGkfv02OHyyKgg4NA27EEBOqAA@mail.gmail.com' \
    --to=gongqijian@gmail.com \
    --cc=Emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).