Enhance `#scan_integer` to check for valid character following it
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 45/100
Piste de recherche
Commencez par le point d’entrée #scan_integer et l’exemple parse_number de l’issue, puis examinez le comportement existant décrit pour des entrées telles que 123d et 123.32. Clarifiez l’API du séparateur et ajoutez une couverture pour les caractères suivants acceptés et rejetés, tout en préservant les analyses actuelles.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Recently the new method #scan_integer was introduced (see #113) to optimize scanning integer values.
The current implementation works regardless of what follows the integer, i.e. scanning 123, 123 something, 123,something, 123.32 and 123something all work and would return 123.
However, in - I suspect - many cases an integer may only be a valid integer if it is (not) followed by certain characters. One example is the input 123d which leads to an error when interpreted as Ruby code.
My use case is PDF syntax. There a token is an integer only when it is followed by a whitespace (ASCII decimal 0, 9, 10, 12, 13 and 32) or a delimiter (( ) < > [ ] / %) character (otherwise it is a generic token). To handle this the implementation using #scan_integer looks like this:
# Parses the number (integer or real) at the current position.
#
# See: PDF2.0 s7.3.3
def parse_number
prepare_string_scanner(20)
pos = self.pos
if (tmp = @ss.scan_integer)
if @ss.eos? || @ss.match?(WHITESPACE_OR_DELIMITER_RE)
# Handle object references, see PDF2.0 s7.3.10
prepare_string_scanner(10)
if @ss.scan(REFERENCE_RE)
tmp = if tmp > 0
Reference.new(tmp, @ss[1].to_i)
else
maybe_raise("Invalid indirect object reference (#{tmp},#{@ss[1].to_i})")
nil
end
end
return tmp
else
self.pos = pos
end
end
val = scan_until(WHITESPACE_OR_DELIMITER_RE) || @ss.scan(/.*/)
if val.match?(/\A[+-]?(?:\d+\.\d*|\.\d+)\z/)
val << '0' if val.getbyte(-1) == 46 # dot '.'
Float(val)
else
TOKEN_CACHE[val] # val is keyword
end
end
end
As you can see we
- need to store the current scan position,
- check if scanning an integer works at the current position,
- scan the content after the integer to verify that it is indeed an integer and work with it, or
- if the previous step didn't work, reset the scan position.
This could be simplified to just a call of #scan_integer if this method would optionally check the contents after it. Something like #scan_integer(separator: SEPARATOR_PATTERN) or maybe #scan_integer(separator_chars: STRING) (where STRING contains separator characters, similar to whole String#tr works).
Would it make sense to include such functionality?
- Langage dominant
- C
- Étoiles
- 88
- Forks
- 51
- Merge moyen
- 5 j 2 h
- PR mergées (30 j)
- 2
Préparer son environnement
Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de ruby/strscan
-
Difficulté 3/5 1-2 jours Accessibilité débutants 54/100
-
Difficulté 3/5 1-2 jours Accessibilité débutants 35/100
-
Caret lost its meaningOuverte
Difficulté 3/5 1-2 jours Accessibilité débutants 35/100
-
Difficulté 4/5 3-5 jours Accessibilité débutants 25/100
Toutes les issues de ruby/strscan
Issues similaires
-
runtime: lf_f128_mod is documented as fmodq (truncating) but implements floor-based modulo; unusedOuverteinternal correctness low priority rt_lib
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
Les mainteneurs répondent en général sous 1 jour
-
Unreachable code warningOuvertebug
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
SanderMertens/flecs#2395 ·
Les mainteneurs répondent en général sous 1 jour
-
Memory leak: "Add to Playlist" (`CMD_EVENT_ADD_TO_PLAYLIST`) never frees the playlist it loadsOuverte
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
Les mainteneurs répondent en général sous 1 jour
-
Signed/unsigned mix in the EOCD backward scan rejects valid archives with a 65535-byte commentOuverte
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
OpenXiangShan/NEMU#1287 ·
Les mainteneurs répondent en général sous 1 jour