Пример #1
0
        /// <summary>
        /// Defines a binary-to-text encoding.
        /// Additional decode characters let you add aliases, and a filter callback can be used
        /// to make decoding case-insensitive among other things.
        /// </summary>
        /// <param name="characterSet">The characters of the encoding.</param>
        /// <param name="msbComesFirst">
        ///     <c>true</c> to begin with the most-significant bit of each byte.
        ///     Otherwise, the encoding begins with the least-significant bit.
        /// </param>
        /// <param name="additionalDecodeCharacters">
        ///     A dictionary of alias characters, or <c>null</c> if no aliases are desired.
        /// </param>
        /// <param name="decodeFilterCallback">
        ///     A callback to map arbitrary characters onto the characters that can be decoded.
        /// </param>
        public BaseEncoding(string characterSet, bool msbComesFirst,
                            IDictionary <char, int> additionalDecodeCharacters,
                            BaseEncodingDecodeFilterCallback decodeFilterCallback)
        {
            Check.Null("characterSet", characterSet);

            if (!BitMath.IsPositivePowerOf2(characterSet.Length))
            {
                throw Exceptions.Argument("characterSet",
                                          "Length must be a power of 2.");
            }

            if (characterSet.Length > 256)
            {
                throw Exceptions.Argument("characterSet",
                                          "Character sets with over 256 characters are not supported.");
            }

            _bitCount             = 31 - BitMath.CountLeadingZeros(characterSet.Length);
            _bitMask              = (1 << _bitCount) - 1;
            _characters           = characterSet;
            _msbComesFirst        = msbComesFirst;
            _decodeFilterCallback = decodeFilterCallback;

            _values = additionalDecodeCharacters != null
                ? new Dictionary <char, int>(additionalDecodeCharacters)
                : new Dictionary <char, int>();
            for (int i = 0; i < characterSet.Length; i++)
            {
                char ch = characterSet[i];
                if (_values.ContainsKey(ch))
                {
                    throw Exceptions.Argument("Duplicate characters are not supported.",
                                              "characterSet");
                }
                _values.Add(ch, (byte)i);
            }
        }
Пример #2
0
        /// <summary>
        /// Defines a binary-to-text encoding.
        /// Additional decode characters let you add aliases, and a filter callback can be used
        /// to make decoding case-insensitive among other things.
        /// </summary>
        /// <param name="characterSet">The characters of the encoding.</param>
        /// <param name="msbComesFirst">
        ///     <c>true</c> to begin with the most-significant bit of each byte.
        ///     Otherwise, the encoding begins with the least-significant bit.
        /// </param>
        /// <param name="additionalDecodeCharacters">
        ///     A dictionary of alias characters, or <c>null</c> if no aliases are desired.
        /// </param>
        /// <param name="decodeFilterCallback">
        ///     A callback to map arbitrary characters onto the characters that can be decoded.
        /// </param>
        public BaseEncoding(string characterSet, bool msbComesFirst,
            IDictionary<char, int> additionalDecodeCharacters,
            BaseEncodingDecodeFilterCallback decodeFilterCallback)
        {
            Check.Null("characterSet", characterSet);

            if (!BitMath.IsPositivePowerOf2(characterSet.Length))
            {
                throw Exceptions.Argument("characterSet",
                                          "Length must be a power of 2.");
            }

            if (characterSet.Length > 256)
            {
                throw Exceptions.Argument("characterSet",
                                          "Character sets with over 256 characters are not supported.");
            }

            _bitCount = 31 - BitMath.CountLeadingZeros(characterSet.Length);
            _bitMask = (1 << _bitCount) - 1;
            _characters = characterSet;
            _msbComesFirst = msbComesFirst;
            _decodeFilterCallback = decodeFilterCallback;

            _values = additionalDecodeCharacters != null
                ? new Dictionary<char, int>(additionalDecodeCharacters)
                : new Dictionary<char, int>();
            for (int i = 0; i < characterSet.Length; i ++)
            {
                char ch = characterSet[i];
                if (_values.ContainsKey(ch))
                {
                    throw Exceptions.Argument("Duplicate characters are not supported.",
                                              "characterSet");
                }
                _values.Add(ch, (byte)i);
            }
        }