123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
- namespace SuperSocket.Common
- {
- /// <summary>
- /// Binary util class
- /// </summary>
- public static class BinaryUtil
- {
- /// <summary>
- /// Search target from source.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source">The source.</param>
- /// <param name="target">The target.</param>
- /// <param name="pos">The pos.</param>
- /// <param name="length">The length.</param>
- /// <returns></returns>
- public static int IndexOf<T>(this IList<T> source, T target, int pos, int length)
- where T : IEquatable<T>
- {
- for (int i = pos; i < pos + length; i++)
- {
- if (source[i].Equals(target))
- return i;
- }
- return -1;
- }
- /// <summary>
- /// Searches the mark from source.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source">The source.</param>
- /// <param name="mark">The mark.</param>
- /// <param name="parsedLength">Length of the parsed.</param>
- /// <returns></returns>
- public static int? SearchMark<T>(this IList<T> source, T[] mark, out int parsedLength)
- where T : IEquatable<T>
- {
- return SearchMark(source, 0, source.Count, mark, 0, out parsedLength);
- }
- /// <summary>
- /// Searches the mark from source.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source">The source.</param>
- /// <param name="mark">The mark.</param>
- /// <returns></returns>
- public static int? SearchMark<T>(this IList<T> source, T[] mark)
- where T : IEquatable<T>
- {
- int parsedLength;
- return SearchMark(source, 0, source.Count, mark, 0, out parsedLength);
- }
- /// <summary>
- /// Searches the mark from source.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source">The source.</param>
- /// <param name="offset">The offset.</param>
- /// <param name="length">The length.</param>
- /// <param name="mark">The mark.</param>
- /// <returns></returns>
- public static int? SearchMark<T>(this IList<T> source, int offset, int length, T[] mark)
- where T : IEquatable<T>
- {
- int parsedLength;
- return SearchMark(source, offset, length, mark, 0, out parsedLength);
- }
- /// <summary>
- /// Searches the mark from source.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source">The source.</param>
- /// <param name="offset">The offset.</param>
- /// <param name="length">The length.</param>
- /// <param name="mark">The mark.</param>
- /// <param name="parsedLength">Length of the parsed.</param>
- /// <returns></returns>
- public static int? SearchMark<T>(this IList<T> source, int offset, int length, T[] mark, out int parsedLength)
- where T : IEquatable<T>
- {
- return SearchMark(source, offset, length, mark, 0, out parsedLength);
- }
- /// <summary>
- /// Searches the mark from source.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source">The source.</param>
- /// <param name="offset">The offset.</param>
- /// <param name="length">The length.</param>
- /// <param name="mark">The mark.</param>
- /// <param name="matched">The matched.</param>
- /// <returns></returns>
- public static int? SearchMark<T>(this IList<T> source, int offset, int length, T[] mark, int matched)
- where T : IEquatable<T>
- {
- int parsedLength;
- return source.SearchMark(offset, length, mark, matched, out parsedLength);
- }
- /// <summary>
- /// Searches the mark from source.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source">The source.</param>
- /// <param name="offset">The offset.</param>
- /// <param name="length">The length.</param>
- /// <param name="mark">The mark.</param>
- /// <param name="matched">The matched.</param>
- /// <param name="parsedLength">Length of the parsed.</param>
- /// <returns></returns>
- public static int? SearchMark<T>(this IList<T> source, int offset, int length, T[] mark, int matched, out int parsedLength)
- where T : IEquatable<T>
- {
- int pos = offset;
- int endOffset = offset + length - 1;
- int matchCount = matched;
- parsedLength = 0;
- if (matched > 0)
- {
- for (int i = matchCount; i < mark.Length; i++)
- {
- if (!source[pos++].Equals(mark[i]))
- break;
- matchCount++;
- if (pos > endOffset)
- {
- if (matchCount == mark.Length)
- {
- parsedLength = mark.Length - matched;
- return offset;
- }
- else
- {
- return (0 - matchCount);
- }
- }
- }
- if (matchCount == mark.Length)
- {
- parsedLength = mark.Length - matched;
- return offset;
- }
- pos = offset;
- matchCount = 0;
- }
- while (true)
- {
- pos = source.IndexOf(mark[matchCount], pos, length - pos + offset);
- if (pos < 0)
- return null;
- matchCount += 1;
- for (int i = matchCount; i < mark.Length; i++)
- {
- int checkPos = pos + i;
- if (checkPos > endOffset)
- {
- //found end, return matched chars count
- return (0 - matchCount);
- }
- if (!source[checkPos].Equals(mark[i]))
- break;
- matchCount++;
- }
- //found the full end mark
- if (matchCount == mark.Length)
- {
- parsedLength = pos - offset + mark.Length;
- return pos;
- }
- //Reset next round read pos
- pos += 1;
- //clear matched chars count
- matchCount = 0;
- }
- }
- /// <summary>
- /// Searches the mark from source.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source">The source.</param>
- /// <param name="offset">The offset.</param>
- /// <param name="length">The length.</param>
- /// <param name="searchState">State of the search.</param>
- /// <param name="parsedLength">Length of the parsed.</param>
- /// <returns></returns>
- public static int SearchMark<T>(this IList<T> source, int offset, int length, SearchMarkState<T> searchState, out int parsedLength)
- where T : IEquatable<T>
- {
- int? result = source.SearchMark(offset, length, searchState.Mark, searchState.Matched, out parsedLength);
- if (!result.HasValue)
- {
- searchState.Matched = 0;
- return -1;
- }
- if (result.Value < 0)
- {
- searchState.Matched = 0 - result.Value;
- return -1;
- }
- searchState.Matched = 0;
- return result.Value;
- }
- /// <summary>
- /// Searches the mark from source.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source">The source.</param>
- /// <param name="offset">The offset.</param>
- /// <param name="length">The length.</param>
- /// <param name="searchState">State of the search.</param>
- /// <returns></returns>
- public static int SearchMark<T>(this IList<T> source, int offset, int length, SearchMarkState<T> searchState)
- where T : IEquatable<T>
- {
- var parsedLen = 0;
- return SearchMark(source, offset, length, searchState, out parsedLen);
- }
- /// <summary>
- /// Startses the with.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source">The source.</param>
- /// <param name="mark">The mark.</param>
- /// <returns></returns>
- public static int StartsWith<T>(this IList<T> source, T[] mark)
- where T : IEquatable<T>
- {
- return source.StartsWith(0, source.Count, mark);
- }
- /// <summary>
- /// Startses the with.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source">The source.</param>
- /// <param name="offset">The offset.</param>
- /// <param name="length">The length.</param>
- /// <param name="mark">The mark.</param>
- /// <returns></returns>
- public static int StartsWith<T>(this IList<T> source, int offset, int length, T[] mark)
- where T : IEquatable<T>
- {
- int pos = offset;
- int endOffset = offset + length - 1;
- for (int i = 0; i < mark.Length; i++)
- {
- int checkPos = pos + i;
- if (checkPos > endOffset)
- return i;
- if (!source[checkPos].Equals(mark[i]))
- return -1;
- }
- return mark.Length;
- }
- /// <summary>
- /// Endses the with.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source">The source.</param>
- /// <param name="mark">The mark.</param>
- /// <returns></returns>
- public static bool EndsWith<T>(this IList<T> source, T[] mark)
- where T : IEquatable<T>
- {
- return source.EndsWith(0, source.Count, mark);
- }
- /// <summary>
- /// Endses the with.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source">The source.</param>
- /// <param name="offset">The offset.</param>
- /// <param name="length">The length.</param>
- /// <param name="mark">The mark.</param>
- /// <returns></returns>
- public static bool EndsWith<T>(this IList<T> source, int offset, int length, T[] mark)
- where T : IEquatable<T>
- {
- if (mark.Length > length)
- return false;
- for (int i = 0; i < Math.Min(length, mark.Length); i++)
- {
- if (!mark[i].Equals(source[offset + length - mark.Length + i]))
- return false;
- }
- return true;
- }
- /// <summary>
- /// Clones the elements in the specific range.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source">The source.</param>
- /// <param name="offset">The offset.</param>
- /// <param name="length">The length.</param>
- /// <returns></returns>
- public static T[] CloneRange<T>(this IList<T> source, int offset, int length)
- {
- T[] target;
- var array = source as T[];
- if (array != null)
- {
- target = new T[length];
- Array.Copy(array, offset, target, 0, length);
- return target;
- }
- target = new T[length];
- for (int i = 0; i < length; i++)
- {
- target[i] = source[offset + i];
- }
- return target;
- }
- }
- }
|