﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SimpleCodepageDetector.cs" company="Microsoft Corporation">
//   Copyright (c) 2008, 2009, 2010 All Rights Reserved, Microsoft Corporation
//
//   This source is subject to the Microsoft Permissive License.
//   Please see the License.txt file for more information.
//   All other rights reserved.
//
//   THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
//   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//   PARTICULAR PURPOSE.
// </copyright>
// <summary>
//   Detects the codepages from a list of valid and safe listed codepages.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Exchange.Data.Globalization
{
    /// <summary>
    /// Detects codepages.
    /// </summary>
    internal struct SimpleCodepageDetector
    {
        /// <summary>
        /// Codepage mask
        /// </summary>
        private uint mask;

        /// <summary>
        /// Adds the specified character to the codepage mask
        /// </summary>
        /// <param name="ch">The character to add</param>
        public void AddData(char ch)
        {
#if !DETECTION_DOUBLE_INDEXING
            this.mask |= ~CodePageDetectData.CodePageMask[CodePageDetectData.Index[ch]];
#else
            ushort index0 = CodePageDetectData.index0[ch >> 4];
            this.mask |= ~CodePageDetectData.codePageMask[(index0 & 0x8000) != 0 ? (index0 & 0x00FF) : CodePageDetectData.index1[index0 + (ch & 0x000F)]];
#endif
        }

        /// <summary>
        /// Adds the specified count of characters from the specified buffer to the codepage mask, starting at the offset
        /// </summary>
        /// <param name="buffer">The data source</param>
        /// <param name="offset">The starting point within the source</param>
        /// <param name="count">The number of characters to add.</param>
        public void AddData(char[] buffer, int offset, int count)
        {
            count++;
            while (--count != 0)
            {
#if !DETECTION_DOUBLE_INDEXING
                this.mask |= ~CodePageDetectData.CodePageMask[CodePageDetectData.Index[buffer[offset]]];
#else
                char ch = buffer[offset];
                ushort index0 = CodePageDetectData.index0[ch >> 4];
                this.mask |= ~CodePageDetectData.codePageMask[(index0 & 0x8000) != 0 ? (index0 & 0x00FF) : CodePageDetectData.index1[index0 + (ch & 0x000F)]];
#endif
                offset++;
            }
        }

        /// <summary>
        /// Adds the specified count of characters from the specified buffer to the codepage mask, starting at the offset
        /// </summary>
        /// <param name="buffer">The data source</param>
        /// <param name="offset">The starting point within the source</param>
        /// <param name="count">The number of characters to add.</param>
        public void AddData(string buffer, int offset, int count)
        {
            count++;
            while (--count != 0)
            {
#if !DETECTION_DOUBLE_INDEXING
                this.mask |= ~CodePageDetectData.CodePageMask[CodePageDetectData.Index[buffer[offset]]];
#else
                char ch = buffer[offset];
                ushort index0 = CodePageDetectData.index0[ch >> 4];
                mask |= ~CodePageDetectData.codePageMask[(index0 & 0x8000) != 0 ? (index0 & 0x00FF) : CodePageDetectData.index1[index0 + (ch & 0x000F)]];
#endif
                offset++;
            }
        }

        /// <summary>
        /// Gets the code page based on the priority list and the specified options.
        /// </summary>
        /// <param name="codePagePriorityList">The code page priority list.</param>
        /// <param name="onlyValidCodePages">Only valid codepages are returned if true.</param>
        /// <returns>The code page number.</returns>
        public int GetCodePage(int[] codePagePriorityList, bool onlyValidCodePages)
        {
            uint cumulativeMask = ~this.mask;

            if (onlyValidCodePages)
            {
                cumulativeMask &= CodePageDetect.ValidCodePagesMask;
            }

            return CodePageDetect.GetCodePage(ref cumulativeMask, codePagePriorityList);
        }

        /// <summary>
        /// Gets the Windows code page based on the priority list and the specified options.
        /// </summary>
        /// <param name="codePagePriorityList">The code page priority list.</param>
        /// <param name="onlyValidCodePages">Only valid codepages are returned if true.</param>
        /// <returns>The code page number.</returns>
        public int GetWindowsCodePage(int[] codePagePriorityList, bool onlyValidCodePages)
        {
            uint cumulativeMask = ~this.mask;

            cumulativeMask &= CodePageDetectData.WindowsCodePagesMask;

            if (onlyValidCodePages)
            {
                cumulativeMask &= CodePageDetect.ValidCodePagesMask;
            }

            return cumulativeMask == 0 ? 1252 : CodePageDetect.GetCodePage(ref cumulativeMask, codePagePriorityList);
        }
    }
}
