From: William Morgan Date: Wed, 13 May 2009 20:07:14 +0000 (-0700) Subject: bugfix: index label parsing code incorrect X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=4ea04212928446fb62c5fd5ecca9c18b0339edf9;p=sup bugfix: index label parsing code incorrect Apparently split(/\s+/) is significantly different from split (which is equivalent to split(" ")) in at least one case: >> " a b c ".split(/\s+/) => ["", "a", "b", "c"] >> " a b c ".split => ["a", "b", "c"] This patch refactors the majority of uses of split(/\s+/) to a String#symbolize method, which is correct. --- diff --git a/bin/sup-sync b/bin/sup-sync index 91710d4..fe72953 100644 --- a/bin/sup-sync +++ b/bin/sup-sync @@ -100,7 +100,7 @@ restored_state = IO.foreach opts[:restore] do |l| l =~ /^(\S+) \((.*?)\)$/ or raise "Can't read dump line: #{l.inspect}" mid, labels = $1, $2 - dump[mid] = labels.split(" ").map { |x| x.intern } + dump[mid] = labels.symbolistize end $stderr.puts "Read #{dump.size} entries from dump file." dump @@ -143,7 +143,7 @@ begin next if target == :changed && entry && entry[:source_id].to_i == source.id && entry[:source_info].to_i == offset ## get the state currently in the index - index_state = entry[:label].split(/\s+/).map { |x| x.intern } if entry + index_state = entry[:label].symbolistize if entry ## skip if we're operating on restored messages, and this one ## ain't. @@ -153,7 +153,7 @@ begin ## to default source state modification flags. m.labels -= [:inbox] if opts[:archive] m.labels -= [:unread] if opts[:read] - m.labels += opts[:extra_labels].split(/\s*,\s*/).map { |x| x.intern } if opts[:extra_labels] + m.labels += opts[:extra_labels].strip.split(/\s*,\s*/).map { |x| x.intern } if opts[:extra_labels] ## assign message labels based on the operation we're performing case op diff --git a/bin/sup-sync-back b/bin/sup-sync-back index 4216cf9..4f1387e 100644 --- a/bin/sup-sync-back +++ b/bin/sup-sync-back @@ -109,7 +109,7 @@ EOS num_scanned += 1 if entry - labels = entry[:label].split.map { |x| x.intern }.to_boolean_h + labels = entry[:label].symbolistize.to_boolean_h if labels.member? :deleted if opts[:drop_deleted] diff --git a/lib/sup/buffer.rb b/lib/sup/buffer.rb index c9e34b3..2db6aa8 100644 --- a/lib/sup/buffer.rb +++ b/lib/sup/buffer.rb @@ -484,7 +484,7 @@ EOS return unless answer - user_labels = answer.split(/\s+/).map { |l| l.intern } + user_labels = answer.symbolistize user_labels.each do |l| if forbidden_labels.include?(l) || LabelManager::RESERVED_LABELS.include?(l) BufferManager.flash "'#{l}' is a reserved label!" diff --git a/lib/sup/index.rb b/lib/sup/index.rb index 838d601..2bdf22b 100644 --- a/lib/sup/index.rb +++ b/lib/sup/index.rb @@ -226,7 +226,7 @@ EOS ## but merge in the labels. if entry[:source_id] && entry[:source_info] && entry[:label] && ((entry[:source_id].to_i > source_id) || (entry[:source_info].to_i < m.source_info)) - labels = (entry[:label].split(/\s+/).map { |l| l.intern } + m.labels).uniq + labels = (entry[:label].symbolistize + m.labels).uniq #Redwood::log "found updated version of message #{m.id}: #{m.subj}" #Redwood::log "previous version was at #{entry[:source_id].inspect}:#{entry[:source_info].inspect}, this version at #{source_id.inspect}:#{m.source_info.inspect}" #Redwood::log "merged labels are #{labels.inspect} (index #{entry[:label].inspect}, message #{m.labels.inspect})" @@ -415,7 +415,7 @@ EOS } Message.new :source => source, :source_info => doc[:source_info].to_i, - :labels => doc[:label].split(" ").map { |s| s.intern }, + :labels => doc[:label].symbolistize, :snippet => doc[:snippet], :header => fake_header end end diff --git a/lib/sup/poll.rb b/lib/sup/poll.rb index 5027218..8c10ef1 100644 --- a/lib/sup/poll.rb +++ b/lib/sup/poll.rb @@ -97,7 +97,7 @@ EOS numi = 0 add_messages_from source do |m, offset, entry| ## always preserve the labels on disk. - m.labels = ((m.labels - [:unread, :inbox]) + entry[:label].split(/\s+/).map { |x| x.intern }).uniq if entry + m.labels = ((m.labels - [:unread, :inbox]) + entry[:label].symbolistize).uniq if entry yield "Found message at #{offset} with labels {#{m.labels * ', '}}" unless entry num += 1 diff --git a/lib/sup/util.rb b/lib/sup/util.rb index 8b92fd2..c54a2c0 100644 --- a/lib/sup/util.rb +++ b/lib/sup/util.rb @@ -271,6 +271,11 @@ class String def normalize_whitespace gsub(/\t/, " ").gsub(/\r/, "") end + + ## takes a space-separated list of words, and returns an array of symbols. + ## typically used in Sup for translating Ferret's representation of a list + ## of labels (a string) to an array of label symbols. + def symbolistize; split.map { |x| x.intern } end end class Numeric