This document describes how query strings are formatted before being sent to Sourcegraph. The goal is to match C/C++ source code literals as reliably as possible.
Strings in results.json are loaded by Python with real control characters:
JSON \\n becomes an actual newline. If sent raw, Sourcegraph splits the query
into multiple terms and the exact string literal is lost.
-
Trim trailing whitespace
rstrip()is applied to reduce accidental mismatches.
-
Convert control chars to C-style escapes
\r->\\r\n->\\n\t->\\t
-
Quote and escape with
json.dumps- Produces a safe
content:"..."filter with properly escaped quotes and backslashes.
- Produces a safe
-
Force keyword mode and limit languages
- Ensures literal matching and avoids regex interpretation.
-
CRLF fallback
- If the cooked string contains
\nbut not\r, a second query is added with\nreplaced by\r\nusingOR.
- If the cooked string contains
type:file patterntype:keyword count:5 (lang:c OR lang:c++)
content:"HTTP/1.0 400 Bad Request\\nServer: CS241Serv v0.1\\nContent-Type: text/html\\n\\n"
ghidra_string_sniper_ext/data/python/sourcegraph_query.pystringify_for_c_source()to_sourcegraph_content_filter()build_sg_query()