// 整型数 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); }
// 整型数 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); }
// 整型数 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); }
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); }
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); }