-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
230 additions
and
6 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/BD.Common/Collections/Generic/Utf8StringComparerOrdinalIgnoreCase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace System.Collections.Generic; | ||
|
||
/// <summary> | ||
/// 适用于 UTF8String 忽略大小写的字符串比较 | ||
/// </summary> | ||
public sealed class Utf8StringComparerOrdinalIgnoreCase : IEqualityComparer<byte> | ||
{ | ||
public Utf8StringComparerOrdinalIgnoreCase() { } | ||
|
||
// https://www.geeksforgeeks.org/lower-case-upper-case-interesting-fact/ | ||
|
||
static byte Convert(byte b) | ||
{ | ||
int i = b; | ||
i &= ~32; | ||
return (byte)i; | ||
} | ||
|
||
bool IEqualityComparer<byte>.Equals(byte x, byte y) | ||
=> Convert(x) == Convert(y); | ||
|
||
int IEqualityComparer<byte>.GetHashCode(byte obj) | ||
=> EqualityComparer<byte>.Default.GetHashCode(Convert(obj)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/BD.Common/IO/FileFormats/FileFormat.AnalyzeFileType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
namespace System.IO.FileFormats; | ||
|
||
partial class FileFormat | ||
{ | ||
public readonly record struct AnalyzeFileTypeResult | ||
{ | ||
AnalyzeFileTypeResult(string fileEx) | ||
{ | ||
FileEx = fileEx; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator AnalyzeFileTypeResult(string fileEx) | ||
=> new(fileEx); | ||
|
||
AnalyzeFileTypeResult(ImageFormat imageFormat) | ||
{ | ||
ImageFormat = imageFormat; | ||
FileEx = imageFormat.GetExtension(); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator AnalyzeFileTypeResult(ImageFormat imageFormat) | ||
=> new(imageFormat); | ||
|
||
public string FileEx { get; init; } | ||
|
||
public ImageFormat? ImageFormat { get; init; } | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static AnalyzeFileTypeResult AnalyzeFileType(ReadOnlyMemory<byte> buffer) | ||
{ | ||
if (IsImage(buffer, out var imageFormat)) | ||
{ | ||
return imageFormat; | ||
} | ||
|
||
if (IsSQLite3(buffer)) | ||
{ | ||
return FileEx.SQLite; | ||
} | ||
|
||
Utf8StringComparerOrdinalIgnoreCase comparer = new(); | ||
// 根据文件头识别一些文件类型使用正确的文件扩展名 | ||
var magicNumber = "<html"u8; | ||
if (magicNumber.SequenceEqual(buffer.Span[..magicNumber.Length], comparer)) | ||
{ | ||
return FileEx.HTML; | ||
} | ||
magicNumber = "<body"u8; | ||
if (magicNumber.SequenceEqual(buffer.Span[..magicNumber.Length], comparer)) | ||
{ | ||
return FileEx.HTML; | ||
} | ||
magicNumber = "<!DOCTYPE"u8; | ||
if (magicNumber.SequenceEqual(buffer.Span[..magicNumber.Length], comparer)) | ||
{ | ||
return FileEx.HTML; | ||
} | ||
magicNumber = "<?xml"u8; | ||
if (magicNumber.SequenceEqual(buffer.Span[..magicNumber.Length], comparer)) | ||
{ | ||
return FileEx.XML; | ||
} | ||
magicNumber = "{"u8; | ||
if (magicNumber.SequenceEqual(buffer.Span[..magicNumber.Length], comparer)) | ||
{ | ||
return FileEx.JSON; | ||
} | ||
|
||
return FileEx.BIN; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters