Skip to content

Commit 989a8ec

Browse files
committed
Simplify logic in EscapeString.
1 parent ab35d34 commit 989a8ec

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/MySqlConnector/MySqlClient/MySqlHelper.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@ public static string EscapeString(string value)
2424
{
2525
if (sb == null)
2626
sb = new StringBuilder();
27-
if (i > last - 1)
28-
sb.Append(value, last + 1, i - last - 1);
27+
sb.Append(value, last + 1, i - (last + 1));
2928
sb.Append('\\');
3029
sb.Append(value[i]);
3130
last = i;
3231
}
3332
}
34-
if (sb != null && last < value.Length - 1)
35-
sb.Append(value, last + 1, value.Length - last - 1);
33+
if (sb != null)
34+
sb.Append(value, last + 1, value.Length - (last + 1));
3635

3736
return sb?.ToString() ?? value;
3837
}

tests/MySqlConnector.Tests/MySqlHelperTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ public class MySqlHelperTests
99
[InlineData("", "")]
1010
[InlineData("test", "test")]
1111
[InlineData("\"", "\\\"")]
12-
[InlineData("'", "\\'")]
13-
[InlineData("\\", "\\\\")]
12+
[InlineData(@"'", @"\'")]
13+
[InlineData(@"\", @"\\")]
14+
[InlineData(@"''", @"\'\'")]
1415
[InlineData(@"'begin", @"\'begin")]
1516
[InlineData(@"end'", @"end\'")]
1617
[InlineData(@"mid'dle", @"mid\'dle")]
18+
[InlineData(@"doub''led", @"doub\'\'led")]
1719
[InlineData(@"'a'b'", @"\'a\'b\'")]
1820
public void EscapeString(string input, string expected)
1921
{

0 commit comments

Comments
 (0)