チェンジセット 1690 (default)


以下の違いを無視:
日時:
2024/09/19 10:00:04 (2週前)
更新者:
hizuya@…
ログメッセージ:
  • Unicode の BOM を判定するメソッドを追加。
場所:
framework/trunk
ファイル:
2個の更新

凡例:

未変更
追加
削除
  • framework/trunk/CoreLibrary/Sources/IO/IOUtility.cs

    r1657 r1690  
    14201420 
    14211421        /// <summary> 
     1422        /// バイト配列の先頭より BOM を読み取って Unicode エンコーディングを判定します。 
     1423        /// </summary> 
     1424        /// <param name="bytes"> 
     1425        /// バイト配列。 
     1426        /// </param> 
     1427        /// <param name="offset"> 
     1428        /// <paramref name="bytes"/> の開始オフセット。 
     1429        /// </param> 
     1430        /// <param name="length"> 
     1431        /// <paramref name="bytes"/> の <paramref name="offset"/> からの有効な長さ。 
     1432        /// </param> 
     1433        /// <returns> 
     1434        /// 有効な BOM が有った場合は、そのエンコーディング。 
     1435        /// それ以外の場合は <see langword="null"/> 参照です。 
     1436        /// </returns> 
     1437        public static Encoding DetectUnicodeEncodingByBom(byte[] bytes, int offset, int length) 
     1438        { 
     1439            return DetectEncodingByBom(bytes, offset, length); 
     1440        } 
     1441 
     1442        /// <summary> 
    14221443        /// 指定したストリームを、破棄時に発生する例外を無視するストリームでラップします。 
    14231444        /// </summary> 
  • framework/trunk/CoreTest/Sources/IO/IOUtilityTest.cs

    r1657 r1690  
    3131    using System.Text; 
    3232    using FCSoft.SilverFrost.Framework.Security; 
     33    using FCSoft.SilverFrost.Framework.Text; 
    3334    using FCSoft.SilverFrost.Framework.UnitTest; 
    3435    using NUnit.Framework; 
     
    679680 
    680681        /// <summary> 
     682        /// <see cref="IOUtility.DetectUnicodeEncodingByBom"/> をテストします。 
     683        /// </summary> 
     684        /// <param name="data"> 
     685        /// バイト配列を表す 16 進数文字列。 
     686        /// </param> 
     687        /// <param name="offset"> 
     688        /// <paramref name="data"/> をバイト配列したときの開始オフセット。 
     689        /// </param> 
     690        /// <param name="length"> 
     691        /// <paramref name="data"/> をバイト配列したときの <paramref name="offset"/> からの有効な長さ。 
     692        /// </param> 
     693        /// <returns> 
     694        /// <see cref="IOUtility.DetectUnicodeEncodingByBom"/> の戻り値の。 
     695        /// <see cref="Encoding.WebName"/>。 
     696        /// </returns> 
     697        [Test] 
     698        [TestCase("", 0, 0, ExpectedResult = null)] 
     699        [TestCase("00", 0, 1, ExpectedResult = null)] 
     700        [TestCase("00000000", 2, 0, ExpectedResult = null)] 
     701        [TestCase("00000000", 0, 4, ExpectedResult = null)] 
     702        [TestCase("00000000", 1, 2, ExpectedResult = null)] 
     703        //// BOM だけ 
     704        [TestCase("EFBBBF", 0, 3, ExpectedResult = "utf-8")] 
     705        [TestCase("FEFF", 0, 2, ExpectedResult = "utf-16BE")] 
     706        [TestCase("FFFE", 0, 2, ExpectedResult = "utf-16")] 
     707        [TestCase("0000FEFF", 0, 4, ExpectedResult = "utf-32BE")] 
     708        [TestCase("FFFE0000", 0, 4, ExpectedResult = "utf-32")] 
     709        //// BOM の前後に別の値 
     710        [TestCase("99EFBBBF99", 1, 3, ExpectedResult = "utf-8")] 
     711        [TestCase("99FEFF99", 1, 2, ExpectedResult = "utf-16BE")] 
     712        [TestCase("99FFFE99", 1, 2, ExpectedResult = "utf-16")] 
     713        [TestCase("990000FEFF99", 1, 4, ExpectedResult = "utf-32BE")] 
     714        [TestCase("99FFFE000099", 1, 4, ExpectedResult = "utf-32")] 
     715        public string TestDetectUnicodeEncodingByBom(string data, int offset, int length) 
     716        { 
     717            byte[] bytes = TextUtility.ConvertHexToBinary(data); 
     718            Encoding encoding = IOUtility.DetectUnicodeEncodingByBom(bytes, offset, length); 
     719            return encoding != null ? encoding.WebName : null; 
     720        } 
     721 
     722        /// <summary> 
    681723        /// <see cref="IOUtility.CreateCloseExceptionIgnorableStream"/> をテストします。 
    682724        /// </summary> 
詳しい使い方は TracChangeset を参照してください。