Browse Source

fix detection of UTF-32 and add Unit Tests

pull/6/merge
Siegfried Pammer 14 years ago
parent
commit
cf5f9a1836
  1. 14
      src/Main/Base/Project/Src/Services/MimeTypeDetection.cs
  2. 1
      src/Main/Base/Test/ICSharpCode.SharpDevelop.Tests.csproj
  3. 34
      src/Main/Base/Test/MimeDetectionTests.cs

14
src/Main/Base/Project/Src/Services/MimeTypeDetection.cs

@ -29,7 +29,9 @@ namespace ICSharpCode.SharpDevelop @@ -29,7 +29,9 @@ namespace ICSharpCode.SharpDevelop
// UTF-16 Big Endian
(buffer.Length >= 2 && buffer[0] == 0xFE && buffer[1] == 0xFF) ||
// UTF-16 Little Endian
(buffer.Length >= 2 && buffer[0] == 0xFF && buffer[1] == 0xFE))
(buffer.Length >= 2 && buffer[0] == 0xFF && buffer[1] == 0xFE) ||
// UTF-32 Big Endian
(buffer.Length >= 4 && buffer[0] == 0x00 && buffer[1] == 0x00 && buffer[2] == 0xFE && buffer[3] == 0xFF))
return "text/plain";
fixed (byte *b = &buffer[offset]) {
@ -60,15 +62,5 @@ namespace ICSharpCode.SharpDevelop @@ -60,15 +62,5 @@ namespace ICSharpCode.SharpDevelop
stream.Position = 0;
return FindMimeType(buffer, 0, stream.Read(buffer, 0, buffer.Length));
}
public static string FindMimeType(ITextBuffer buffer)
{
if (buffer == null)
throw new ArgumentNullException("buffer");
// TODO USE PROPER ENCODING!
// Maybe use Encoding detection from AvalonEdit?
byte[] bytes = Encoding.Default.GetBytes(buffer.TextLength > BUFFER_SIZE ? buffer.GetText(0, BUFFER_SIZE) : buffer.Text);
return FindMimeType(bytes, 0, bytes.Length);
}
}
}

1
src/Main/Base/Test/ICSharpCode.SharpDevelop.Tests.csproj

@ -81,6 +81,7 @@ @@ -81,6 +81,7 @@
<Compile Include="GetElementByReflectionNameTests.cs" />
<Compile Include="Highlighting\AddInHighlightingResourceTests.cs" />
<Compile Include="Highlighting\SyntaxDoozerAddsHighlightingToHighlightingManagerTestFixture.cs" />
<Compile Include="MimeDetectionTests.cs" />
<Compile Include="NRefactoryResolverTests.cs" />
<Compile Include="CollectionClassOverridesTestFixture.cs" />
<Compile Include="OutputTextLineParserTests.cs" />

34
src/Main/Base/Test/MimeDetectionTests.cs

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using NUnit.Framework;
namespace ICSharpCode.SharpDevelop.Tests
{
[TestFixture]
public class MimeTypeDetectionTests
{
[Test]
public void TextPlain()
{
// always open empty files with text editor
TestMime(new byte[] {}, "text/plain");
// UTF-8
TestMime(new byte[] { 0xEF, 0xBB, 0xBF }, "text/plain");
// UTF-16 Big Endian
TestMime(new byte[] { 0xFE, 0xFF }, "text/plain");
// UTF-16 Little Endian
TestMime(new byte[] { 0xFF, 0xFE }, "text/plain");
// UTF-32 Big Endian
TestMime(new byte[] { 0x00, 0x00, 0xFE, 0xFF }, "text/plain");
// UTF-32 Little Endian
TestMime(new byte[] { 0xFF, 0xFE, 0x00, 0x00 }, "text/plain");
}
void TestMime(byte[] bytes, string expectedMime)
{
Assert.AreEqual(expectedMime, MimeTypeDetection.FindMimeType(bytes));
}
}
}
Loading…
Cancel
Save