Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Escape carriage return when round-tripping

Previously the carriage return character ("\r") was not round-tripped
properly.

This will cause a warning on the (upcoming) Ruby 2.1.0 as a plain
carriage return that is not at end-of-line is treated as a space.
  • Loading branch information...
commit a88bcbcafc25c07b31d67caba57524f5c16eba25 1 parent 20bfe5c
@drbrain drbrain authored
View
1  lib/kpeg/string_escape.kpeg
@@ -7,6 +7,7 @@
segment = < /[\w ]+/ > { text } # Don't use \s because that matchs \n
| "\\" { "\\\\" }
| "\n" { "\\n" }
+ | "\r" { "\\r" }
| "\t" { "\\t" }
| "\b" { "\\b" }
| "\"" { "\\\"" }
View
38 lib/kpeg/string_escape.rb
@@ -363,7 +363,7 @@ def self.rule_info(name, rendered)
# :stopdoc:
def setup_foreign_grammar; end
- # segment = (< /[\w ]+/ > { text } | "\\" { "\\\\" } | "\n" { "\\n" } | "\t" { "\\t" } | "\b" { "\\b" } | "\"" { "\\\"" } | < . > { text })
+ # segment = (< /[\w ]+/ > { text } | "\\" { "\\\\" } | "\n" { "\\n" } | "\r" { "\\r" } | "\t" { "\\t" } | "\b" { "\\b" } | "\"" { "\\\"" } | < . > { text })
def _segment
_save = self.pos
@@ -429,12 +429,12 @@ def _segment
_save4 = self.pos
while true # sequence
- _tmp = match_string("\t")
+ _tmp = match_string("\r")
unless _tmp
self.pos = _save4
break
end
- @result = begin; "\\t" ; end
+ @result = begin; "\\r" ; end
_tmp = true
unless _tmp
self.pos = _save4
@@ -447,12 +447,12 @@ def _segment
_save5 = self.pos
while true # sequence
- _tmp = match_string("\b")
+ _tmp = match_string("\t")
unless _tmp
self.pos = _save5
break
end
- @result = begin; "\\b" ; end
+ @result = begin; "\\t" ; end
_tmp = true
unless _tmp
self.pos = _save5
@@ -465,12 +465,12 @@ def _segment
_save6 = self.pos
while true # sequence
- _tmp = match_string("\"")
+ _tmp = match_string("\b")
unless _tmp
self.pos = _save6
break
end
- @result = begin; "\\\"" ; end
+ @result = begin; "\\b" ; end
_tmp = true
unless _tmp
self.pos = _save6
@@ -483,19 +483,37 @@ def _segment
_save7 = self.pos
while true # sequence
+ _tmp = match_string("\"")
+ unless _tmp
+ self.pos = _save7
+ break
+ end
+ @result = begin; "\\\"" ; end
+ _tmp = true
+ unless _tmp
+ self.pos = _save7
+ end
+ break
+ end # end sequence
+
+ break if _tmp
+ self.pos = _save
+
+ _save8 = self.pos
+ while true # sequence
_text_start = self.pos
_tmp = get_byte
if _tmp
text = get_text(_text_start)
end
unless _tmp
- self.pos = _save7
+ self.pos = _save8
break
end
@result = begin; text ; end
_tmp = true
unless _tmp
- self.pos = _save7
+ self.pos = _save8
end
break
end # end sequence
@@ -603,7 +621,7 @@ def _embed
end
Rules = {}
- Rules[:_segment] = rule_info("segment", "(< /[\\w ]+/ > { text } | \"\\\\\" { \"\\\\\\\\\" } | \"\\n\" { \"\\\\n\" } | \"\\t\" { \"\\\\t\" } | \"\\b\" { \"\\\\b\" } | \"\\\"\" { \"\\\\\\\"\" } | < . > { text })")
+ Rules[:_segment] = rule_info("segment", "(< /[\\w ]+/ > { text } | \"\\\\\" { \"\\\\\\\\\" } | \"\\n\" { \"\\\\n\" } | \"\\r\" { \"\\\\r\" } | \"\\t\" { \"\\\\t\" } | \"\\b\" { \"\\\\b\" } | \"\\\"\" { \"\\\\\\\"\" } | < . > { text })")
Rules[:_root] = rule_info("root", "segment*:s { @text = s.join }")
Rules[:_embed_seg] = rule_info("embed_seg", "(\"\#\" { \"\\\\\#\" } | segment)")
Rules[:_embed] = rule_info("embed", "embed_seg*:s { @text = s.join }")
View
42 test/test_kpeg_string_escape.rb
@@ -0,0 +1,42 @@
+require 'minitest/autorun'
+require 'kpeg'
+require 'kpeg/string_escape'
+
+class TestKPegStringEscape < Minitest::Test
+
+ def test_bell
+ assert_equal '\b', parse("\b")
+ end
+
+ def test_carriage_return
+ assert_equal '\r', parse("\r")
+ end
+
+ def test_newline
+ assert_equal '\n', parse("\n")
+ end
+
+ def test_quote
+ assert_equal '\\\\\"', parse('\\"')
+ end
+
+ def test_slash
+ assert_equal '\\\\', parse('\\')
+ end
+
+ def test_tab
+ assert_equal '\t', parse("\t")
+ end
+
+ def parse(str, embed = false)
+ se = KPeg::StringEscape.new(str)
+
+ rule = (embed ? 'embed' : nil)
+
+ se.raise_error unless se.parse(rule)
+
+ se.text
+ end
+
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.