File tree Expand file tree Collapse file tree 3 files changed +26
-3
lines changed
ReadableExpressions.UnitTests
ReadableExpressions/Translations Expand file tree Collapse file tree 3 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,17 @@ public void ShouldTranslateAString()
2727 translated . ShouldBe ( "\" hello!\" " ) ;
2828 }
2929
30+ // See https://github.com/agileobjects/ReadableExpressions/issues/43
31+ [ Fact ]
32+ public void ShouldTranslateAStringWithANullTerminatingCharacter ( )
33+ {
34+ var stringConstant = Constant ( "hel\0 lo!" , typeof ( string ) ) ;
35+
36+ var translated = ToReadableString ( stringConstant ) ;
37+
38+ translated . ShouldBe ( @"""hel\0lo!""" ) ;
39+ }
40+
3041 [ Fact ]
3142 public void ShouldTranslateABoolean ( )
3243 {
Original file line number Diff line number Diff line change @@ -101,5 +101,16 @@ public void ShouldMaintainNumericOperandParentheses()
101101
102102 translated . ShouldBe ( "((i - j) / k) + \" Maths!\" " ) ;
103103 }
104+
105+ // See https://github.com/agileobjects/ReadableExpressions/issues/43
106+ [ Fact ]
107+ public void ShouldChandleANullTerminatingCharacter ( )
108+ {
109+ var concat = CreateLambda ( ( string str1 , string str2 ) => str1 + '\0 ' + str2 ) ;
110+
111+ var translated = ToReadableString ( concat . Body ) ;
112+
113+ translated . ShouldBe ( "str1 + '\\ 0' + str2" ) ;
114+ }
104115 }
105116}
Original file line number Diff line number Diff line change @@ -74,8 +74,9 @@ private static bool TryTranslateFromTypeCode(
7474 return true ;
7575
7676 case NetStandardTypeCode . Char :
77- translation = new TranslationWrapper ( FixedValueTranslation ( constant ) ) . WrappedWith ( "'" , "'" ) ;
78-
77+ var character = ( char ) constant . Value ;
78+ var value = character == '\0 ' ? Expression . Constant ( @"\0" ) : constant ;
79+ translation = new TranslationWrapper ( FixedValueTranslation ( value ) ) . WrappedWith ( "'" , "'" ) ;
7980 return true ;
8081
8182 case NetStandardTypeCode . DateTime :
@@ -123,7 +124,7 @@ private static bool TryTranslateFromTypeCode(
123124 return true ;
124125
125126 case NetStandardTypeCode . String :
126- var stringValue = ( string ) constant . Value ;
127+ var stringValue = ( ( string ) constant . Value ) . Replace ( " \0 " , @"\0" ) ;
127128
128129 if ( stringValue . IsComment ( ) )
129130 {
You can’t perform that action at this time.
0 commit comments