Skip to content
Snippets Groups Projects
Commit d45a4388 authored by David Byers's avatar David Byers
Browse files

Merge branch '6-allow-olc-is-valid-and-friends-recognize-compound-codes' into 'master'

Resolve "Allow olc-is-valid and friends recognize compound codes"

Closes #6

See merge request !8
parents 941aca51 90c26599
No related branches found
Tags 1.3.0
1 merge request!8Resolve "Allow olc-is-valid and friends recognize compound codes"
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:
......
......@@ -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) ?+)))))
......
No preview for this file type
......@@ -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
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment