(str: string)
| 4 | } |
| 5 | |
| 6 | export function toCString(str: string): CString { |
| 7 | const buffer = Buffer.from(str, 'utf-8'); |
| 8 | let literalContent = '"'; |
| 9 | |
| 10 | for (let i = 0; i < buffer.length; i++) { |
| 11 | const c = buffer[i]; |
| 12 | |
| 13 | if (c >= 32 && c <= 126) { |
| 14 | // Visible character |
| 15 | if (c === 0x22) { |
| 16 | literalContent += '\\"'; |
| 17 | } else if (c === 0x5c) { |
| 18 | literalContent += '\\\\'; |
| 19 | } else { |
| 20 | literalContent += String.fromCharCode(c); |
| 21 | } |
| 22 | } else if (c === 0x0a) { |
| 23 | literalContent += '\\n'; |
| 24 | } else if (c === 0x09) { |
| 25 | literalContent += '\\t'; |
| 26 | } else { |
| 27 | literalContent += '\\x'; |
| 28 | literalContent += c.toString(16); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | literalContent += '"'; |
| 33 | |
| 34 | return { |
| 35 | literalContent, |
| 36 | length: buffer.length, |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | export function escapeCComment(str: string): string { |
| 41 | return str.replace(/\*\//g, '*\\/').replace('/*', '/\\*'); |
no test coverage detected