@@ -55,15 +55,21 @@ impl MappedSql {
5555 }
5656 }
5757
58- fn slice ( & self , start : usize , end : usize ) -> Self {
59- let first = self . sql [ ..start] . chars ( ) . count ( ) ;
60- let last = first + self . sql [ start..end] . chars ( ) . count ( ) ;
58+ /// The text from byte `start` to `end`, with `cursor` counting characters along `self.sql`.
59+ fn slice ( & self , cursor : & mut CharCursor , start : usize , end : usize ) -> Self {
60+ let first: usize = cursor. at ( & self . sql , start) ;
61+ let last: usize = cursor. at ( & self . sql , end) ;
6162 Self {
6263 sql : self . sql [ start..end] . to_owned ( ) ,
6364 offsets : self . offsets [ first..=last] . to_vec ( ) ,
6465 }
6566 }
6667
68+ /// The source offset of the character at byte `byte`.
69+ fn anchor ( & self , cursor : & mut CharCursor , byte : usize ) -> usize {
70+ self . offsets [ cursor. at ( & self . sql , byte) ]
71+ }
72+
6773 fn generated ( text : String , anchor : usize ) -> Self {
6874 Self {
6975 offsets : vec ! [ anchor; text. chars( ) . count( ) + 1 ] ,
@@ -81,18 +87,35 @@ impl MappedSql {
8187 if edits. is_empty ( ) {
8288 return self ;
8389 }
84- let mut result = self . slice ( 0 , 0 ) ;
90+ let mut cursor: CharCursor = CharCursor :: default ( ) ;
91+ let mut result = self . slice ( & mut cursor, 0 , 0 ) ;
8592 let mut previous = 0 ;
8693 for ( start, end, replacement) in edits {
87- result. append ( self . slice ( previous, start) ) ;
94+ result. append ( self . slice ( & mut cursor , previous, start) ) ;
8895 result. append ( replacement) ;
8996 previous = end;
9097 }
91- result. append ( self . slice ( previous, self . sql . len ( ) ) ) ;
98+ result. append ( self . slice ( & mut cursor , previous, self . sql . len ( ) ) ) ;
9299 result
93100 }
94101}
95102
103+ /// The character index of byte offsets read in increasing order, counted on from the last one read.
104+ #[ derive( Default ) ]
105+ struct CharCursor {
106+ byte : usize ,
107+ chars : usize ,
108+ }
109+
110+ impl CharCursor {
111+ /// The number of characters of `sql` before byte `byte`, which is at or after the last one read.
112+ fn at ( & mut self , sql : & str , byte : usize ) -> usize {
113+ self . chars += sql[ self . byte ..byte] . chars ( ) . count ( ) ;
114+ self . byte = byte;
115+ self . chars
116+ }
117+ }
118+
96119pub ( super ) fn normalize (
97120 sql : & str ,
98121 dialect : & str ,
@@ -101,6 +124,7 @@ pub(super) fn normalize(
101124) -> Result < MappedSql , String > {
102125 let mut result = MappedSql :: new ( sql) ;
103126 let mut edits: Vec < Edit > = Vec :: new ( ) ;
127+ let mut cursor: CharCursor = CharCursor :: default ( ) ;
104128 for captures in REFERENCES
105129 . as_ref ( )
106130 . map_err ( Clone :: clone) ?
@@ -111,15 +135,16 @@ pub(super) fn normalize(
111135 let replacement = if & captures[ 1 ] != DBT_REF && stubs. contains_key ( name. as_str ( ) ) {
112136 MappedSql :: generated (
113137 stubs[ name. as_str ( ) ] . clone ( ) ,
114- result. offsets [ result . sql [ .. name. start ( ) ] . chars ( ) . count ( ) ] ,
138+ result. anchor ( & mut cursor , name. start ( ) ) ,
115139 )
116140 } else {
117- result. slice ( name. start ( ) , name. end ( ) )
141+ result. slice ( & mut cursor , name. start ( ) , name. end ( ) )
118142 } ;
119143 edits. push ( ( whole. start ( ) , whole. end ( ) , replacement) ) ;
120144 }
121145 result = result. edits ( edits) ;
122146 let mut edits: Vec < Edit > = Vec :: new ( ) ;
147+ let mut cursor: CharCursor = CharCursor :: default ( ) ;
123148 let mut consumed_until = 0 ;
124149 let non_code: Vec < ( usize , usize ) > =
125150 dialect_non_code_ranges ( & result. sql , & dialect_lexical_syntax ( dialect) ) ;
@@ -157,7 +182,7 @@ pub(super) fn normalize(
157182 edits. push ( (
158183 start,
159184 end,
160- MappedSql :: generated ( stub, result. offsets [ result . sql [ ..start ] . chars ( ) . count ( ) ] ) ,
185+ MappedSql :: generated ( stub, result. anchor ( & mut cursor , start ) ) ,
161186 ) ) ;
162187 consumed_until = end;
163188 }
@@ -166,6 +191,7 @@ pub(super) fn normalize(
166191 result = normalize_variant ( result) ?;
167192 }
168193 let mut edits: Vec < Edit > = Vec :: new ( ) ;
194+ let mut cursor: CharCursor = CharCursor :: default ( ) ;
169195 for captures in PLACEHOLDERS
170196 . as_ref ( )
171197 . map_err ( Clone :: clone) ?
@@ -176,10 +202,7 @@ pub(super) fn normalize(
176202 edits. push ( (
177203 whole. start ( ) ,
178204 whole. end ( ) ,
179- MappedSql :: generated (
180- value. clone ( ) ,
181- result. offsets [ result. sql [ ..whole. start ( ) ] . chars ( ) . count ( ) ] ,
182- ) ,
205+ MappedSql :: generated ( value. clone ( ) , result. anchor ( & mut cursor, whole. start ( ) ) ) ,
183206 ) ) ;
184207 }
185208 }
@@ -213,10 +236,11 @@ pub(crate) fn normalize_native_sql_mapped(
213236 placeholders : & [ ( String , String ) ] ,
214237) -> Result < ( String , Vec < usize > ) , String > {
215238 let native: MappedSql = native_mapped ( sql, dialect, stub_prefix, & HashMap :: new ( ) ) ?;
239+ let mut cursor: CharCursor = CharCursor :: default ( ) ;
216240 let edits: Vec < Edit > = placeholder_default_edits ( & native. sql , placeholders)
217241 . into_iter ( )
218242 . map ( |( start, end, default) | {
219- let anchor: usize = native. offsets [ native . sql [ ..start ] . chars ( ) . count ( ) ] ;
243+ let anchor: usize = native. anchor ( & mut cursor , start ) ;
220244 ( start, end, MappedSql :: generated ( default. to_owned ( ) , anchor) )
221245 } )
222246 . collect ( ) ;
@@ -237,6 +261,7 @@ fn native_mapped(
237261 let non_code: Vec < ( usize , usize ) > =
238262 dialect_non_code_ranges ( & normalized. sql , & dialect_lexical_syntax ( dialect) ) ;
239263 let mut edits: Vec < Edit > = Vec :: new ( ) ;
264+ let mut cursor: CharCursor = CharCursor :: default ( ) ;
240265 for captures in stubs. captures_iter ( & normalized. sql ) {
241266 let whole = captures. get ( 0 ) . ok_or ( "missing stub match" ) ?;
242267 let quoted: bool = non_code
@@ -255,7 +280,7 @@ fn native_mapped(
255280 ( None , Some ( parameter) ) => format ! ( "'{HOOK_PARAMETER_PREFIX}{}'" , parameter. as_str( ) ) ,
256281 ( None , None ) => continue ,
257282 } ;
258- let anchor: usize = normalized. offsets [ normalized . sql [ .. whole. start ( ) ] . chars ( ) . count ( ) ] ;
283+ let anchor: usize = normalized. anchor ( & mut cursor , whole. start ( ) ) ;
259284 edits. push ( (
260285 whole. start ( ) ,
261286 whole. end ( ) ,
@@ -337,6 +362,8 @@ fn matching_bracket(sql: &str, start: usize) -> Result<Option<usize>, String> {
337362fn normalize_variant ( input : MappedSql ) -> Result < MappedSql , String > {
338363 let sql = & input. sql ;
339364 let mut edits: Vec < Edit > = Vec :: new ( ) ;
365+ let mut cursor: CharCursor = CharCursor :: default ( ) ;
366+ let mut key_cursor: CharCursor = CharCursor :: default ( ) ;
340367 let mut index = 0 ;
341368 while index < sql. len ( ) {
342369 if let Some ( end) = skip_non_code ( sql, index) ? {
@@ -359,7 +386,7 @@ fn normalize_variant(input: MappedSql) -> Result<MappedSql, String> {
359386 index = close + 1 ;
360387 continue ;
361388 }
362- let key = normalize_variant ( input. slice ( index + 1 , close) ) ?;
389+ let key = normalize_variant ( input. slice ( & mut key_cursor , index + 1 , close) ) ?;
363390 let mut base_end = index;
364391 loop {
365392 base_end = sql[ ..base_end] . trim_end ( ) . len ( ) ;
@@ -376,17 +403,17 @@ fn normalize_variant(input: MappedSql) -> Result<MappedSql, String> {
376403 . map_err ( Clone :: clone) ?
377404 . find ( & sql[ ..base_end] )
378405 {
379- let anchor = input. offsets [ sql [ .. base. start ( ) ] . chars ( ) . count ( ) ] ;
406+ let anchor = input. anchor ( & mut cursor , base. start ( ) ) ;
380407 let mut replacement = MappedSql :: generated ( "GET(" . to_owned ( ) , anchor) ;
381- replacement. append ( input. slice ( base. start ( ) , index) ) ;
408+ replacement. append ( input. slice ( & mut cursor , base. start ( ) , index) ) ;
382409 replacement. append ( MappedSql :: generated (
383410 ", " . to_owned ( ) ,
384- input. offsets [ sql [ ..index ] . chars ( ) . count ( ) ] ,
411+ input. anchor ( & mut cursor , index ) ,
385412 ) ) ;
386413 replacement. append ( key) ;
387414 replacement. append ( MappedSql :: generated (
388415 ")" . to_owned ( ) ,
389- input. offsets [ sql [ ..close ] . chars ( ) . count ( ) ] ,
416+ input. anchor ( & mut cursor , close ) ,
390417 ) ) ;
391418 edits. push ( ( base. start ( ) , close + 1 , replacement) ) ;
392419 } else if key. sql != sql[ index + 1 ..close] {
0 commit comments