Skip to content

Commit

Permalink
Merge pull request #41 from Arkham/fix_adapter
Browse files Browse the repository at this point in the history
revert to previous regexp; added regression test
  • Loading branch information
Ju Liu committed Apr 4, 2013
2 parents b141400 + c09a21b commit 6b1f47d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
17 changes: 13 additions & 4 deletions lib/wordmove/sql_mover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,19 @@ def replace_field!(source_field, dest_field)
end

def serialized_replace!(source_field, dest_field)
sql_content.gsub!(/s:(?:\d+):(\\*['"])(.*?)\1;/) do |match|
delimiter, string = $1, $2
string.gsub!(/#{Regexp.escape(source_field)}/, dest_field)
%(s:#{string.length}:#{delimiter}#{string}#{delimiter};)
length_delta = source_field.length - dest_field.length

sql_content.gsub!(/s:(\d+):([\\]*['"])(.*?)\2;/) do |match|
length = $1.to_i
delimiter = $2
string = $3

string.gsub!(/#{Regexp.escape(source_field)}/) do |match|
length -= length_delta
dest_field
end

%(s:#{length}:#{delimiter}#{string}#{delimiter};)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/wordmove/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Wordmove
VERSION = "1.0.10"
VERSION = "1.0.11"
end
9 changes: 9 additions & 0 deletions spec/sql_mover_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@
end
end

context "given strings with escaped content" do
let(:content) { 's:6:"dump\"\"";' }

it "should calculate the correct final length" do
sql_mover.serialized_replace!('dump', 'sausage')
sql_mover.sql_content.should == 's:9:"sausage\"\"";'
end
end

context "given multiple types of string quoting" do
let(:content) { "a:3:{s:20:\\\"http://dump.com/spam\\\";s:6:'foobar';s:22:'http://dump.com/foobar';s:8:'sausages';}" }

Expand Down

0 comments on commit 6b1f47d

Please sign in to comment.