MCPcopy Create free account
hub / github.com/Snapchat/Valdi / toCString

Function toCString

compiler/companion/src/native/utils/StringEscape.ts:6–38  ·  view source on GitHub ↗
(str: string)

Source from the content-addressed store, hash-verified

4}
5
6export 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
40export function escapeCComment(str: string): string {
41 return str.replace(/\*\//g, '*\\/').replace('/*', '/\\*');

Callers 3

emitLiteralStringMethod · 0.90
finalizeMethod · 0.90

Calls 2

toStringMethod · 0.65
fromMethod · 0.45

Tested by

no test coverage detected