Пример #1
0
 public void TestPack()
 {
     Assert.Equal(BitPackage.Pack4bits(0, 0, 0, 0, 0, 0, 0, 0), 0);
     Assert.Equal(BitPackage.Pack4bits(1, 1, 1, 1, 1, 1, 1, 1), 286331153);
     Assert.Equal(BitPackage.Pack4bits(2, 2, 2, 2, 2, 2, 2, 2), 572662306);
     Assert.Equal(BitPackage.Pack4bits(15, 15, 15, 15, 15, 15, 15, 15), -1);
 }
Пример #2
0
        // 整型数
        public static byte[] Bit6Compact(string text)
        {
            // 检查
            CheckBit6(text);

            BitPackage package = new BitPackage();

            foreach (char ch in text)
            {
#if DEBUG
                int prefix = (ch >> 6) & 0x3;
                Debug.Assert(prefix == 0x0 || prefix == 0x1);
#endif
                int start = ch << 2;
                for (int i = 0; i < 6; i++)
                {
                    package.Add((start & 0x80) == 0x80 ? true : false);
                    start = start << 1;
                }
            }

            // 尾部 bits 补齐 BIN 100000
            if (package.Index != 0)
            {
                package.Add(true);
            }

            package.Flush();
            return(package.Bytes);
        }
Пример #3
0
        // 整型数
        public static byte[] Bit7Compact(string text)
        {
            // 检查
            CheckBit7(text);

            BitPackage package = new BitPackage();

            foreach (char ch in text)
            {
#if DEBUG
                int prefix = (ch >> 7) & 0x1;
                Debug.Assert(prefix == 0x0);
#endif
                int start = ch << 1;
                for (int i = 0; i < 7; i++)
                {
                    package.Add((start & 0x80) == 0x80 ? true : false);
                    start = start << 1;
                }
            }

            // 尾部 bits 补齐 BIN 1111111
            if (package.Index != 0)
            {
                int count = 8 - package.Index;
                for (int i = 0; i < count; i++)
                {
                    package.Add(true);
                }
            }

            package.Flush();
            return(package.Bytes);
        }
Пример #4
0
 public void ShouldPackCorrectly()
 {
     Assert.Equal(BitPackage.Pack4bits(0, 0, 0, 0, 0, 0, 0, 0), 0);
     Assert.Equal(BitPackage.Pack4bits(1, 1, 1, 1, 1, 1, 1, 1), 286331153);
     Assert.Equal(BitPackage.Pack4bits(2, 2, 2, 2, 2, 2, 2, 2), 572662306);
     Assert.Equal(BitPackage.Pack4bits(15, 15, 15, 15, 15, 15, 15, 15), -1);
 }
Пример #5
0
 protected StateMachineModel(BitPackage classTable, int classFactor, BitPackage stateTable, int[] charLenTable, string name)
 {
     this.ClassTable   = classTable;
     this.classFactor  = classFactor;
     this.StateTable   = stateTable;
     this.CharLenTable = charLenTable;
     this.name         = name;
 }
Пример #6
0
 protected StateMachineModel(BitPackage classTable, int classFactor, BitPackage stateTable, int[] charLenTable, string name)
 {
     this.ClassTable = classTable;
     this.classFactor = classFactor;
     this.StateTable = stateTable;
     this.CharLenTable = charLenTable;
     this.name = name;
 }
Пример #7
0
 public StateMachineModel(BitPackage classTable, int classFactor,
                          BitPackage stateTable, int[] charLenTable, String name)
 {
     this.classTable   = classTable;
     ClassFactor       = classFactor;
     this.stateTable   = stateTable;
     this.charLenTable = charLenTable;
     Name = name;
 }
Пример #8
0
        public void TestUnpack()
        {
            int[] data = new int[] {
                BitPackage.Pack4bits(0, 1, 2, 3, 4, 5, 6, 7),
                BitPackage.Pack4bits(8, 9, 10, 11, 12, 13, 14, 15)
            };

            BitPackage pkg = new BitPackage(
                BitPackage.INDEX_SHIFT_4BITS,
                BitPackage.SHIFT_MASK_4BITS,
                BitPackage.BIT_SHIFT_4BITS,
                BitPackage.UNIT_MASK_4BITS,
                data);

            for (int i = 0; i < 16; i++)
            {
                int n = pkg.Unpack(i);
                Assert.Equal(n, i);
            }
        }
Пример #9
0
        public void ShouldUnpackCorrectly()
        {
            var data = new int[]
            {
                BitPackage.Pack4bits(0, 1, 2, 3, 4, 5, 6, 7),
                BitPackage.Pack4bits(8, 9, 10, 11, 12, 13, 14, 15)
            };

            var package = new BitPackage(
                BitPackage.IndexShift4Bits,
                BitPackage.ShiftMask4Bits,
                BitPackage.BitShift4Bits,
                BitPackage.UnitMask4Bits,
                data);

            for (int i = 0; i < 16; i++)
            {
                int actual = package.Unpack(i);
                Assert.Equal(actual, i);
            }
        }
Пример #10
0
        static string AddToPackage(char ch, char charset, BitPackage package)
        {
            string bits = "";

            if (charset == 'u')
            {
                bits = GetUpperSetBits(ch);
            }
            else if (charset == 'l')
            {
                bits = GetLowerSetBits(ch);
            }
            else if (charset == 'd')
            {
                bits = GetDigiSetBits(ch);
            }
            else
            {
                throw new ArgumentException($"无法识别的 charset '{charset}'");
            }
            package.AddRange(bits);
            return(bits);
        }
