MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / segments

Function segments

nodedb-sql/src/parser/preprocess/lex.rs:37–161  ·  view source on GitHub ↗

Segment a SQL string into classified [`SqlSegment`]s. The entire input is covered exactly once (no bytes are skipped). Adjacent `Text` bytes are collected into a single segment.

(sql: &str)

Source from the content-addressed store, hash-verified

35/// The entire input is covered exactly once (no bytes are skipped). Adjacent
36/// `Text` bytes are collected into a single segment.
37pub fn segments(sql: &str) -> Vec<SqlSegment<'_>> {
38 let mut out = Vec::new();
39 let bytes = sql.as_bytes();
40 let len = bytes.len();
41 let mut i = 0;
42 let mut text_start = 0;
43
44 macro_rules! flush_text {
45 () => {
46 if text_start < i {
47 out.push(SqlSegment::Text(&sql[text_start..i]));
48 }
49 };
50 }
51
52 while i < len {
53 // ── single-quoted string ──────────────────────────────────────────
54 // Optional `E` or `e` escape prefix before the opening quote.
55 let is_escape_prefix =
56 (bytes[i] == b'E' || bytes[i] == b'e') && i + 1 < len && bytes[i + 1] == b'\'';
57
58 if bytes[i] == b'\'' || is_escape_prefix {
59 flush_text!();
60 let start = i;
61 if is_escape_prefix {
62 i += 1; // skip `E`
63 }
64 i += 1; // skip opening `'`
65 let escape = is_escape_prefix;
66 while i < len {
67 match bytes[i] {
68 b'\\' if escape => {
69 // backslash escape: skip two chars
70 i += 2;
71 }
72 b'\'' => {
73 i += 1;
74 // doubled-quote escape `''`
75 if i < len && bytes[i] == b'\'' {
76 i += 1;
77 } else {
78 break;
79 }
80 }
81 _ => i += 1,
82 }
83 }
84 out.push(SqlSegment::SingleQuotedString(&sql[start..i]));
85 text_start = i;
86 continue;
87 }
88
89 // ── double-quoted identifier ──────────────────────────────────────
90 if bytes[i] == b'"' {
91 flush_text!();
92 let start = i;
93 i += 1; // skip opening `"`
94 while i < len {

Callers 15

last_text_segmentFunction · 0.85
first_sql_wordFunction · 0.85
second_sql_wordFunction · 0.85
find_operator_positionsFunction · 0.85
quoted_ident_opaqueFunction · 0.85
line_comment_opaqueFunction · 0.85
block_comment_opaqueFunction · 0.85
nested_block_commentFunction · 0.85

Calls 3

as_bytesMethod · 0.45
lenMethod · 0.45
pushMethod · 0.45

Tested by 8

quoted_ident_opaqueFunction · 0.68
line_comment_opaqueFunction · 0.68
block_comment_opaqueFunction · 0.68
nested_block_commentFunction · 0.68
escape_string_prefixFunction · 0.68