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)
| 35 | /// The entire input is covered exactly once (no bytes are skipped). Adjacent |
| 36 | /// `Text` bytes are collected into a single segment. |
| 37 | pub 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 { |