Пример #11
0
        // 整型数
        public static byte[] Bit5Compact(string text)
        {
            // 检查
            CheckBit5(text);

            BitPackage package = new BitPackage();

            foreach (char ch in text)
            {
#if DEBUG
                int prefix = (ch >> 5) & 0x7;
                Debug.Assert(prefix == 2);
#endif
                int start = ch << 3;
                for (int i = 0; i < 5; i++)
                {
                    package.Add((start & 0x80) == 0x80 ? true : false);
                    start = start << 1;
                }
            }

            package.Flush();
            return(package.Bytes);
        }
Пример #12
0
        public static byte[] IsilCompact(string text,
                                         StringBuilder debugInfo = null)
        {
            // 检查
            CheckIsil(text);

            BitPackage package      = new BitPackage();
            char       prev_charset = 'u'; // u(大写) l(小写) d(数字)

            for (int index = 0; index < text.Length; index++)
            {
                char ch = text[index];

                if (debugInfo != null)
                {
                    debugInfo.Append($"{ch}");
                }

                char next_ch = (char)0;
                if (index + 1 <= text.Length - 1)
                {
                    next_ch = text[index + 1];
                }

                // 获得当前字符可能处于的字符集
                string current_charsets = GetCharsets(ch);

                string action  = "";
                string targets = "";
                // 如果当前字符的字符集和 prev_charset 不同
                // 就必须 switch 或者 shift
                if (current_charsets.IndexOf(prev_charset) == -1)
                {
                    // 1) 如果 next_ch 在 prev_charset 以内,则用 shift
                    if (next_ch == 0 ||
                        GetCharsets(next_ch).IndexOf(prev_charset) != -1)
                    {
                        action  = "shift";
                        targets = current_charsets[0].ToString();
                    }
                    else
                    {
                        action  = "switch";
                        targets = GetCharsets(ch);
                    }

                    if (debugInfo != null)
                    {
                        debugInfo.Append($" {action}:{prev_charset}-{targets}");
                    }

                    string output = "";
                    if (action == "switch")
                    {
                        output = GetSwitchBits(prev_charset, targets[0]);
                        package.AddRange(output);
                        prev_charset = targets[0];
                        output      += "," + AddToPackage(ch, prev_charset, package);
                    }
                    else
                    {
                        Debug.Assert(action == "shift");
                        output = GetShiftBits(prev_charset, targets[0]);
                        package.AddRange(output);
                        output += "," + AddToPackage(ch, targets[0], package);
                    }

                    if (debugInfo != null)
                    {
                        debugInfo.Append($" output:{output}\r\n");
                    }
                }
                else
                {
                    // 直接输出字符
                    string output = AddToPackage(ch, prev_charset, package);
                    if (debugInfo != null)
                    {
                        debugInfo.Append($" output:{output}\r\n");
                    }
                }
            }

            // 尾部 bits 补齐 BIN 1111111
            if (package.Index != 0)
            {
                int count = 8 - package.Index;
                for (int i = 0; i < count; i++)
                {
                    package.Add(true);
                }
            }

            package.Flush();
            return(package.Bytes);
        }
Пример #13
0
        public static byte[] IsilCompress(string text)
        {
            // 检查
            CheckIsil(text);

            BitPackage package         = new BitPackage();
            char       current_charset = 'u'; // u(大写) l(小写) d(数字)

            for (int index = 0; index < text.Length; index++)
            {
                char ch = text[index];

                if (ch == '-')
                {
                    if (current_charset == 'u' || current_charset == 'l')
                    {
                        package.AddRange("00000");
                    }
                    else
                    {
                        Debug.Assert(current_charset == 'd');
                        package.AddRange("1010");
                    }
                    continue;
                }

                if (ch == ':')
                {
                    if (current_charset == 'u')
                    {
                        package.AddRange("11011");
                    }
                    else if (current_charset == 'd')
                    {
                        package.AddRange("1011");
                    }
                    else
                    {
                        Debug.Assert(current_charset == 'l');
                        // TODO: 切换到 upper 或者 digit。最好预测一下,下一个字符,以便切换最优化
                        package.AddRange(GetSwitchBits(current_charset, 'u'));
                        current_charset = 'u';
                        package.AddRange("11011");
                    }
                    continue;
                }

                if (ch == '/')
                {
                    if (current_charset == 'l')
                    {
                        package.AddRange("11011");
                    }
                    else
                    {
                        Debug.Assert(current_charset == 'd' || current_charset == 'u');
                        // 切换到 lower
                        package.AddRange(GetSwitchBits(current_charset, 'l'));
                        current_charset = 'l';
                        package.AddRange("11011");
                    }
                    continue;
                }

                if (ch >= 'A' && ch <= 'Z')
                {
                    if (current_charset != 'u')
                    {
                        package.AddRange(GetSwitchBits(current_charset, 'u'));
                        current_charset = 'u';
                    }
                    AddToPackage(ch, current_charset, package);
                    continue;
                }

                if (ch >= 'a' && ch <= 'z')
                {
                    if (current_charset != 'l')
                    {
                        package.AddRange(GetSwitchBits(current_charset, 'l'));
                        current_charset = 'l';
                    }
                    AddToPackage(ch, current_charset, package);
                    continue;
                }

                if (ch >= '0' && ch <= '9')
                {
                    if (current_charset != 'd')
                    {
                        package.AddRange(GetSwitchBits(current_charset, 'd'));
                        current_charset = 'd';
                    }
                    AddToPackage(ch, current_charset, package);
                    continue;
                }

                throw new Exception($"出现了超出范围的字符 '{ch}'");
            }

            // 尾部 bits 补齐 BIN 1111111
            if (package.Index != 0)
            {
                int count = 8 - package.Index;
                for (int i = 0; i < count; i++)
                {
                    package.Add(true);
                }
            }

            package.Flush();
            return(package.Bytes);
        }