Ejemplo n.º 1
0
 public TypeCoding GetTypeCoding(PatternMatch patternMatch)
 {
     if (!typeCharCoding.ContainsKey(patternMatch.type))
     {
         return(null);
     }
     return(new TypeCoding(typeCharCoding[patternMatch.type]));
 }
Ejemplo n.º 2
0
 public CodingInfo GetNewTypeInfo(PatternMatch patternMatch)
 {
     if (currentTypeCodingByte == 255)
     {
         return(null);
     }
     if (!typeCharCoding.ContainsKey(patternMatch.type))
     {
         typeCharCoding[patternMatch.type] = currentTypeCodingByte;
         var codingInfoToReturn = new CodingInfo(patternMatch.type, currentTypeCodingByte);
         currentTypeCodingByte++;
         return(codingInfoToReturn);
     }
     return(null);
 }
Ejemplo n.º 3
0
        private void HandleString(string currentString)
        {
            if (currentString.Length > MaxStringLength || currentString.Length < MinStringLength)
            {
                CodeStringRaw(currentString);
                return;
            }
            var match = FormatRegex.Match(currentString);

            if (match.Success)
            {
                if (TestMatch(match))
                {
                    var patternMatch = new PatternMatch(match);

                    var typeCoding = patternHandler.GetTypeCoding(patternMatch);
                    if (typeCoding == null)
                    {
                        var typeInfo = patternHandler.GetNewTypeInfo(patternMatch);
                        if (typeInfo == null)
                        {
                            CodeStringRaw(currentString);
                            return;
                        }
                        CodeTypeInfo(typeInfo);
                        typeCoding = patternHandler.GetTypeCoding(patternMatch);
                    }

                    var patternCoding = patternHandler.GetPatternCoding(patternMatch);
                    if (patternCoding == null)
                    {
                        var patternInfo = patternHandler.GetNewPatternInfo(patternMatch);
                        if (patternInfo == null)
                        {
                            CodeStringRaw(currentString);
                            return;
                        }
                        CodePatternInfo(patternInfo);
                        patternCoding = patternHandler.GetPatternCoding(patternMatch);
                    }

                    CodePatternString(patternCoding, typeCoding, patternMatch);
                    return;
                }
            }
            CodeStringRaw(currentString);
        }
Ejemplo n.º 4
0
        public PatternCoding GetPatternCoding(PatternMatch patternMatch)
        {
            if (!typePatternDictionaryMapping.ContainsKey(patternMatch.type))
            {
                return(null);
            }
            var strSplit = patternMatch.str.Split(' ');

            if (!typePatternDictionaryMapping[patternMatch.type].ContainsKey(strSplit.Length))
            {
                return(null);
            }
            foreach (var pattern in typePatternDictionaryMapping[patternMatch.type][strSplit.Length])
            {
                var arguments   = new List <string>();
                var needPattern = true;
                for (int i = 0; i < strSplit.Length; i++)
                {
                    if (pattern.strSplit[i] == ArgumentMark)
                    {
                        arguments.Add(strSplit[i]);
                    }
                    else
                    {
                        needPattern &= pattern.strSplit[i] == strSplit[i];
                        if (!needPattern)
                        {
                            break;
                        }
                    }
                }
                if (needPattern)
                {
                    return(new PatternCoding(arguments, pattern.coding));
                }
            }
            return(null);
        }
Ejemplo n.º 5
0
        private void CodePatternString(PatternCoding patternCoding, TypeCoding typeCoding, PatternMatch patternMatch)
        {
            uint length = 8 + 8 + 1 + 1 + 2 * (uint)patternCoding.arguments.Count;

            for (int i = 0; i < patternCoding.arguments.Count; i++)
            {
                length += (uint)NeedEncoding.GetByteCount(patternCoding.arguments[i]);
            }
            length <<= 2;
            length  |= (uint)TypeBits.PatternString;

            byte[] lenBytes = BitConverter.GetBytes(length);
            compactFileStream.Write(lenBytes);

            var difference = patternMatch.date.ToBinary() - lastDateBinary;

            byte[] dateBytes = BitConverter.GetBytes(difference);
            compactFileStream.Write(dateBytes);
            lastDateBinary = patternMatch.date.ToBinary();

            byte[] numBytes = BitConverter.GetBytes(patternMatch.number - lastNumber);
            compactFileStream.Write(numBytes);
            lastNumber = patternMatch.number;

            compactFileStream.WriteByte(typeCoding.coding);

            compactFileStream.WriteByte(patternCoding.coding);

            for (int i = 0; i < patternCoding.arguments.Count; i++)
            {
                ushort bytesCount  = (ushort)NeedEncoding.GetByteCount(patternCoding.arguments[i]);
                byte[] argLenBytes = BitConverter.GetBytes(bytesCount);
                compactFileStream.Write(argLenBytes);
                compactFileStream.Write(NeedEncoding.GetBytes(patternCoding.arguments[i]));
            }
        }
Ejemplo n.º 6
0
        public CodingInfo GetNewPatternInfo(PatternMatch patternMatch)
        {
            if (currentPatternCodingByte == 255)
            {
                return(null);
            }
            var strSplit = patternMatch.str.Split(' ');

            if (strSplit.Length == 1)
            {
                return(null);
            }
            if (!typeCandidateDictionaryMapping.ContainsKey(patternMatch.type))
            {
                typeCandidateDictionaryMapping[patternMatch.type] = new Dictionary <int, List <PatternCandidate> >();
            }
            if (!typeCandidateDictionaryMapping[patternMatch.type].ContainsKey(strSplit.Length))
            {
                typeCandidateDictionaryMapping[patternMatch.type][strSplit.Length] = new List <PatternCandidate>();
            }
            foreach (var candidate in typeCandidateDictionaryMapping[patternMatch.type][strSplit.Length])
            {
                var anyShared             = false;
                var differentPartsCounter = 0;
                var formatStringBuilder   = new StringBuilder(patternMatch.str.Length);
                var patternSplit          = new string[strSplit.Length];
                for (int i = 0; i < strSplit.Length; i++)
                {
                    if (strSplit[i] == candidate.strSplit[i])
                    {
                        anyShared       = true;
                        patternSplit[i] = candidate.strSplit[i];
                        formatStringBuilder.Append(strSplit[i]);
                    }
                    else
                    {
                        patternSplit[i] = ArgumentMark;
                        formatStringBuilder.Append($"{{{differentPartsCounter}}}");
                        differentPartsCounter++;
                    }
                    formatStringBuilder.Append(' ');
                }
                formatStringBuilder.Remove(formatStringBuilder.Length - 1, 1);
                if (anyShared)
                {
                    var formatPattern = formatStringBuilder.ToString();
                    if (!typePatternDictionaryMapping.ContainsKey(patternMatch.type))
                    {
                        typePatternDictionaryMapping[patternMatch.type] = new Dictionary <int, List <Pattern> >();
                    }
                    if (!typePatternDictionaryMapping[patternMatch.type].ContainsKey(strSplit.Length))
                    {
                        typePatternDictionaryMapping[patternMatch.type][strSplit.Length] = new List <Pattern>();
                    }
                    typePatternDictionaryMapping[patternMatch.type][strSplit.Length].Add(new Pattern(patternSplit, currentPatternCodingByte));
                    var toReturn = new CodingInfo(formatPattern, currentPatternCodingByte);
                    currentPatternCodingByte++;
                    return(toReturn);
                }
            }
            typeCandidateDictionaryMapping[patternMatch.type][strSplit.Length].Add(new PatternCandidate(strSplit));
            return(null);
        }