Example #1
0
 /// <summary>
 /// Concats a string to this hash value.
 /// </summary>
 public StringHash32 Concat(StringSlice inSlice)
 {
     return(new StringHash32(inSlice.AppendHash32(m_HashValue)));
 }
Example #2
0
 public bool Equals(StringSlice other, bool inbIgnoreCase)
 {
     return(Equals(m_Source, m_StartIndex, Length, other.m_Source, other.m_StartIndex, other.Length, inbIgnoreCase));
 }
Example #3
0
 public StringHash32(StringSlice inSlice)
 {
     m_HashValue = inSlice.CalculateHash32();
 }
Example #4
0
        static private IEnumerable <StringSlice> EnumerableSplit(string inString, int inStartIdx, int inLength, ISplitter inSplitter, StringSplitOptions inSplitOptions)
        {
            if (inString == null)
            {
                yield break;
            }

            bool bRemoveEmpty = (inSplitOptions & StringSplitOptions.RemoveEmptyEntries) != 0;

            if (inSplitter == null)
            {
                if (!bRemoveEmpty || inLength > 0)
                {
                    yield return(new StringSlice(inString, inStartIdx, inLength));
                }
                yield break;
            }

            inSplitter.Reset();

            int startIdx      = inStartIdx;
            int currentLength = 0;

            for (int charIdx = 0; charIdx < inLength; ++charIdx)
            {
                int realIdx = inStartIdx + charIdx;

                int  evalAdvance;
                bool bSplit = inSplitter.Evaluate(inString, realIdx, out evalAdvance);

                charIdx       += evalAdvance;
                currentLength += evalAdvance;

                if (bSplit)
                {
                    if (!bRemoveEmpty || currentLength > 0)
                    {
                        StringSlice slice = new StringSlice(inString, startIdx, currentLength);
                        slice = inSplitter.Process(slice);
                        if (!bRemoveEmpty || slice.Length > 0)
                        {
                            yield return(slice);
                        }
                    }

                    startIdx      = realIdx + 1 + evalAdvance;
                    currentLength = 0;
                }
                else
                {
                    ++currentLength;
                }
            }

            if (currentLength > 0)
            {
                StringSlice slice = new StringSlice(inString, startIdx, currentLength);
                slice = inSplitter.Process(slice);
                if (!bRemoveEmpty || slice.Length > 0)
                {
                    yield return(slice);
                }
            }
        }
Example #5
0
 public bool Equals(StringSlice other)
 {
     return(Equals(m_Source, m_StartIndex, Length, other.m_Source, other.m_StartIndex, other.Length, false));
 }
Example #6
0
        static private int Split <T>(string inString, int inStartIdx, int inLength, char[] inSeparators, StringSplitOptions inSplitOptions, ref T outSlices) where T : ITempList <StringSlice>
        {
            if (inString == null)
            {
                return(0);
            }

            bool bRemoveEmpty = (inSplitOptions & StringSplitOptions.RemoveEmptyEntries) != 0;

            if (inSeparators == null || inSeparators.Length == 0)
            {
                if (!bRemoveEmpty || inLength > 0)
                {
                    outSlices.Add(new StringSlice(inString, inStartIdx, inLength));
                    return(1);
                }

                return(0);
            }

            int sepCount = inSeparators.Length;

            int startIdx      = inStartIdx;
            int currentLength = 0;
            int slices        = 0;

            for (int charIdx = 0; charIdx < inLength && slices < outSlices.Capacity; ++charIdx)
            {
                int  realIdx = inStartIdx + charIdx;
                char c       = inString[realIdx];
                bool bSplit  = false;
                for (int sepIdx = 0; !bSplit && sepIdx < sepCount; ++sepIdx)
                {
                    bSplit = c == inSeparators[sepIdx];
                }

                if (bSplit)
                {
                    if (!bRemoveEmpty || currentLength > 0)
                    {
                        StringSlice slice = new StringSlice(inString, startIdx, currentLength);
                        outSlices.Add(slice);
                        ++slices;
                    }

                    startIdx      = realIdx + 1;
                    currentLength = 0;
                }
                else
                {
                    ++currentLength;
                }
            }

            if (currentLength > 0 && slices < outSlices.Capacity)
            {
                StringSlice slice = new StringSlice(inString, startIdx, currentLength);
                outSlices.Add(slice);
                ++slices;
            }

            return(slices);
        }
