Skip to content

Commit a9b9583

Browse files
committed
po parser: fix a too much unescape bug
1 parent 237630f commit a9b9583

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

src/po_parser.ry

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class GetText::POParser
2424
msgctxt
2525
: MSGCTXT string_list
2626
{
27-
@msgctxt = unescape(val[1])
27+
@msgctxt = val[1]
2828
}
2929
;
3030

@@ -36,9 +36,8 @@ class GetText::POParser
3636
single_message
3737
: MSGID string_list MSGSTR string_list
3838
{
39-
msgid_raw = val[1]
40-
msgid = unescape(msgid_raw)
41-
msgstr = unescape(val[3])
39+
msgid = val[1]
40+
msgstr = val[3]
4241
use_message_p = true
4342
if @fuzzy and not msgid.empty?
4443
use_message_p = (not ignore_fuzzy?)
@@ -48,7 +47,7 @@ class GetText::POParser
4847
else
4948
$stderr.print _("Warning: fuzzy message was used.\n")
5049
end
51-
$stderr.print " #{@po_file}: msgid '#{msgid_raw}'\n"
50+
$stderr.print " #{@po_file}: msgid '#{msgid}'\n"
5251
end
5352
end
5453
@fuzzy = false
@@ -144,20 +143,23 @@ require "gettext/po"
144143
@report_warning
145144
end
146145

147-
def unescape(orig)
148-
ret = orig.gsub(/\\n/, "\n")
149-
ret.gsub!(/\\t/, "\t")
150-
ret.gsub!(/\\r/, "\r")
151-
ret.gsub!(/\\"/, "\"")
152-
ret
146+
def unescape(string)
147+
string.gsub(/\\(.)/) do
148+
escaped_character = $1
149+
case escaped_character
150+
when "t"
151+
"\t"
152+
when "r"
153+
"\r"
154+
when "n"
155+
"\n"
156+
else
157+
escaped_character
158+
end
159+
end
153160
end
154161
private :unescape
155162

156-
def unescape_string(string)
157-
string.gsub(/\\\\/, "\\")
158-
end
159-
private :unescape_string
160-
161163
def parse(str, data)
162164
@translator_comments = []
163165
@extracted_comments = []
@@ -202,7 +204,7 @@ require "gettext/po"
202204
@q.push [:COMMENT, $&]
203205
str = $'
204206
when /\A\"(.*)\"/
205-
@q.push [:STRING, unescape_string($1)]
207+
@q.push [:STRING, unescape($1)]
206208
str = $'
207209
else
208210
#c = str[0,1]

test/test_po_parser.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ def test_plural
6262
class TestPO < self
6363
include Helper::Warning
6464

65+
def test_msgid_escape
66+
po_file = create_po_file(<<-'EOP')
67+
msgid "\\\\r \\r \t \r \n"
68+
msgstr "\\\\r \\r \t \r \n"
69+
EOP
70+
entries = parse_po_file(po_file)
71+
assert_equal("\\\\r \\r \t \r \n",
72+
entries["\\\\r \\r \t \r \n"].msgstr)
73+
end
74+
6575
def test_msgstr
6676
po_file = create_po_file(<<-EOP)
6777
# This is the comment.

0 commit comments

Comments
 (0)