using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace ryCommon
{
    /// <summary>
    /// 字符串字数统计
    /// </summary>
    public static class StrCount
    {
        /// <summary>
        /// 返回字数统计信息
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static CountInfo GetLength(string s)
        {
            int l = 0;
            char[] q = s.ToCharArray();
            bool start_eng = false;
            bool start_num = false;
            var result = new CountInfo();
            for (int i = 0; i < q.Length; i++)
            {
                int qInt = (int)q[i];
                bool is_eng = false;
                bool is_num = false;
                if(qInt!= 160 && qInt != 127 && qInt>32)
                {
                    result.VisibleTotalCount++;
                }
                if (qInt >= 0x4E00 && qInt <= 0x9FA5) // 汉字
                {
                    l += 2;
                    result.TotalCount++;
                    result.HanziCount++;
                }
                else if ((qInt >= 97 && qInt <= 122) || (qInt >= 65 && qInt <= 90)) // 英文
                {
                    l += 1;
                    result.EngLetterCount++;
                    is_eng = true;
                    if (!start_eng){start_eng = true;}
                }
                else if ((qInt >= 48 && qInt <= 57)) // 数字
                {
                    l += 1;
                    result.NumCount++;
                    is_num = true;
                    if (!start_num) { start_num = true; }
                }
                else
                {
                    l += 1;
                    if (q[i] != ' ' && qInt!= 160 && qInt != 127 && qInt>31)
                    {
                        result.TotalCount++;
                    }
                    else
                    {

                    }
                    if(q[i]== ' ')
                    {

                    }
                }
                if(!is_eng)
                {
                    if (start_eng) { start_eng = false; result.EngWordCount++; result.TotalCount++; }
                }
                if(!is_num)
                {
                    if (start_num) { start_num = false; result.NumWordCount++; result.TotalCount++; }
                }
            }
            if (start_eng) { result.EngWordCount++; result.TotalCount++; }
            if (start_num) { result.NumWordCount++; result.TotalCount++; }
            result.TotalByteCount = l;
            return result;
        }
    }
    /// <summary>
    ///字符串统计信息
    /// </summary>
    public class CountInfo
    {
        /// <summary>
        /// 汉字数量
        /// </summary>
        public int HanziCount { get; set; } = 0;
        /// <summary>
        /// 英文字母数量
        /// </summary>
        public int EngLetterCount { get; set; } = 0;
        /// <summary>
        /// 英文单词数量
        /// </summary>
        public int EngWordCount { get; set; } = 0;
        /// <summary>
        /// 数字数量(一个数字字符算一个数)
        /// </summary>
        public int NumCount{ get; set; } = 0;
        /// <summary>
        /// 数字数量(一串数字算一个数)
        /// </summary>
        public int NumWordCount { get; set; } = 0;
        /// <summary>
        /// 字节总数(汉字算2个字节)
        /// </summary>
        public int TotalByteCount { get; set; } = 0;
        /// <summary>
        /// 总数(英文单词算一个数，一串数字算一个数)
        /// </summary>
        public int TotalCount { get; set; } = 0;
        /// <summary>
        /// 可见字符总数(一个字母算一个数,一个数字算一个数)
        /// </summary>
        public int VisibleTotalCount { get; set; } = 0;
    }
}