Example #7
0
        static private int Split <T>(string inString, int inStartIdx, int inLength, ISplitter inSplitter, StringSplitOptions inSplitOptions, ref T outSlices) where T : ITempList <StringSlice>
        {
            if (inString == null)
            {
                return(0);
            }

            bool bRemoveEmpty = (inSplitOptions & StringSplitOptions.RemoveEmptyEntries) != 0;

            if (inSplitter == null)
            {
                if (!bRemoveEmpty || inLength > 0)
                {
                    outSlices.Add(new StringSlice(inString, inStartIdx, inLength));
                    return(1);
                }

                return(0);
            }

            inSplitter.Reset();

            int startIdx      = inStartIdx;
            int currentLength = 0;
            int slices        = 0;

            for (int charIdx = 0; charIdx < inLength && slices < outSlices.Capacity; ++charIdx)
            {
                int realIdx = inStartIdx + charIdx;

                int  evalAdvance;
                bool bSplit = inSplitter.Evaluate(inString, realIdx, out evalAdvance);

                charIdx       += evalAdvance;
                currentLength += evalAdvance;

                if (bSplit)
                {
                    if (!bRemoveEmpty || currentLength > 0)
                    {
                        StringSlice slice = new StringSlice(inString, startIdx, currentLength);
                        slice = inSplitter.Process(slice);
                        if (!bRemoveEmpty || slice.Length > 0)
                        {
                            outSlices.Add(slice);
                            ++slices;
                        }
                    }

                    startIdx      = realIdx + 1 + evalAdvance;
                    currentLength = 0;
                }
                else
                {
                    ++currentLength;
                }
            }

            if (currentLength > 0 && slices < outSlices.Capacity)
            {
                StringSlice slice = new StringSlice(inString, startIdx, currentLength);
                slice = inSplitter.Process(slice);
                if (!bRemoveEmpty || slice.Length > 0)
                {
                    outSlices.Add(slice);
                    ++slices;
                }
            }

            return(slices);
        }
Example #8
0
            /// <summary>
            /// Returns if the given string contains at least two comma-separated arguments.
            /// </summary>
            static public bool IsList(StringSlice inString)
            {
                bool quote = false;
                int  group = 0;

                char c;

                for (int i = 0; i < inString.Length; i++)
                {
                    c = inString[i];

                    switch (c)
                    {
                    case ',':
                        if (!quote && group <= 0)
                        {
                            return(true);
                        }
                        break;

                    case '(':
                    case '[':
                    case '<':
                    case '{':
                        if (!quote)
                        {
                            group++;
                        }
                        break;

                    case ')':
                    case ']':
                    case '>':
                    case '}':
                        if (!quote)
                        {
                            --group;
                        }
                        break;

                    case '"':
                        if (quote)
                        {
                            if (i > 0 && inString[i - 1] == '\\')
                            {
                                i++;
                            }
                            else
                            {
                                quote = !quote;
                            }
                        }
                        else
                        {
                            quote = true;
                        }
                        break;
                    }
                }
                return(false);
            }
Example #9
0
 public MethodCall(StringHash32 inId, StringSlice inArgs = default)
 {
     Id   = inId;
     Args = inArgs;
 }
Example #10
0
        static private bool TryParseLongInternal(StringSlice inSlice, bool inbCheckHex, out long outLong)
        {
            inSlice = inSlice.Trim();

            if (inbCheckHex && inSlice.StartsWith("0x"))
            {
                ulong hex;
                if (TryParseHex(inSlice.Substring(2), 16, out hex))
                {
                    outLong = (long)hex;
                    return(true);
                }

                outLong = 0;
                return(false);
            }

            if (inSlice.Length == 0 || TooLong(inSlice, MaxDigits64))
            {
                outLong = 0;
                return(false);
            }

            decimal accum = 0;
            char    c;
            int     sign = 1;

            for (int i = 0; i < inSlice.Length; ++i)
            {
                c = inSlice[i];

                if (c == Negative)
                {
                    if (i > 0)
                    {
                        outLong = 0;
                        return(false);
                    }

                    sign = -1;
                    continue;
                }
                else if (c == Positive)
                {
                    if (i > 0)
                    {
                        outLong = 0;
                        return(false);
                    }

                    continue;
                }

                if (!IsDigit(c))
                {
                    outLong = 0;
                    return(false);
                }

                accum = (accum * 10) + (c - '0');
            }

            accum *= sign;

            if (accum > long.MaxValue || accum < long.MinValue)
            {
                outLong = 0;
                return(false);
            }

            outLong = (long)accum;
            return(true);
        }
