public void Basics() { var trie = new ByteTrie <string>(); var strings = new[] { "HELLO", "HELLO WORLD", "HERBERT" }; foreach (var s in strings) { trie.Add(s, Encoding.UTF8.GetBytes(s)); } foreach (var s1 in strings) { Assert.Same(s1, trie.Find(Encoding.UTF8.GetBytes(s1))); } Assert.Null(trie.Find(Encoding.UTF8.GetBytes("Not Included"))); Assert.Null(trie.Find(Encoding.UTF8.GetBytes("HELL"))); Assert.Equal("HELLO", trie.Find(Encoding.UTF8.GetBytes("HELLO MUM"))); Assert.Equal("HELLO WORLD".Length, trie.MaxDepth); trie.SetDefaultValue("DEFAULT"); Assert.Equal("DEFAULT", trie.Find(Encoding.UTF8.GetBytes("Also Not Included"))); }
static FileTypeDetector() { _root = new ByteTrie<FileType>(); _root.SetDefaultValue(FileType.Unknown); // https://en.wikipedia.org/wiki/List_of_file_signatures _root.AddPath(FileType.Jpeg, new[] { (byte)0xff, (byte)0xd8 }); _root.AddPath(FileType.Tiff, Encoding.UTF8.GetBytes("II"), new byte[] { 0x2a, 0x00 }); _root.AddPath(FileType.Tiff, Encoding.UTF8.GetBytes("MM"), new byte[] { 0x00, 0x2a }); _root.AddPath(FileType.Psd, Encoding.UTF8.GetBytes("8BPS")); _root.AddPath(FileType.Png, new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52 }); _root.AddPath(FileType.Bmp, Encoding.UTF8.GetBytes("BM")); // TODO technically there are other very rare magic numbers for OS/2 BMP files... _root.AddPath(FileType.Gif, Encoding.UTF8.GetBytes("GIF87a")); _root.AddPath(FileType.Gif, Encoding.UTF8.GetBytes("GIF89a")); _root.AddPath(FileType.Ico, new byte[] { 0x00, 0x00, 0x01, 0x00 }); _root.AddPath(FileType.Pcx, new byte[] { 0x0A, 0x00, 0x01 }); // multiple PCX versions, explicitly listed _root.AddPath(FileType.Pcx, new byte[] { 0x0A, 0x02, 0x01 }); _root.AddPath(FileType.Pcx, new byte[] { 0x0A, 0x03, 0x01 }); _root.AddPath(FileType.Pcx, new byte[] { 0x0A, 0x05, 0x01 }); _root.AddPath(FileType.Riff, Encoding.UTF8.GetBytes("RIFF")); _root.AddPath(FileType.Arw, Encoding.UTF8.GetBytes("II"), new byte[] { 0x2a, 0x00, 0x08, 0x00 }); _root.AddPath(FileType.Crw, Encoding.UTF8.GetBytes("II"), new byte[] { 0x1a, 0x00, 0x00, 0x00 }, Encoding.UTF8.GetBytes("HEAPCCDR")); _root.AddPath(FileType.Cr2, Encoding.UTF8.GetBytes("II"), new byte[] { 0x2a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x43, 0x52 }); _root.AddPath(FileType.Nef, Encoding.UTF8.GetBytes("MM"), new byte[] { 0x00, 0x2a, 0x00, 0x00, 0x00, 0x80, 0x00 }); _root.AddPath(FileType.Orf, Encoding.UTF8.GetBytes("IIRO"), new byte[] { 0x08, 0x00 }); _root.AddPath(FileType.Orf, Encoding.UTF8.GetBytes("IIRS"), new byte[] { 0x08, 0x00 }); _root.AddPath(FileType.Raf, Encoding.UTF8.GetBytes("FUJIFILMCCD-RAW")); _root.AddPath(FileType.Rw2, Encoding.UTF8.GetBytes("II"), new byte[] { 0x55, 0x00 }); }
static FileTypeDetector() { _root = new ByteTrie <FileType?>(); _root.SetDefaultValue(FileType.Unknown); // https://en.wikipedia.org/wiki/List_of_file_signatures _root.AddPath(FileType.Jpeg, new sbyte[] { unchecked ((sbyte)0xff), unchecked ((sbyte)0xd8) }); _root.AddPath(FileType.Tiff, Sharpen.Runtime.GetBytesForString("II"), new sbyte[] { unchecked ((int)(0x2a)), unchecked ((int)(0x00)) }); _root.AddPath(FileType.Tiff, Sharpen.Runtime.GetBytesForString("MM"), new sbyte[] { unchecked ((int)(0x00)), unchecked ((int)(0x2a)) }); _root.AddPath(FileType.Psd, Sharpen.Runtime.GetBytesForString("8BPS")); _root.AddPath(FileType.Png, new sbyte[] { unchecked ((sbyte)0x89), unchecked ((int)(0x50)), unchecked ((int)(0x4E)), unchecked ((int)(0x47)), unchecked ((int)(0x0D)), unchecked ((int)(0x0A)), unchecked ((int)(0x1A)), unchecked ((int)(0x0A)), unchecked ( (int)(0x00)), unchecked ((int)(0x00)), unchecked ((int)(0x00)), unchecked ((int)(0x0D)), unchecked ((int)(0x49)), unchecked ((int)(0x48)), unchecked ((int)(0x44)), unchecked ((int)(0x52)) }); _root.AddPath(FileType.Bmp, Sharpen.Runtime.GetBytesForString("BM")); // TODO technically there are other very rare magic numbers for OS/2 BMP files... _root.AddPath(FileType.Gif, Sharpen.Runtime.GetBytesForString("GIF87a")); _root.AddPath(FileType.Gif, Sharpen.Runtime.GetBytesForString("GIF89a")); _root.AddPath(FileType.Ico, new sbyte[] { unchecked ((int)(0x00)), unchecked ((int)(0x00)), unchecked ((int)(0x01)), unchecked ((int)(0x00)) }); _root.AddPath(FileType.Pcx, new sbyte[] { unchecked ((int)(0x0A)), unchecked ((int)(0x00)), unchecked ((int)(0x01)) }); // multiple PCX versions, explicitly listed _root.AddPath(FileType.Pcx, new sbyte[] { unchecked ((int)(0x0A)), unchecked ((int)(0x02)), unchecked ((int)(0x01)) }); _root.AddPath(FileType.Pcx, new sbyte[] { unchecked ((int)(0x0A)), unchecked ((int)(0x03)), unchecked ((int)(0x01)) }); _root.AddPath(FileType.Pcx, new sbyte[] { unchecked ((int)(0x0A)), unchecked ((int)(0x05)), unchecked ((int)(0x01)) }); _root.AddPath(FileType.Riff, Sharpen.Runtime.GetBytesForString("RIFF")); _root.AddPath(FileType.Arw, Sharpen.Runtime.GetBytesForString("II"), new sbyte[] { unchecked ((int)(0x2a)), unchecked ((int)(0x00)), unchecked ((int)(0x08)), unchecked ((int)(0x00)) }); _root.AddPath(FileType.Crw, Sharpen.Runtime.GetBytesForString("II"), new sbyte[] { unchecked ((int)(0x1a)), unchecked ((int)(0x00)), unchecked ((int)(0x00)), unchecked ((int)(0x00)) }, Sharpen.Runtime.GetBytesForString("HEAPCCDR")); _root.AddPath(FileType.Cr2, Sharpen.Runtime.GetBytesForString("II"), new sbyte[] { unchecked ((int)(0x2a)), unchecked ((int)(0x00)), unchecked ((int)(0x10)), unchecked ((int)(0x00)), unchecked ((int)(0x00)), unchecked ((int)(0x00)), unchecked ((int )(0x43)), unchecked ((int)(0x52)) }); _root.AddPath(FileType.Nef, Sharpen.Runtime.GetBytesForString("MM"), new sbyte[] { unchecked ((int)(0x00)), unchecked ((int)(0x2a)), unchecked ((int)(0x00)), unchecked ((int)(0x00)), unchecked ((int)(0x00)), unchecked ((sbyte)0x80), unchecked ((int )(0x00)) }); _root.AddPath(FileType.Orf, Sharpen.Runtime.GetBytesForString("IIRO"), new sbyte[] { unchecked ((sbyte)0x08), unchecked ((int)(0x00)) }); _root.AddPath(FileType.Orf, Sharpen.Runtime.GetBytesForString("IIRS"), new sbyte[] { unchecked ((sbyte)0x08), unchecked ((int)(0x00)) }); _root.AddPath(FileType.Raf, Sharpen.Runtime.GetBytesForString("FUJIFILMCCD-RAW")); _root.AddPath(FileType.Rw2, Sharpen.Runtime.GetBytesForString("II"), new sbyte[] { unchecked ((int)(0x55)), unchecked ((int)(0x00)) }); }
static FileTypeDetector() { _root = new ByteTrie<FileType?>(); _root.SetDefaultValue(FileType.Unknown); // https://en.wikipedia.org/wiki/List_of_file_signatures _root.AddPath(FileType.Jpeg, new sbyte[] { unchecked((sbyte)0xff), unchecked((sbyte)0xd8) }); _root.AddPath(FileType.Tiff, Sharpen.Runtime.GetBytesForString("II"), new sbyte[] { unchecked((int)(0x2a)), unchecked((int)(0x00)) }); _root.AddPath(FileType.Tiff, Sharpen.Runtime.GetBytesForString("MM"), new sbyte[] { unchecked((int)(0x00)), unchecked((int)(0x2a)) }); _root.AddPath(FileType.Psd, Sharpen.Runtime.GetBytesForString("8BPS")); _root.AddPath(FileType.Png, new sbyte[] { unchecked((sbyte)0x89), unchecked((int)(0x50)), unchecked((int)(0x4E)), unchecked((int)(0x47)), unchecked((int)(0x0D)), unchecked((int)(0x0A)), unchecked((int)(0x1A)), unchecked((int)(0x0A)), unchecked( (int)(0x00)), unchecked((int)(0x00)), unchecked((int)(0x00)), unchecked((int)(0x0D)), unchecked((int)(0x49)), unchecked((int)(0x48)), unchecked((int)(0x44)), unchecked((int)(0x52)) }); _root.AddPath(FileType.Bmp, Sharpen.Runtime.GetBytesForString("BM")); // TODO technically there are other very rare magic numbers for OS/2 BMP files... _root.AddPath(FileType.Gif, Sharpen.Runtime.GetBytesForString("GIF87a")); _root.AddPath(FileType.Gif, Sharpen.Runtime.GetBytesForString("GIF89a")); _root.AddPath(FileType.Ico, new sbyte[] { unchecked((int)(0x00)), unchecked((int)(0x00)), unchecked((int)(0x01)), unchecked((int)(0x00)) }); _root.AddPath(FileType.Pcx, new sbyte[] { unchecked((int)(0x0A)), unchecked((int)(0x00)), unchecked((int)(0x01)) }); // multiple PCX versions, explicitly listed _root.AddPath(FileType.Pcx, new sbyte[] { unchecked((int)(0x0A)), unchecked((int)(0x02)), unchecked((int)(0x01)) }); _root.AddPath(FileType.Pcx, new sbyte[] { unchecked((int)(0x0A)), unchecked((int)(0x03)), unchecked((int)(0x01)) }); _root.AddPath(FileType.Pcx, new sbyte[] { unchecked((int)(0x0A)), unchecked((int)(0x05)), unchecked((int)(0x01)) }); _root.AddPath(FileType.Riff, Sharpen.Runtime.GetBytesForString("RIFF")); _root.AddPath(FileType.Arw, Sharpen.Runtime.GetBytesForString("II"), new sbyte[] { unchecked((int)(0x2a)), unchecked((int)(0x00)), unchecked((int)(0x08)), unchecked((int)(0x00)) }); _root.AddPath(FileType.Crw, Sharpen.Runtime.GetBytesForString("II"), new sbyte[] { unchecked((int)(0x1a)), unchecked((int)(0x00)), unchecked((int)(0x00)), unchecked((int)(0x00)) }, Sharpen.Runtime.GetBytesForString("HEAPCCDR")); _root.AddPath(FileType.Cr2, Sharpen.Runtime.GetBytesForString("II"), new sbyte[] { unchecked((int)(0x2a)), unchecked((int)(0x00)), unchecked((int)(0x10)), unchecked((int)(0x00)), unchecked((int)(0x00)), unchecked((int)(0x00)), unchecked((int )(0x43)), unchecked((int)(0x52)) }); _root.AddPath(FileType.Nef, Sharpen.Runtime.GetBytesForString("MM"), new sbyte[] { unchecked((int)(0x00)), unchecked((int)(0x2a)), unchecked((int)(0x00)), unchecked((int)(0x00)), unchecked((int)(0x00)), unchecked((sbyte)0x80), unchecked((int )(0x00)) }); _root.AddPath(FileType.Orf, Sharpen.Runtime.GetBytesForString("IIRO"), new sbyte[] { unchecked((sbyte)0x08), unchecked((int)(0x00)) }); _root.AddPath(FileType.Orf, Sharpen.Runtime.GetBytesForString("IIRS"), new sbyte[] { unchecked((sbyte)0x08), unchecked((int)(0x00)) }); _root.AddPath(FileType.Raf, Sharpen.Runtime.GetBytesForString("FUJIFILMCCD-RAW")); _root.AddPath(FileType.Rw2, Sharpen.Runtime.GetBytesForString("II"), new sbyte[] { unchecked((int)(0x55)), unchecked((int)(0x00)) }); }
public virtual void TestBasics() { ByteTrie<string> trie = new ByteTrie<string>(); string[] strings = new string[] { "HELLO", "HELLO WORLD", "HERBERT" }; foreach (string s in strings) { trie.AddPath(s, Sharpen.Runtime.GetBytesForString(s)); } foreach (string s_1 in strings) { NUnit.Framework.Assert.AreSame(s_1, trie.Find(Sharpen.Runtime.GetBytesForString(s_1))); } NUnit.Framework.Assert.IsNull(trie.Find(Sharpen.Runtime.GetBytesForString("Not Included"))); NUnit.Framework.Assert.IsNull(trie.Find(Sharpen.Runtime.GetBytesForString("HELL"))); Sharpen.Tests.AreEqual("HELLO", trie.Find(Sharpen.Runtime.GetBytesForString("HELLO MUM"))); Sharpen.Tests.AreEqual("HELLO WORLD".Length, trie.GetMaxDepth()); trie.SetDefaultValue("DEFAULT"); Sharpen.Tests.AreEqual("DEFAULT", trie.Find(Sharpen.Runtime.GetBytesForString("Also Not Included"))); }
public void TestBasics() { var trie = new ByteTrie<string>(); var strings = new[] { "HELLO", "HELLO WORLD", "HERBERT" }; foreach (var s in strings) { trie.AddPath(s, Encoding.UTF8.GetBytes(s)); } foreach (var s1 in strings) { Assert.Same(s1, trie.Find(Encoding.UTF8.GetBytes(s1))); } Assert.Null(trie.Find(Encoding.UTF8.GetBytes("Not Included"))); Assert.Null(trie.Find(Encoding.UTF8.GetBytes("HELL"))); Assert.Equal("HELLO", trie.Find(Encoding.UTF8.GetBytes("HELLO MUM"))); Assert.Equal("HELLO WORLD".Length, trie.MaxDepth); trie.SetDefaultValue("DEFAULT"); Assert.Equal("DEFAULT", trie.Find(Encoding.UTF8.GetBytes("Also Not Included"))); }