Пример #1
0
        public HuffmanTable this[char from]
        {
            get
            {
                // Validate
                if ((from < '\x0001') || (from > '\x007f'))
                {
                    if (from != HuffmanItem.StartOrEnd)
                    {
                        throw new ArgumentException(from.ToString(), "from");
                    }
                }

                // Attach to the table
                HuffmanTable table = Tables.Find(t => t.Source[0] == from);

                // Found it
                if (null != table)
                {
                    return(table);
                }

                // Create a new table
                table = new HuffmanTable {
                    Source = new string( new char[] { from } )
                };

                // Remember it
                Tables.Add(table);

                // Report
                return(table);
            }
        }
Пример #2
0
        public HuffmanTable this[char from]
        {
            get
            {
                // Validate
                if ((from < '\x0001') || (from > '\x007f'))
                    if (from != HuffmanItem.StartOrEnd)
                        throw new ArgumentException( from.ToString(), "from" );

                // Attach to the table
                HuffmanTable table = Tables.Find( t => t.Source[0] == from );

                // Found it
                if (null != table)
                    return table;

                // Create a new table
                table = new HuffmanTable { Source = new string( new char[] { from } ) };

                // Remember it
                Tables.Add( table );

                // Report
                return table;
            }
        }
Пример #3
0
        /// <summary>
        /// Ergänzt eine alternative Auflösungssequenz.
        /// </summary>
        /// <param name="sequence">Die neue Sequenz.</param>
        public void AddAlternateSequence(string sequence)
        {
            // Validate
            HuffmanTable.ValidateSequence(sequence);

            // All of it
            Dictionary <string, string> sequences = new Dictionary <string, string>();

            // Load
            if (!string.IsNullOrEmpty(AlternateSequences))
            {
                sequences = AlternateSequences.Split(';').ToDictionary(s => s);
            }

            // Add the new one
            sequences[sequence] = sequence;

            // Push back
            AlternateSequences = string.Join(";", sequences.Keys.ToArray());
        }
Пример #4
0
        /// <summary>
        /// Ermittelt zu einer Sequence von UTF-8 codierten Zeichen die zugehörige Übergangssequenz.
        /// </summary>
        /// <param name="text">Die Zeichensequenz.</param>
        /// <param name="offset">Das erste auszuwertende Zeichen.</param>
        /// <param name="length">Die Anzahl der auszuwertenden Zeichen.</param>
        /// <param name="good">Meldet die Anzahl der bearbeiteten Zeichen.</param>
        /// <param name="blindEscape">Gesetzt um eine vorhandene Ausnahmesequenz so oft wie möglich zu verwenden.</param>
        /// <returns>Die ermittelte Übergangssequenz.</returns>
        public string GetSequence(byte[] text, int offset, int length, bool blindEscape, out int good)
        {
            // Result
            StringBuilder result = new StringBuilder();

            // Previous item
            char?prev = null;

            // Count processed items
            good = -1;

            // Process all
            while (length-- > 0)
            {
                // Load
                byte next = text[offset++];
                char item = (char)next;

                // Not allowed
                if (next < 1)
                {
                    break;
                }

                // Check for escape mode
                if ((next > 127) && (HuffmanItem.StartOrEnd != item))
                {
                    // Not allowed at the very beginning
                    if (!prev.HasValue)
                    {
                        break;
                    }

                    // Check out the escape sequence
                    string escape = this[prev.Value, HuffmanItem.PassThrough];
                    if (string.IsNullOrEmpty(escape))
                    {
                        break;
                    }

                    // Enter the escape sequence and the current character
                    result.Append(escape);
                    AppendHex(result, next);

                    // Count it
                    ++good;

                    // Repeat
                    while (length-- > 0)
                    {
                        // Load
                        next = text[offset++];
                        item = (char)next;

                        // Forbidden
                        if (next < 1)
                        {
                            break;
                        }

                        // Always add
                        AppendHex(result, next);

                        // Count it
                        ++good;

                        // Done
                        if (next <= 127)
                        {
                            break;
                        }
                    }

                    // Forbidden
                    if (next < 1)
                    {
                        break;
                    }
                }
                else
                {
                    // Not for the first byte
                    if (prev.HasValue)
                    {
                        // Get the sequence
                        string sequence = this[prev.Value, item];

                        // Done
                        if (string.IsNullOrEmpty(sequence))
                        {
                            // See if the current target is a confirmed escape character
                            HuffmanTable table = this[prev.Value];

                            // Respect client settings
                            if (!blindEscape)
                            {
                                // No confirmed escapes at all
                                if (null == table.ConfirmedEscapes)
                                {
                                    break;
                                }

                                // Not an confirmed escape
                                if (!table.ConfirmedEscapes.Contains(item))
                                {
                                    break;
                                }
                            }

                            // Read the escape sequence
                            sequence = table[HuffmanItem.PassThrough];
                            if (string.IsNullOrEmpty(sequence))
                            {
                                break;
                            }

                            // Did it all
                            if (item == HuffmanItem.StartOrEnd)
                            {
                                // Append escape
                                result.Append(sequence);

                                // Append terminator
                                result.Append("00000000");

                                // Count it
                                ++good;

                                // Done
                                break;
                            }

                            // Create new sequence
                            StringBuilder escape = new StringBuilder(sequence);

                            // Append the item
                            AppendHex(escape, (byte)item);

                            // Use it
                            sequence = escape.ToString();
                        }

                        // Append
                        result.Append(sequence);
                    }

                    // Count it
                    ++good;
                }

                // Just remember for next step
                prev = item;
            }

            // Report
            return(result.ToString());
        }