Skip to content

Commit 5b657ce

Browse files
committed
ToAscii extension in unsafe content
1 parent 0f9f30f commit 5b657ce

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
using System.Text;
22

3-
namespace QuickViewFile.Extensions
3+
namespace System
44
{
55
public static class StringExtension
66
{
77
private static readonly ASCIIEncoding asciiEncoding = new ASCIIEncoding();
88

9-
public static string ToAscii(this string dirty)
9+
public unsafe static string ToAscii(this string dirty)
1010
{
11-
byte[] bytes = asciiEncoding.GetBytes(dirty);
12-
string clean = asciiEncoding.GetString(bytes);
13-
return clean;
11+
fixed (char* p = dirty)
12+
{
13+
for (int i = 0; i < dirty.Length; i++)
14+
{
15+
if (p[i] > 127) // If character is non-ASCII
16+
{
17+
p[i] = ' ';
18+
}
19+
}
20+
}
21+
return dirty;
1422
}
1523
}
1624
}

QuickViewFile/QuickViewFile.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<Version>1.6.0.0</Version>
1010
<ApplicationIcon>QuickViewFile.ico</ApplicationIcon>
1111
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
12+
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
1213
</PropertyGroup>
1314

1415
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

QuickViewFile/ViewModel/FilesListViewModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using QuickViewFile.Extensions;
2-
using QuickViewFile.Helpers;
1+
using QuickViewFile.Helpers;
32
using QuickViewFile.Models;
43
using System.Collections.ObjectModel;
54
using System.ComponentModel;

0 commit comments

Comments
 (0)