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 end
1985819858
1985919859 def c_string_literal(s)
19860+ # Input `s` is the runtime Ruby string content (already-cooked: any
19861+ # 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 Ruby
19865+ # escapes, so a 2-char input "\\n" (backslash + n) wrongly collapsed
19866+ # 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 = 0
1986219870 while i < s.length
1986319871 ch = s[i]
1986419872 if ch == bsl
19865- # Check for Ruby escape sequences
19866- if i + 1 < s.length
19867- nch = s[i + 1]
19868- if nch == "n"
19869- result = result + bsl_n
19870- i = i + 2
19871- else
19872- if nch == "t"
19873- result = result + bsl + "t"
19874- i = i + 2
19875- else
19876- if nch == "r"
19877- result = result + bsl + "r"
19878- i = i + 2
19879- else
19880- if nch == bsl
19881- result = result + bsl + bsl
19882- i = i + 2
19883- else
19884- if nch == "\""
19885- result = result + bsl + "\""
19886- i = i + 2
19887- else
19888- result = result + bsl + bsl
19889- i = i + 1
19890- end
19891- end
19892- end
19893- end
19894- end
19895- else
19896- result = result + "\\\\"
19897- i = i + 1
19898- end
19873+ result = result + bsl + bsl
1989919874 else
1990019875 if ch == "\""
1990119876 result = result + bsl + "\""
@@ -19914,8 +19889,8 @@
1991419889 end
1991519890 end
1991619891 end
19917- i = i + 1
1991819892 end
19893+ i = i + 1
1991919894 end
1992019895 # Prepend 0xff marker byte so GC can identify static literals.
1992119896 # Return form: (&("\xff" "content")[1]) — same pointer value as the