Example #11
0
        /// <summary>
        /// Attempts to parse a string slice into a byte.
        /// </summary>
        static public bool TryParseByte(StringSlice inSlice, out byte outByte)
        {
            inSlice = inSlice.Trim();

            if (inSlice.StartsWith("0x"))
            {
                ulong hex;
                if (TryParseHex(inSlice.Substring(2), 2, out hex))
                {
                    outByte = (byte)hex;
                    return(true);
                }

                outByte = 0;
                return(false);
            }

            if (inSlice.Length == 0 || TooLong(inSlice, MaxDigits8))
            {
                outByte = 0;
                return(false);
            }

            int  accum = 0;
            char c;

            for (int i = 0; i < inSlice.Length; ++i)
            {
                c = inSlice[i];

                if (c == Negative)
                {
                    outByte = 0;
                    return(false);
                }
                if (c == Positive)
                {
                    if (i > 0)
                    {
                        outByte = 0;
                        return(false);
                    }

                    continue;
                }

                if (!IsDigit(c))
                {
                    outByte = 0;
                    return(false);
                }

                accum = (accum * 10) + (c - '0');
            }

            if (accum > byte.MaxValue)
            {
                outByte = 0;
                return(false);
            }

            outByte = (byte)accum;
            return(true);
        }
Example #12
0
 /// <summary>
 /// Attempts to parse a string slice into a long.
 /// </summary>
 static public bool TryParseLong(StringSlice inSlice, out long outLong)
 {
     return(TryParseLongInternal(inSlice, true, out outLong));
 }
Example #13
0
        /// <summary>
        /// Attempts to parse a string slice into an int.
        /// </summary>
        static public bool TryParseInt(StringSlice inSlice, out int outInt)
        {
            inSlice = inSlice.Trim();

            if (inSlice.StartsWith("0x"))
            {
                ulong hex;
                if (TryParseHex(inSlice.Substring(2), 8, out hex))
                {
                    outInt = (int)hex;
                    return(true);
                }

                outInt = 0;
                return(false);
            }

            if (inSlice.Length == 0 || TooLong(inSlice, MaxDigits32))
            {
                outInt = 0;
                return(false);
            }

            long accum = 0;
            char c;
            int  sign = 1;

            for (int i = 0; i < inSlice.Length; ++i)
            {
                c = inSlice[i];

                if (c == Negative)
                {
                    if (i > 0)
                    {
                        outInt = 0;
                        return(false);
                    }

                    sign = -1;
                    continue;
                }
                else if (c == Positive)
                {
                    if (i > 0)
                    {
                        outInt = 0;
                        return(false);
                    }

                    continue;
                }

                if (!IsDigit(c))
                {
                    outInt = 0;
                    return(false);
                }

                accum = (accum * 10) + (c - '0');
            }

            accum *= sign;

            if (accum > int.MaxValue || accum < int.MinValue)
            {
                outInt = 0;
                return(false);
            }

            outInt = (int)accum;
            return(true);
        }
Example #14
0
        static private int EvaluateFloatMode(StringSlice inSlice)
        {
            if (inSlice.StartsWith("0x"))
            {
                return(ReadAsInteger);
            }

            bool bHasDot = false;
            bool bHasE   = false;

            for (int i = 0; i < inSlice.Length; ++i)
            {
                char c = inSlice[i];
                if (c == '.')
                {
                    if (bHasDot)
                    {
                        return(DoNotRead);
                    }

                    bHasDot = true;
                }
                else if (c == 'e' || c == 'E')
                {
                    if (bHasE)
                    {
                        return(DoNotRead);
                    }

                    bHasE = true;
                }
                else
                {
                    if (c == Positive || c == Negative)
                    {
                        if (i > 0)
                        {
                            return(DoNotRead);
                        }

                        continue;
                    }

                    if (!IsDigit(c))
                    {
                        return(DoNotRead);
                    }
                }

                if (bHasDot && bHasE)
                {
                    break;
                }
            }

            if (bHasE)
            {
                return(ReadAsSystemFloat);
            }

            if (TooLong(inSlice, MaxDigits32))
            {
                return(ReadAsSystemFloat);
            }

            return(bHasDot ? ReadAsDecimalPlace : ReadAsInteger);
        }
Example #15
0
        static private bool TryParseLongDouble(StringSlice inSlice, out double outDouble)
        {
            decimal accum = 0;
            char    c;
            int     sign          = 1;
            int     decimalPoints = 0;

            for (int i = 0; i < inSlice.Length; ++i)
            {
                c = inSlice[i];

                if (c == Negative)
                {
                    if (i > 0)
                    {
                        outDouble = 0;
                        return(false);
                    }

                    sign = -1;
                    continue;
                }
                else if (c == Positive)
                {
                    if (i > 0)
                    {
                        outDouble = 0;
                        return(false);
                    }

                    continue;
                }

                if (c == Dot)
                {
                    if (decimalPoints == 0)
                    {
                        decimalPoints = inSlice.Length - 1 - i;
                        continue;
                    }

                    outDouble = 0;
                    return(false);
                }

                if (!IsDigit(c))
                {
                    outDouble = 0;
                    return(false);
                }

                accum = (accum * 10) + (c - '0');
            }

            while (--decimalPoints >= 0)
            {
                accum /= 10;
            }

            outDouble = (double)accum * sign;
            return(true);
        }
Example #16
0
        /// <summary>
        /// Attempts to convert a string slice into a value of the given type.
        /// </summary>
        static public bool TryConvertTo(StringSlice inSlice, Type inType, out object outValue)
        {
            if (inType.IsEnum)
            {
                try
                {
                    outValue = Enum.Parse(inType, inSlice.ToString(), false);
                    return(true);
                }
                catch
                {
                    outValue = null;
                    return(false);
                }
            }

            TypeCode tc = Type.GetTypeCode(inType);

            switch (tc)
            {
            case TypeCode.Boolean:
            {
                bool b;
                if (TryParseBool(inSlice, out b))
                {
                    outValue = b;
                    return(true);
                }

                break;
            }

            case TypeCode.Byte:
            {
                byte b;
                if (TryParseByte(inSlice, out b))
                {
                    outValue = b;
                    return(true);
                }

                break;
            }

            case TypeCode.Char:
            {
                if (inSlice.Length > 0)
                {
                    outValue = inSlice[0];
                    return(true);
                }

                break;
            }

            case TypeCode.DateTime:
            {
                try
                {
                    outValue = Convert.ToDateTime(inSlice.ToString());
                    return(true);
                }
                catch
                {
                    outValue = null;
                    return(false);
                }
            }

            case TypeCode.Decimal:
            {
                double d;
                if (TryParseDouble(inSlice, out d))
                {
                    outValue = (decimal)d;
                    return(true);
                }

                break;
            }

            case TypeCode.Double:
            {
                double d;
                if (TryParseDouble(inSlice, out d))
                {
                    outValue = d;
                    return(true);
                }

                break;
            }

            case TypeCode.Int16:
            {
                short s;
                if (TryParseShort(inSlice, out s))
                {
                    outValue = s;
                    return(true);
                }

                break;
            }

            case TypeCode.Int32:
            {
                int i;
                if (TryParseInt(inSlice, out i))
                {
                    outValue = i;
                    return(true);
                }

                break;
            }

            case TypeCode.Int64:
            {
                long l;
                if (TryParseLong(inSlice, out l))
                {
                    outValue = l;
                    return(true);
                }

                break;
            }

            case TypeCode.Object:
            {
                if (inType == typeof(StringSlice) || inType == typeof(object))
                {
                    outValue = inSlice;
                    return(true);
                }
                if (inType == typeof(StringHash32))
                {
                    StringHash32 hash;
                    if (StringHash32.TryParse(inSlice, out hash))
                    {
                        outValue = hash;
                        return(true);
                    }
                }
                if (inType == typeof(StringHash64))
                {
                    StringHash64 hash;
                    if (StringHash64.TryParse(inSlice, out hash))
                    {
                        outValue = hash;
                        return(true);
                    }
                }
                else if (inType == typeof(Variant))
                {
                    Variant v;
                    if (Variant.TryParse(inSlice, true, out v))
                    {
                        outValue = v;
                        return(true);
                    }
                }

                break;
            }

            case TypeCode.SByte:
            {
                sbyte s;
                if (TryParseSByte(inSlice, out s))
                {
                    outValue = s;
                    return(true);
                }

                break;
            }

            case TypeCode.Single:
            {
                float f;
                if (TryParseFloat(inSlice, out f))
                {
                    outValue = f;
                    return(true);
                }

                break;
            }

            case TypeCode.String:
            {
                outValue = inSlice.ToString();
                return(true);
            }

            case TypeCode.UInt16:
            {
                ushort u;
                if (TryParseUShort(inSlice, out u))
                {
                    outValue = u;
                    return(true);
                }

                break;
            }

            case TypeCode.UInt32:
            {
                uint u;
                if (TryParseUInt(inSlice, out u))
                {
                    outValue = u;
                    return(true);
                }

                break;
            }

            case TypeCode.UInt64:
            {
                ulong u;
                if (TryParseULong(inSlice, out u))
                {
                    outValue = u;
                    return(true);
                }

                break;
            }
            }

            outValue = null;
            return(false);
        }