MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / maskCppRawStrings

Function maskCppRawStrings

src/extraction/languages/c-cpp.ts:1228–1265  ·  view source on GitHub ↗
(source: string)

Source from the content-addressed store, hash-verified

1226 * use this helper so they are safe when called directly.
1227 */
1228export function maskCppRawStrings(source: string): { source: string; restore: (blanked: string) => string } {
1229 const unchanged = { source, restore: (blanked: string): string => blanked };
1230 if (source.indexOf('R"') === -1) return unchanged;
1231 // Skip comments and ordinary literals before looking for a raw opener. The
1232 // char-literal boundary leaves numeric digit separators (1'000) alone.
1233 const re = /\/\/[^\r\n]*|\/\*[\s\S]*?(?:\*\/|$)|\b(?:u8|[LuU])?R"([^ \t\v\f\r\n()\\]{0,16})\(|"(?:\\[\s\S]|[^"\\])*(?:"|$)|(?<!\w)(?:u8|[LuU])?'(?:\\[\s\S]|[^'\\])*(?:'|$)/g;
1234 const spans: Array<{ start: number; end: number }> = [];
1235 const parts: string[] = [];
1236 let last = 0;
1237 let m: RegExpExecArray | null;
1238 while ((m = re.exec(source)) !== null) {
1239 if (m[1] === undefined) continue;
1240 const closer = `)${m[1]}"`;
1241 const close = source.indexOf(closer, re.lastIndex);
1242 // An unterminated raw literal owns the rest of the file too.
1243 const end = close < 0 ? source.length : close + closer.length;
1244 spans.push({ start: m.index, end });
1245 parts.push(source.slice(last, m.index), source.slice(m.index, end).replace(/[^\r\n]/g, '\0'));
1246 last = re.lastIndex = end;
1247 }
1248 if (spans.length === 0) return unchanged;
1249 parts.push(source.slice(last));
1250 const masked = parts.join('');
1251 return {
1252 source: masked,
1253 restore(blanked): string {
1254 // A length-changing rewrite cannot be restored at the original offsets.
1255 if (blanked === masked || blanked.length !== source.length) return source;
1256 const chars = blanked.split('');
1257 for (const { start, end } of spans) {
1258 for (let i = start; i < end; i++) {
1259 if (chars[i] === '\0') chars[i] = source[i] as string;
1260 }
1261 }
1262 return chars.join('');
1263 },
1264 };
1265}
1266
1267/**
1268 * Blank annotation-style macro invocations that decorate a declaration but carry

Callers 10

directiveLinesFunction · 0.90
preParseCppSourceFunction · 0.85
blankCTypeKeywordArgsFunction · 0.85
preParseCSourceFunction · 0.85

Calls 3

pushMethod · 0.80
execMethod · 0.65
joinMethod · 0.45

Tested by

no test coverage detected