From b540ed8d6bb2298d57079c4240540b4b52db8aae Mon Sep 17 00:00:00 2001 From: Scott Weber Date: Tue, 6 Mar 2012 09:48:25 -0500 Subject: [PATCH] make sure that substitutions are numbered when necessary When there is more than one substitution in a string, Android requires that the substitutions are explicitly numbered (e.g. %1$s, %2$s, etc.). This change will number the substitutions if they are not already numbered. It just numbers them from left to right in the string. --- lib/twine/formatters/android.rb | 50 +++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/lib/twine/formatters/android.rb b/lib/twine/formatters/android.rb index e13a141..5a4c476 100644 --- a/lib/twine/formatters/android.rb +++ b/lib/twine/formatters/android.rb @@ -104,8 +104,8 @@ module Twine # 2) ampersand and less-than must be in XML-escaped form value.gsub!('&', '&') value.gsub!('<', '<') - # 3) use "s" instead of "@" for substituting strings - value.gsub!(/%([0-9\$]*)@/, '%\1s') + # 3) fix substitutions (e.g. %s/%@) + value = fix_substitutions(value) comment = row.comment if comment @@ -124,6 +124,52 @@ module Twine f.puts '' end end + + def fix_substitutions(str) + # 1) use "s" instead of "@" for substituting strings + str.gsub!(/%([0-9\$]*)@/, '%\1s') + + # 2) if there is more than one substitution in a string, make sure they are numbered + substituteCount = 0 + startFound = false + str.each_char do |c| + if startFound + if c == "%" + # ignore as this is a literal % + elsif c.match(/\d/) + # leave the string alone if it already has numbered substitutions + return str + else + substituteCount += 1 + end + startFound = false + elsif c == "%" + startFound = true + end + end + + if substituteCount > 1 + currentSub = 1 + startFound = false + newstr = "" + str.each_char do |c| + if startFound + if !(c == "%") + newstr = newstr + "#{currentSub}$" + currentSub += 1 + end + startFound = false + elsif c == "%" + startFound = true + end + newstr = newstr + c + end + return newstr + else + return str + end + end + end end end