Exemple #1
0
        /// <summary>
        ///     Decodes the given mini string into a regular string.
        /// </summary>
        /// <param name="bytes">The beytes to decode.</param>
        /// <returns>The string.</returns>
        /// <seealso href="https://patrick-sachs.de/projekte/ministring/" />
        public static string Decode(byte[] bytes)
        {
            StringBuilder builder = new StringBuilder();
            var           chars   = new List <MiniChar>();

            using (MemoryStream stream = new MemoryStream(bytes)) {
                int  byte1     = stream.ReadByte();
                int  byte2     = stream.ReadByte();
                bool isDone    = byte1 == -1;
                var  b         = new bool[6];
                int  bitOffset = 0;
                while (!isDone)
                {
                    for (int i = 0; i < 6; i++)
                    {
                        b[i] = ((byte)byte1).GetBit(bitOffset);
                        bitOffset++;
                        if (bitOffset == 8)
                        {
                            byte1 = byte2;
                            if (byte1 == -1)
                            {
                                isDone = true;
                                byte1  = 0;
                                byte2  = -1;
                            }
                            else
                            {
                                byte2 = stream.ReadByte();
                            }
                            bitOffset = 0;
                        }
                    }
                    MiniChar mini = new MiniChar(b[5], b[4], b[3], b[2], b[1], b[0]);
                    chars.Add(mini);
                }
            }
            MiniCharInStream inStream = new MiniCharInStream(chars);

            foreach (char c in inStream)
            {
                builder.Append(c);
            }
            return(builder.ToString());
        }
 public IEnumerator <char> GetEnumerator()
 {
     using (IEnumerator <MiniChar> enmrtr = m_Chars.GetEnumerator()) {
         while (enmrtr.MoveNext())
         {
             char simple = MiniString.GetSimpleChar(enmrtr.Current);
             if (simple != '\0')
             {
                 yield return(simple);
             }
             else
             {
                 var codePoints = new MiniChar[3];
                 for (int i = 0; i < 3; i++)
                 {
                     if (!enmrtr.MoveNext())
                     {
                         if (i == 1)
                         {
                             yield break;
                         }
                         codePoints[i] = MiniString.GetSimpleChar('\0');
                     }
                     else
                     {
                         codePoints[i] = enmrtr.Current;
                     }
                 }
                 ushort codePoint = 0;
                 for (int i = 0; i < 16; i++)
                 {
                     // The first two bits in the first mini char are unused.
                     MiniChar current = codePoints[(i + 2) / 6];
                     bool     bit     = current[(i + 2) % 6 + 1];
                     if (bit)
                     {
                         codePoint |= (ushort)(1 << i);
                     }
                 }
                 yield return((char)codePoint);
             }
         }
     }
 }
Exemple #3
0
 /// <summary>
 ///     Registers the given character aswell as the mini character in both lookups.
 /// </summary>
 /// <param name="c">The character.</param>
 /// <param name="mc">The mini character.</param>
 private static void Register(char c, MiniChar mc)
 {
     s_ToMini.Add(c, mc);
     s_ToChar.Add(mc, c);
 }
Exemple #4
0
 /// <summary>
 ///     Gets the character the given mini char represents.
 /// </summary>
 /// <param name="c">The mini character.</param>
 /// <returns>The character.</returns>
 internal static char GetSimpleChar(MiniChar c)
 {
     return(s_ToChar[c]);
 }
Exemple #5
0
 protected bool Equals(MiniChar other)
 {
     return(B1 == other.B1 && B2 == other.B2 && B3 == other.B3 && B4 == other.B4 && B5 == other.B5 && B6 == other.B6);
 }