Skip to content

Commit

Permalink
Merge pull request #92 from nirvdrum/cache_symbols
Browse files Browse the repository at this point in the history
Cache symbols while tokenizing.
  • Loading branch information
tenderlove committed Oct 13, 2012
2 parents 4be2aef + dd925d3 commit 74ed8ae
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/psych/scalar_scanner.rb
Expand Up @@ -16,12 +16,14 @@ class ScalarScanner
# Create a new scanner
def initialize
@string_cache = {}
@symbol_cache = {}
end

# Tokenize +string+ returning the ruby object
def tokenize string
return nil if string.empty?
return string if @string_cache.key?(string)
return @symbol_cache[string] if @symbol_cache.key?(string)

case string
# Check for a String type, being careful not to get caught by hash keys, hex values, and
Expand Down Expand Up @@ -67,9 +69,9 @@ def tokenize string
0.0 / 0.0
when /^:./
if string =~ /^:(["'])(.*)\1/
$2.sub(/^:/, '').to_sym
@symbol_cache[string] = $2.sub(/^:/, '').to_sym
else
string.sub(/^:/, '').to_sym
@symbol_cache[string] = string.sub(/^:/, '').to_sym
end
when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+$/
i = 0
Expand Down

0 comments on commit 74ed8ae

Please sign in to comment.