using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace TZONE.GPSSocketServer.Common
{
    /// <summary>
    /// 高效 Split
    /// </summary>
    public class StringExtend
    {
        public static int DefaultResultLength = 8;

        public static IList<string> Split(byte[] data, Encoding coding, int start, int count, string splitdata)
        {
            return Split(coding.GetString(data, start, count), splitdata);
        }

        public static IList<string> Split(string value, string splitdata)
        {
            List<string> result = new List<string>(DefaultResultLength);
            int startIndex = 0;
            bool eq = false;
            int sindex = 0;
            int splitlength = splitdata.Length;
            int datalength = value.Length;
            while (sindex < value.Length)
            {
                eq = true;
                for (int k = 0; k < splitlength; k++)
                {
                    if (value[sindex + k] != splitdata[k])
                    {
                        eq = false;
                        break;
                    }

                }
                if (eq)
                {
                    sindex += splitlength;
                    if (sindex - splitlength > startIndex)
                        result.Add(value.Substring(startIndex, sindex - startIndex - splitlength));
                    startIndex = sindex;

                }
                else
                {
                    sindex++;
                }
            }
            if (startIndex < datalength)
            {
                if (sindex - splitlength > startIndex)
                    result.Add(value.Substring(startIndex, datalength - startIndex));
            }
            return result;
        }

        public static Hashtable GetProperties(string value, string linesplit, char propertysplit)
        {
            Hashtable result = new Hashtable(DefaultResultLength);
            int splitlength = linesplit.Length;
            int startIndex = 0;
            bool eq = false;
            int sindex = 0;
            int propertyindex = 0;
            while (sindex < value.Length)
            {
                eq = true;
                if (propertyindex == 0 && value[sindex] == propertysplit)
                    propertyindex = sindex;
                for (int k = 0; k < splitlength; k++)
                {
                    if (value[sindex + k] != linesplit[k])
                    {
                        eq = false;
                        break;
                    }
                }
                if (eq)
                {

                    if (sindex > startIndex)
                    {
                        result[value.Substring(startIndex, propertyindex - startIndex)] = value.Substring(propertyindex + 1, sindex - propertyindex);
                    }
                    sindex += splitlength;
                    propertyindex = 0;
                    startIndex = sindex;

                }
                else
                {
                    sindex++;
                }
            }
            if (startIndex < value.Length)
            {
                result[value.Substring(startIndex, propertyindex - startIndex)] = value.Substring(propertyindex + 1, sindex - propertyindex);
            }
            return result;
        }

        public static Hashtable GetProperties(byte[] data, Encoding coding, int start, int count, string linesplit, char propertysplit)
        {
            return GetProperties(coding.GetString(data, start, count), linesplit, propertysplit);
        }
    }
}
