diff options
author | Gerhard Weis <g.weis@uq.edu.au> | 2021-12-13 09:15:15 +1000 |
---|---|---|
committer | Gerhard Weis <g.weis@uq.edu.au> | 2021-12-13 09:15:15 +1000 |
commit | 924975890471f9baf3f0a86b277770fff299e106 (patch) | |
tree | 8854e9afbd4614d3b97d9d87aa71ad2b5212250e | |
parent | 6bebac0913c431a0406c7851e2d3356822b59baa (diff) | |
parent | 4f422ae415e8c9591677b97faf0776ece6d7afb8 (diff) | |
download | isodate-924975890471f9baf3f0a86b277770fff299e106.tar.gz |
Merge branch 'PR16'
* PR16:
Don't match garbage characters at the end of time strings
Don't match garbage characters at the end of date strings
Factor in some regexes
# Conflicts:
# src/isodate/isodates.py
# src/isodate/isotime.py
-rw-r--r-- | CHANGES.txt | 1 | ||||
-rw-r--r-- | src/isodate/isodates.py | 98 | ||||
-rw-r--r-- | src/isodate/isotime.py | 47 |
3 files changed, 57 insertions, 89 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 0271298..1bac7d5 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -6,6 +6,7 @@ CHANGES ------------------ - drop end of life python versions +- Don't match garbage characters at the end of parsed strings #16 (Gabriel de Perthuis) 0.6.1 (2021-12-13) diff --git a/src/isodate/isodates.py b/src/isodate/isodates.py index ef8c7f9..3c5c095 100644 --- a/src/isodate/isodates.py +++ b/src/isodate/isodates.py @@ -41,93 +41,75 @@ def build_date_regexps(yeardigits=4, expanded=False): sign = 1 else: sign = 0 + + def add_re(regex_text): + cache_entry.append(re.compile(r"\A" + regex_text + r"\Z")) + # 1. complete dates: # YYYY-MM-DD or +- YYYYYY-MM-DD... extended date format - cache_entry.append( - re.compile( - r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" - r"-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})" % (sign, yeardigits) - ) + add_re( + r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" + r"-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})" % (sign, yeardigits) ) # YYYYMMDD or +- YYYYYYMMDD... basic date format - cache_entry.append( - re.compile( - r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" - r"(?P<month>[0-9]{2})(?P<day>[0-9]{2})" % (sign, yeardigits) - ) + add_re( + r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" + r"(?P<month>[0-9]{2})(?P<day>[0-9]{2})" % (sign, yeardigits) ) # 2. complete week dates: # YYYY-Www-D or +-YYYYYY-Www-D ... extended week date - cache_entry.append( - re.compile( - r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" - r"-W(?P<week>[0-9]{2})-(?P<day>[0-9]{1})" % (sign, yeardigits) - ) + add_re( + r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" + r"-W(?P<week>[0-9]{2})-(?P<day>[0-9]{1})" % (sign, yeardigits) ) # YYYYWwwD or +-YYYYYYWwwD ... basic week date - cache_entry.append( - re.compile( - r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})W" - r"(?P<week>[0-9]{2})(?P<day>[0-9]{1})" % (sign, yeardigits) - ) + add_re( + r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})W" + r"(?P<week>[0-9]{2})(?P<day>[0-9]{1})" % (sign, yeardigits) ) # 3. ordinal dates: # YYYY-DDD or +-YYYYYY-DDD ... extended format - cache_entry.append( - re.compile( - r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" - r"-(?P<day>[0-9]{3})" % (sign, yeardigits) - ) + add_re( + r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" + r"-(?P<day>[0-9]{3})" % (sign, yeardigits) ) # YYYYDDD or +-YYYYYYDDD ... basic format - cache_entry.append( - re.compile( - r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" - r"(?P<day>[0-9]{3})" % (sign, yeardigits) - ) + add_re( + r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" + r"(?P<day>[0-9]{3})" % (sign, yeardigits) ) # 4. week dates: # YYYY-Www or +-YYYYYY-Www ... extended reduced accuracy week date - cache_entry.append( - re.compile( - r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" - r"-W(?P<week>[0-9]{2})" % (sign, yeardigits) - ) + # 4. week dates: + # YYYY-Www or +-YYYYYY-Www ... extended reduced accuracy week date + add_re( + r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" + r"-W(?P<week>[0-9]{2})" % (sign, yeardigits) ) # YYYYWww or +-YYYYYYWww ... basic reduced accuracy week date - cache_entry.append( - re.compile( - r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})W" - r"(?P<week>[0-9]{2})" % (sign, yeardigits) - ) + add_re( + r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})W" + r"(?P<week>[0-9]{2})" % (sign, yeardigits) ) # 5. month dates: # YYY-MM or +-YYYYYY-MM ... reduced accuracy specific month - cache_entry.append( - re.compile( - r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" - r"-(?P<month>[0-9]{2})" % (sign, yeardigits) - ) + # 5. month dates: + # YYY-MM or +-YYYYYY-MM ... reduced accuracy specific month + add_re( + r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" + r"-(?P<month>[0-9]{2})" % (sign, yeardigits) ) # YYYMM or +-YYYYYYMM ... basic incomplete month date format - cache_entry.append( - re.compile( - r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" - r"(?P<month>[0-9]{2})" % (sign, yeardigits) - ) + add_re( + r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" + r"(?P<month>[0-9]{2})" % (sign, yeardigits) ) # 6. year dates: # YYYY or +-YYYYYY ... reduced accuracy specific year - cache_entry.append( - re.compile(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" % (sign, yeardigits)) - ) + add_re(r"(?P<sign>[+-]){%d}(?P<year>[0-9]{%d})" % (sign, yeardigits)) # 7. century dates: # YY or +-YYYY ... reduced accuracy specific century - cache_entry.append( - re.compile( - r"(?P<sign>[+-]){%d}" r"(?P<century>[0-9]{%d})" % (sign, yeardigits - 2) - ) - ) + add_re(r"(?P<sign>[+-]){%d}" r"(?P<century>[0-9]{%d})" % (sign, yeardigits - 2)) DATE_REGEX_CACHE[(yeardigits, expanded)] = cache_entry return DATE_REGEX_CACHE[(yeardigits, expanded)] diff --git a/src/isodate/isotime.py b/src/isodate/isotime.py index 182770b..3e2bb46 100644 --- a/src/isodate/isotime.py +++ b/src/isodate/isotime.py @@ -41,46 +41,31 @@ def build_time_regexps(): # +-hhmm # +-hh => # isotzinfo.TZ_REGEX + def add_re(regex_text): + TIME_REGEX_CACHE.append(re.compile(r"\A" + regex_text + TZ_REGEX + r"\Z")) + # 1. complete time: # hh:mm:ss.ss ... extended format - TIME_REGEX_CACHE.append( - re.compile( - r"T?(?P<hour>[0-9]{2}):" - r"(?P<minute>[0-9]{2}):" - r"(?P<second>[0-9]{2}" - r"([,.][0-9]+)?)" + TZ_REGEX - ) + add_re( + r"T?(?P<hour>[0-9]{2}):" + r"(?P<minute>[0-9]{2}):" + r"(?P<second>[0-9]{2}" + r"([,.][0-9]+)?)" ) # hhmmss.ss ... basic format - TIME_REGEX_CACHE.append( - re.compile( - r"T?(?P<hour>[0-9]{2})" - r"(?P<minute>[0-9]{2})" - r"(?P<second>[0-9]{2}" - r"([,.][0-9]+)?)" + TZ_REGEX - ) + add_re( + r"T?(?P<hour>[0-9]{2})" + r"(?P<minute>[0-9]{2})" + r"(?P<second>[0-9]{2}" + r"([,.][0-9]+)?)" ) # 2. reduced accuracy: # hh:mm.mm ... extended format - TIME_REGEX_CACHE.append( - re.compile( - r"T?(?P<hour>[0-9]{2}):" - r"(?P<minute>[0-9]{2}" - r"([,.][0-9]+)?)" + TZ_REGEX - ) - ) + add_re(r"T?(?P<hour>[0-9]{2}):" r"(?P<minute>[0-9]{2}" r"([,.][0-9]+)?)") # hhmm.mm ... basic format - TIME_REGEX_CACHE.append( - re.compile( - r"T?(?P<hour>[0-9]{2})" - r"(?P<minute>[0-9]{2}" - r"([,.][0-9]+)?)" + TZ_REGEX - ) - ) + add_re(r"T?(?P<hour>[0-9]{2})" r"(?P<minute>[0-9]{2}" r"([,.][0-9]+)?)") # hh.hh ... basic format - TIME_REGEX_CACHE.append( - re.compile(r"T?(?P<hour>[0-9]{2}" r"([,.][0-9]+)?)" + TZ_REGEX) - ) + add_re(r"T?(?P<hour>[0-9]{2}" r"([,.][0-9]+)?)") return TIME_REGEX_CACHE |