@@ -226,16 +226,16 @@ internal void AppendSqlString(ByteBufferWriter writer, StatementPreparerOptions
226226 writer . Write ( ( byte ) '\' ' ) ;
227227 switch ( charValue )
228228 {
229- case '\' ' :
230- writer . Write ( ( byte ) '\' ' ) ;
231- writer . Write ( ( byte ) charValue ) ;
229+ case '\0 ' when ! noBackslashEscapes :
230+ writer . Write ( ( ushort ) 0x305C ) ; // \0
232231 break ;
233232
234- case '\\ ' :
235- if ( ! noBackslashEscapes )
236- writer . Write ( ( byte ) ' \\ ' ) ;
233+ case '\' ' :
234+ writer . Write ( ( ushort ) 0x2727 ) ; // ''
235+ break ;
237236
238- writer . Write ( ( byte ) charValue ) ;
237+ case '\\ ' when ! noBackslashEscapes :
238+ writer . Write ( ( ushort ) 0x5C5C ) ; // \\
239239 break ;
240240
241241 default :
@@ -288,7 +288,7 @@ internal void AppendSqlString(ByteBufferWriter writer, StatementPreparerOptions
288288 var length = inputSpan . Length + BinaryBytes . Length + 1 ;
289289 foreach ( var by in inputSpan )
290290 {
291- if ( by is 0x27 || by is 0x5C && ! noBackslashEscapes )
291+ if ( by is 0x27 || by is 0x00 or 0x5C && ! noBackslashEscapes )
292292 length ++ ;
293293 }
294294
@@ -297,9 +297,18 @@ internal void AppendSqlString(ByteBufferWriter writer, StatementPreparerOptions
297297 var index = BinaryBytes . Length ;
298298 foreach ( var by in inputSpan )
299299 {
300- if ( by is 0x27 || by is 0x5C && ! noBackslashEscapes )
300+ if ( by == 0x00 && ! noBackslashEscapes )
301+ {
302+ // \0
303+ outputSpan [ index ++ ] = 0x5C ;
304+ outputSpan [ index ++ ] = 0x30 ;
305+ }
306+ else
307+ {
308+ if ( by is 0x27 || by is 0x5C && ! noBackslashEscapes )
309+ outputSpan [ index ++ ] = by ;
301310 outputSpan [ index ++ ] = by ;
302- outputSpan [ index ++ ] = by ;
311+ }
303312 }
304313 outputSpan [ index ++ ] = 0x27 ;
305314 Debug . Assert ( index == length , "index == length" ) ;
@@ -394,9 +403,9 @@ internal void AppendSqlString(ByteBufferWriter writer, StatementPreparerOptions
394403 writer . Write ( BinaryBytes ) ;
395404 foreach ( var by in bytes )
396405 {
397- if ( by is 0x27 or 0x5C )
406+ if ( by is 0x00 or 0x27 or 0x5C )
398407 writer . Write ( ( byte ) 0x5C ) ;
399- writer . Write ( by ) ;
408+ writer . Write ( by == 0 ? ( byte ) 0x30 : by ) ;
400409 }
401410 writer . Write ( ( byte ) '\' ' ) ;
402411 }
@@ -471,7 +480,7 @@ static void WriteString(ByteBufferWriter writer, bool noBackslashEscapes, string
471480 if ( noBackslashEscapes )
472481 writer . Write ( value . Replace ( "'" , "''" ) ) ;
473482 else
474- writer . Write ( value . Replace ( "\\ " , "\\ \\ " ) . Replace ( "'" , "''" ) ) ;
483+ writer . Write ( value . Replace ( "\\ " , "\\ \\ " ) . Replace ( "'" , "''" ) . Replace ( " \0 " , " \\ 0" ) ) ;
475484
476485 writer . Write ( ( byte ) '\' ' ) ;
477486 }
@@ -485,7 +494,7 @@ static void WriteString(ByteBufferWriter writer, bool noBackslashEscapes, bool w
485494 while ( charsWritten < value . Length )
486495 {
487496 var remainingValue = value . Slice ( charsWritten ) ;
488- var nextDelimiterIndex = remainingValue . IndexOfAny ( '\' ' , '\\ ' ) ;
497+ var nextDelimiterIndex = remainingValue . IndexOfAny ( '\0 ' , ' \ ' ', '\\ ' ) ;
489498 if ( nextDelimiterIndex == - 1 )
490499 {
491500 // write the rest of the string
@@ -495,11 +504,17 @@ static void WriteString(ByteBufferWriter writer, bool noBackslashEscapes, bool w
495504 else
496505 {
497506 // write up to (and including) the delimiter, then double it
498- writer . Write ( remainingValue . Slice ( 0 , nextDelimiterIndex + 1 ) , flush : true ) ;
507+ writer . Write ( remainingValue . Slice ( 0 , nextDelimiterIndex ) , flush : true ) ;
499508 if ( remainingValue [ nextDelimiterIndex ] == '\\ ' && ! noBackslashEscapes )
500- writer . Write ( ( byte ) '\\ ' ) ;
509+ writer . Write ( ( ushort ) 0x5C5C ) ; // \\
510+ else if ( remainingValue [ nextDelimiterIndex ] == '\\ ' && noBackslashEscapes )
511+ writer . Write ( ( byte ) 0x5C ) ; // \
501512 else if ( remainingValue [ nextDelimiterIndex ] == '\' ' )
502- writer . Write ( ( byte ) '\' ' ) ;
513+ writer . Write ( ( ushort ) 0x2727 ) ; // ''
514+ else if ( remainingValue [ nextDelimiterIndex ] == '\0 ' && ! noBackslashEscapes )
515+ writer . Write ( ( ushort ) 0x305C ) ; // \0
516+ else if ( remainingValue [ nextDelimiterIndex ] == '\0 ' && noBackslashEscapes )
517+ writer . Write ( ( byte ) 0x00 ) ; // (nul)
503518 charsWritten += nextDelimiterIndex + 1 ;
504519 }
505520 }
0 commit comments