Commit 888d3b39
888d3b39e271Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #326 from ryanseys/fix/c-string-literal-backslash
fix(codegen): c_string_literal preserves literal backslashes
Details
- Commit
- 888d3b39e271
- Parent
- 2578cf78555c
- Merge parent
- 4c63fda3bee2
spinel_codegen.rbmodifiedView file
@@ -19857,45 +19857,20 @@
1985719857
end1985819858
1985919859
def c_string_literal(s)19860+
# Input `s` is the runtime Ruby string content (already-cooked: any19861+
# backslash in `s` is a literal backslash, NOT an escape introducer).19862+
# We C-escape the small set that needs it: backslash, double-quote,19863+
# newline, carriage return, tab. Everything else copies through.19864+
# The previous version treated `s` as if it still carried Ruby19865+
# escapes, so a 2-char input "\\n" (backslash + n) wrongly collapsed19866+
# to a C newline; that bug is what made `"hello\\nworld\\n".lines`19867+
# emit invalid C with a literal newline inside a string literal.1986019868
result = "\""1986119869
i = 01986219870
while i < s.length1986319871
ch = s[i]1986419872
if ch == bsl19865-
# Check for Ruby escape sequences19866-
if i + 1 < s.length19867-
nch = s[i + 1]19868-
if nch == "n"19869-
result = result + bsl_n19870-
i = i + 219871-
else19872-
if nch == "t"19873-
result = result + bsl + "t"19874-
i = i + 219875-
else19876-
if nch == "r"19877-
result = result + bsl + "r"19878-
i = i + 219879-
else19880-
if nch == bsl19881-
result = result + bsl + bsl19882-
i = i + 219883-
else19884-
if nch == "\""19885-
result = result + bsl + "\""19886-
i = i + 219887-
else19888-
result = result + bsl + bsl19889-
i = i + 119890-
end19891-
end19892-
end19893-
end19894-
end19895-
else19896-
result = result + "\\\\"19897-
i = i + 119898-
end19873+
result = result + bsl + bsl1989919874
else1990019875
if ch == "\""1990119876
result = result + bsl + "\""@@ -19914,8 +19889,8 @@
1991419889
end1991519890
end1991619891
end19917-
i = i + 11991819892
end19893+
i = i + 11991919894
end1992019895
# Prepend 0xff marker byte so GC can identify static literals.1992119896
# Return form: (&("\xff" "content")[1]) — same pointer value as thetest/string_escape.rbaddedView file
Loading diff…
test/string_escape.rb.expectedaddedView file
Loading diff…