@@ -62,10 +62,15 @@ class Card(_Verify):
6262 # This regex helps delete leading zeros from numbers, otherwise
6363 # Python might evaluate them as octal values (this is not-greedy, however,
6464 # so it may not strip leading zeros from a float, which is fine)
65- _number_FSC_RE = re .compile (r'(?P<sign>[+-])?0*?(?P<digt>{})' .format (
66- _digits_FSC ))
67- _number_NFSC_RE = re .compile (r'(?P<sign>[+-])? *0*?(?P<digt>{})' .format (
68- _digits_NFSC ))
65+ _number_FSC_RE = re .compile (rf'(?P<sign>[+-])?0*?(?P<digt>{ _digits_FSC } )' )
66+ _number_NFSC_RE = \
67+ re .compile (rf'(?P<sign>[+-])? *0*?(?P<digt>{ _digits_NFSC } )' )
68+
69+ # Used in cards using the CONTINUE convention which expect a string
70+ # followed by an optional comment
71+ _strg = r'\'(?P<strg>([ -~]+?|\'\'|) *?)\'(?=$|/| )'
72+ _comm_field = r'(?P<comm_field>(?P<sepr>/ *)(?P<comm>(.|\n)*))'
73+ _strg_comment_RE = re .compile (f'({ _strg } )? *{ _comm_field } ?' )
6974
7075 # FSC commentary card string which must contain printable ASCII characters.
7176 # Note: \Z matches the end of the string without allowing newlines
@@ -96,7 +101,7 @@ class Card(_Verify):
96101 # since a greedy match will find a single-quote after
97102 # the comment separator resulting in an incorrect
98103 # match.
99- r'\'(?P<strg>([ -~]+?|\'\'|)) *?\'(?=$|/| ) |'
104+ rf' { _strg } |'
100105 r'(?P<bool>[FT])|'
101106 r'(?P<numr>' + _numr_FSC + r')|'
102107 r'(?P<cplx>\( *'
@@ -111,17 +116,13 @@ class Card(_Verify):
111116 _value_NFSC_RE = re .compile (
112117 r'(?P<valu_field> *'
113118 r'(?P<valu>'
114- r'\'(?P<strg>([ -~]+?|\'\'|) *?)\'(?=$|/| ) |'
119+ rf' { _strg } |'
115120 r'(?P<bool>[FT])|'
116121 r'(?P<numr>' + _numr_NFSC + r')|'
117122 r'(?P<cplx>\( *'
118123 r'(?P<real>' + _numr_NFSC + r') *, *'
119124 r'(?P<imag>' + _numr_NFSC + r') *\))'
120- r')? *)'
121- r'(?P<comm_field>'
122- r'(?P<sepr>/ *)'
123- r'(?P<comm>(.|\n)*)'
124- r')?$' )
125+ fr')? *){ _comm_field } ?$' )
125126
126127 _rvkc_identifier = r'[a-zA-Z_]\w*'
127128 _rvkc_field = _rvkc_identifier + r'(\.\d+)?'
@@ -746,19 +747,6 @@ def _parse_value(self):
746747 if self ._check_if_rvkc (self ._image ):
747748 return self ._value
748749
749- if len (self ._image ) > self .length :
750- values = []
751- for card in self ._itersubcards ():
752- value = card .value .rstrip ().replace ("''" , "'" )
753- if value and value [- 1 ] == '&' :
754- value = value [:- 1 ]
755- values .append (value )
756-
757- value = '' .join (values )
758-
759- self ._valuestring = value
760- return value
761-
762750 m = self ._value_NFSC_RE .match (self ._split ()[1 ])
763751
764752 if m is None :
@@ -810,21 +798,22 @@ def _parse_comment(self):
810798 if self .keyword in Card ._commentary_keywords or self ._invalid :
811799 return ''
812800
813- if len (self ._image ) > self .length :
814- comments = []
815- for card in self ._itersubcards ():
816- if card .comment :
817- comments .append (card .comment )
818- comment = '/ ' + ' ' .join (comments ).rstrip ()
819- m = self ._value_NFSC_RE .match (comment )
820- else :
821- m = self ._value_NFSC_RE .match (self ._split ()[1 ])
822-
801+ valuecomment = self ._split ()[1 ]
802+ m = self ._value_NFSC_RE .match (valuecomment )
803+ comment = ''
823804 if m is not None :
824- comment = m .group ('comm' )
825- if comment :
826- return comment .rstrip ()
827- return ''
805+ # Don't combine this if statement with the one above, because
806+ # we only want the elif case to run if this was not a valid
807+ # card at all
808+ if m .group ('comm' ):
809+ comment = m .group ('comm' ).rstrip ()
810+ elif '/' in valuecomment :
811+ # The value in this FITS file was not in a valid/known format. In
812+ # this case the best we can do is guess that everything after the
813+ # first / was meant to be the comment
814+ comment = valuecomment .split ('/' , 1 )[1 ].strip ()
815+
816+ return comment
828817
829818 def _split (self ):
830819 """
@@ -838,6 +827,34 @@ def _split(self):
838827 else :
839828 image = self .image
840829
830+ # Split cards with CONTINUE cards
831+ if len (self ._image ) > self .length :
832+ values = []
833+ comments = []
834+ keyword = None
835+ for card in self ._itersubcards ():
836+ kw , vc = card ._split ()
837+ if keyword is None :
838+ keyword = kw
839+
840+ # Should match a string followed by a comment; if not it
841+ # might be an invalid Card, so we just take it verbatim
842+ m = self ._strg_comment_RE .match (vc )
843+ if not m :
844+ return kw , vc
845+
846+ value = m .group ('strg' ) or ''
847+ value = value .rstrip ().replace ("''" , "'" )
848+ if value and value [- 1 ] == '&' :
849+ value = value [:- 1 ]
850+ values .append (value )
851+ comment = m .group ('comm' )
852+ if comment :
853+ comments .append (comment .rstrip ())
854+
855+ valuecomment = f"'{ '' .join (values )} ' / { ' ' .join (comments )} "
856+ return keyword , valuecomment
857+
841858 if self .keyword in self ._special_keywords :
842859 keyword , valuecomment = image .split (' ' , 1 )
843860 else :
0 commit comments