Files
HCoreLib/Commons/EncodingUxer.cs
T
2026-06-01 12:28:44 +09:00

55 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KssSmaPlaLib.Commons
{
public class EncodingUxer
{
#region
public enum EncodingType
{
Utf8WithBom,
Utf8WithoutBom,
ShiftJIS
}
#endregion
public static Encoding GetEncoding(EncodingType type)
{
switch (type)
{
case EncodingType.Utf8WithBom:
return new UTF8Encoding(encoderShouldEmitUTF8Identifier: true); // BOMあり
case EncodingType.Utf8WithoutBom:
return new UTF8Encoding(encoderShouldEmitUTF8Identifier: false); // BOMなし
case EncodingType.ShiftJIS:
return Encoding.GetEncoding("shift_jis"); // ANSIWindows-932
default:
throw new ArgumentOutOfRangeException(nameof(type), "未対応のエンコーディングです");
}
}
public static EncodingType? GetEncodingType(string typeString)
{
typeString = typeString.ToLower();
if (StringUxer.WildcardMatch(typeString, "*utf*8*without*bom*"))
return EncodingType.Utf8WithoutBom;
if (StringUxer.WildcardMatch(typeString, "*utf*8*with*bom*"))
return EncodingType.Utf8WithBom;
if (StringUxer.WildcardMatch(typeString, "*utf*8*"))
return EncodingType.Utf8WithoutBom;
if (StringUxer.WildcardMatch(typeString, "*s*jis*"))
return EncodingType.ShiftJIS;
if (StringUxer.WildcardMatch(typeString, "*シ*jis*"))
return EncodingType.ShiftJIS;
return null;
}
}
}