From ee9c870bfbb4032ca08c1f1760716b5472b64293 Mon Sep 17 00:00:00 2001 From: murphy Date: Sat, 5 Nov 2005 03:09:21 +0000 Subject: helpers/word_list.rb: new CaseIgnoringWordList class simplified implementation Performance gain should be small. Adjusted scanner.rb and scanners/delphi.rb --- lib/coderay/helpers/word_list.rb | 62 +++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 35 deletions(-) (limited to 'lib/coderay/helpers') diff --git a/lib/coderay/helpers/word_list.rb b/lib/coderay/helpers/word_list.rb index 9f48b83..a74705d 100644 --- a/lib/coderay/helpers/word_list.rb +++ b/lib/coderay/helpers/word_list.rb @@ -27,7 +27,7 @@ module CodeRay # ] # # # make a WordList - # IDENT_KIND = Scanner::WordList.new(:ident). + # IDENT_KIND = WordList.new(:ident). # add(RESERVED_WORDS, :reserved). # add(PREDEFINED_TYPES, :pre_type). # add(PREDEFINED_CONSTANTS, :pre_constant) @@ -37,7 +37,7 @@ module CodeRay # def scan_tokens tokens, options # ... # - # elsif match = scan(/ [A-Za-z_][A-Za-z_0-9]* /x) + # elsif scan(/[A-Za-z_][A-Za-z_0-9]*/) # # use it # kind = IDENT_KIND[match] # ... @@ -45,56 +45,48 @@ module CodeRay class WordList < Hash # Creates a new WordList with +default+ as default value. - # case_mode controls how keys are compared; - # :case_match is faster. - def initialize default = false, case_mode = :case_match - @case_ignore = - case case_mode - when :case_match then false - when :case_ignore then true - else raise ArgumentError, - ":case_ignore or :case_match expected, but #{case_mode} given" - end - - if @case_ignore - super() do |h, k| - h[k] = h.fetch k.downcase, default - end - else - super default - end + def initialize default = false, &block + super default, &block end # Checks if a word is included. def include? word - self[word] if @case_ignore has_key? word end - # Add words to the list and associate them with - # +kind+. + # Add words to the list and associate them with +kind+. def add words, kind = true words.each do |word| - self[mind_case(word)] = kind + self[word] = kind end self end - alias words keys + end + + + class CaseIgnoringWordList < WordList + + # Creates a new WordList with +default+ as default value. + # + # Text case is ignored. + def initialize default = false + super() do |h, k| + h[k] = h.fetch k.downcase, default + end + end - # Returns whether key comparing is done case insensitive. - def case_ignore? - @case_mode + # Checks if a word is included. + def include? word + has_key? word.downcase end - private - # helper method for key - def mind_case word - if @case_ignore - word.downcase - else - word.dup + # Add words to the list and associate them with +kind+. + def add words, kind = true + words.each do |word| + self[word.downcase] = kind end + self end end -- cgit v1.2.1