diff --git a/CHANGELOG b/CHANGELOG index a3edf69308b1ade4ddbf68164f26210e9ca422ed..c3969f969ac2cf7956047bdc18fa6b0416f7e073 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,14 @@ +2020-07-25 David Byers <david.byers@liu.se> + + Fix issue #6: + * olc.texi (Functions): Document "compound". Correct error in + documentation of return value for olc-is-full and olc-is-short. + + * olc.el (olc-parse-code): Document that match data is changed. + (olc-is-valid): Added the "compound" keyword arg. + (olc-is-full): Added the "compound" keyword arg. + (olc-is-short): Added the "compound" keyword arg. + 2020-07-24 David Byers <david.byers@liu.se> Fix issue #4: diff --git a/olc.el b/olc.el index ea6b36691149936d8295a430bb92ef287f4b8b5f..f4a0fdfbc65ef0eee5f90f803d6a64ed866b63eb 100644 --- a/olc.el +++ b/olc.el @@ -3,7 +3,7 @@ ;; Copyright (C) 2020 David Byers ;; ;; Author: David Byers <david.byers@liu.se> -;; Version: 1.2.0 +;; Version: 1.3.0 ;; Package-Requires: ((emacs "25.1")) ;; Keywords: extensions, lisp ;; URL: https://gitlab.liu.se/davby02/olc @@ -247,7 +247,9 @@ raise, and args for the raised error. (/ (expt 20 -3) (expt 5 (- len 10))))) (cl-defun olc-parse-code (code) - "Parse an open location code CODE." + "Parse an open location code CODE. + +This function changes the match data." (if (olc-parse-p code) code (cl-check-type code stringp) @@ -362,57 +364,71 @@ raise, and args for the raised error. "Regular expression for parsing codes.") -(defun olc-is-valid (code) - "Return non-nil if CODE is a valid open location code." - (or (olc-parse-p code) - (save-match-data - (let ((case-fold-search t)) - - ;; The code is decomposed into PAIRS PADDING "+" SUFFIX. - ;; - ;; Rules: - ;; - ;; - For all codes: - ;; - Pairs has an even (zero counts) length of at most 8. - ;; - Suffix is either zero or between 2 and 8 characters. - ;; - One or both of pairs and suffix must not be empty. - ;; - ;; - If there is padding: - ;; - The suffix must be empty - ;; - The length of pairs and padding combined must be 8 - - (when (string-match olc-code-regexp code) - (let ((pair-len (- (match-end 1) (match-beginning 1))) - (padd-len (- (match-end 2) (match-beginning 2))) - (suff-len (- (match-end 3) (match-beginning 3)))) - (and (and (= 0 (% pair-len 2)) (<= pair-len 8)) ; Check pairs - (and (<= suff-len 8) (/= suff-len 1)) ; Check suffix - (> (+ pair-len suff-len) 0) ; Check for not empty - (or (= padd-len 0) ; Empty padding... - (and (= suff-len 0) ; ...or suffix - (= (+ padd-len pair-len) 8)))))))))) - -(defun olc-is-short (code) +(cl-defun olc-is-valid (code &key compound) + "Return non-nil if CODE is a valid open location code. + +If compound is non-nil, then return non-nil if CODE looks like a +compound open location code (i.e. everything up to the first +space character is a valid code)." + (or (olc-parse-p code) + (save-match-data + (when (and compound (string-match " " code)) + (setq code (substring code 0 (match-beginning 0)))) + (let ((case-fold-search t)) + + ;; The code is decomposed into PAIRS PADDING "+" SUFFIX. + ;; + ;; Rules: + ;; + ;; - For all codes: + ;; - Pairs has an even (zero counts) length of at most 8. + ;; - Suffix is either zero or between 2 and 8 characters. + ;; - One or both of pairs and suffix must not be empty. + ;; + ;; - If there is padding: + ;; - The suffix must be empty + ;; - The length of pairs and padding combined must be 8 + + (when (string-match olc-code-regexp code) + (let ((pair-len (- (match-end 1) (match-beginning 1))) + (padd-len (- (match-end 2) (match-beginning 2))) + (suff-len (- (match-end 3) (match-beginning 3)))) + (and (and (= 0 (% pair-len 2)) (<= pair-len 8)) ; Check pairs + (and (<= suff-len 8) (/= suff-len 1)) ; Check suffix + (> (+ pair-len suff-len) 0) ; Check for not empty + (or (= padd-len 0) ; Empty padding... + (and (= suff-len 0) ; ...or suffix + (= (+ padd-len pair-len) 8)))))))))) + +(cl-defun olc-is-short (code &key compound) "Return non-nil if CODE is a valid short open location code. +If compound is non-nil, then return non-nil if CODE looks like a +compound open location code (i.e. everything up to the first +space character is a valid short code). + Note that nil means the code is either not short, or it is invalid." (if (olc-parse-p code) (olc-parse-short code) - (and (olc-is-valid code) + (and (olc-is-valid code :compound compound) (or (< (length code) 9) (and (>= (length code) 9) (not (= (elt code 8) ?+))))))) -(defun olc-is-full (code) +(cl-defun olc-is-full (code &key compound) "Return non-nil if CODE is a valid long open location code. +If compound is non-nil, then return non-nil if CODE looks like a +compound open location code (i.e. everything up to the first +space character is a valid short code). + Note that nil means the code is either not long, or it is invalid." (if (olc-parse-p code) (not (olc-parse-short code)) - (and (olc-is-valid code) + (and (olc-is-valid code :compound compound) (and (>= (length code) 9) (= (elt code 8) ?+))))) diff --git a/olc.info b/olc.info index fcfb028bcb60a005970e3af96a0f0b38827f0922..c7ef9ba2401e37a2e5f56c953312610f76bf2cb4 100644 --- a/olc.info +++ b/olc.info @@ -312,16 +312,25 @@ Functions coordinates. Please make sure you follow the acceptable use policy for the API (e.g., one request per second, tops, allowed). - -- Function: olc-is-valid code - Returns non-‘nil’ if CODE is a valid open location code. - - -- Function: olc-is-short code - Returns non-‘nil’ if CODE is a valid short location code. Returns - ‘nil’ for valid short and for invalid codes. - - -- Function: olc-is-full code - Returns non-‘nil’ if CODE is a valid full open location code. - Returns ‘nil’ for valid long and for invalid codes. + -- Function: olc-is-valid code &key compound + Returns non-‘nil’ if CODE is a valid open location code. If + COMPOUND is non-nil, then check only up to the first space + character so non-nil is returned for strings that look like + compound codes. + + -- Function: olc-is-short code &key compound + Returns non-‘nil’ if CODE is a valid short location code. If + COMPOUND is non-nil, then check only up to the first space + character so non-nil is returned for strings that look like + compound codes. Returns ‘nil’ for valid full and for invalid + codes. + + -- Function: olc-is-full code &key compound + Returns non-‘nil’ if CODE is a valid full open location code. If + COMPOUND is non-nil, then check only up to the first space + character so non-nil is returned for strings that look like + compound codes. Returns ‘nil’ for valid short and for invalid + codes. File: olc.info, Node: Index, Prev: Functions, Up: Top @@ -342,8 +351,8 @@ Index * olc-area-p: olc-area. (line 13) * olc-decode: Functions. (line 21) * olc-encode: Functions. (line 6) -* olc-is-full: Functions. (line 118) -* olc-is-short: Functions. (line 114) +* olc-is-full: Functions. (line 124) +* olc-is-short: Functions. (line 117) * olc-is-valid: Functions. (line 111) * olc-parse-create: olc-parse. (line 9) * olc-parse-grid: olc-parse. (line 18) @@ -364,7 +373,7 @@ Node: olc-area1277 Node: olc-parse2205 Node: Errors3435 Node: Functions7375 -Node: Index12739 +Node: Index13244 End Tag Table diff --git a/olc.texi b/olc.texi index cf5fa147c35dbf8da69aec3c6c5368eed02bb125..c4864079338126af1adbe7b27f729616d34bd1e4 100644 --- a/olc.texi +++ b/olc.texi @@ -354,18 +354,26 @@ coordinates. Please make sure you follow the acceptable use policy for the API (e.g., one request per second, tops, allowed). @end defun -@defun olc-is-valid code -Returns non-@code{nil} if @var{code} is a valid open location code. +@defun olc-is-valid code &key compound +Returns non-@code{nil} if @var{code} is a valid open location code. If +@var{compound} is non-nil, then check only up to the first space +character so non-nil is returned for strings that look like compound +codes. @end defun -@defun olc-is-short code +@defun olc-is-short code &key compound Returns non-@code{nil} if @var{code} is a valid short location code. -Returns @code{nil} for valid short and for invalid codes. +If @var{compound} is non-nil, then check only up to the first space +character so non-nil is returned for strings that look like compound +codes. Returns @code{nil} for valid full and for invalid codes. @end defun -@defun olc-is-full code +@defun olc-is-full code &key compound Returns non-@code{nil} if @var{code} is a valid full open location -code. Returns @code{nil} for valid long and for invalid codes. +code. If @var{compound} is non-nil, then check only up to the first +space character so non-nil is returned for strings that look like +compound codes. Returns @code{nil} for valid short and for invalid +codes. @end defun diff --git a/test/olctest.el b/test/olctest.el index 34add359f8b8aa8e984f2821e72ad64affef4bd2..6bcf479162b632f81283a197adefc9ab7524eafd 100644 --- a/test/olctest.el +++ b/test/olctest.el @@ -511,6 +511,32 @@ :act (olc-nominatim-endpoint "reverse") :msg "3")))) +(defun olctest-issue-6 () + (olctest-testcase "issue-6" + (olctest-equal :exp t :act (olc-is-valid "2222+")) + (olctest-equal :exp t :act (olc-is-valid "2222+" :compound t)) + (olctest-equal :exp nil :act (olc-is-valid "2222+ Sweden")) + (olctest-equal :exp t :act (olc-is-valid "2222+ Sweden" :compound t)) + + (olctest-equal :exp t :act (olc-is-short "2222+")) + (olctest-equal :exp t :act (olc-is-short "2222+" :compound t)) + (olctest-equal :exp nil :act (olc-is-short "2222+ Sweden")) + (olctest-equal :exp t :act (olc-is-short "2222+ Sweden" :compound t)) + (olctest-equal :exp nil :act (olc-is-short "22222222+")) + (olctest-equal :exp nil :act (olc-is-short "22222222+" :compound t)) + (olctest-equal :exp nil :act (olc-is-short "22222222+ Sweden")) + (olctest-equal :exp nil :act (olc-is-short "22222222+ Sweden" :compound t)) + + (olctest-equal :exp nil :act (olc-is-full "2222+")) + (olctest-equal :exp nil :act (olc-is-full "2222+" :compound t)) + (olctest-equal :exp nil :act (olc-is-full "2222+ Sweden")) + (olctest-equal :exp nil :act (olc-is-full "2222+ Sweden" :compound t)) + (olctest-equal :exp t :act (olc-is-full "22222222+")) + (olctest-equal :exp t :act (olc-is-full "22222222+" :compound t)) + (olctest-equal :exp nil :act (olc-is-full "22222222+ Sweden")) + (olctest-equal :exp t :act (olc-is-full "22222222+ Sweden" :compound t)) + )) + (defvar olctest-selected-tests) @@ -530,6 +556,7 @@ (run-test validity) (run-test localtests) (run-test errors) + (run-test issue-6) (run-test issue-5) (run-test issue-3) (run-test issue-2)