예제 #1
0
        public IEnumerable <Nabe> Encode()
        {
            while (stream.Peek() > 0)
            {
                foreach (var b in Encoding.UTF8.GetBytes(stream.ReadLine() + Environment.NewLine))
                {
                    var val = b;
                    yield return(new Nabe((BaseNabe)(val % 3)));

                    do
                    {
                        yield return(Wbase59.ToNabe(val % Max));

                        val /= Max;
                    } while (val >= Max);

                    yield return(Wbase59.ToNabe(val));
                }
            }
        }
예제 #2
0
        /// <summary>
        /// デコード本体
        /// </summary>
        /// <returns></returns>
        public IEnumerable <byte> Decode()
        {
            var nextMod  = 0;
            var stack    = new Stack <int>();
            var encoding = new UnicodeEncoding(true, false);

            while (stream.Peek() > 0)
            {
                for (var item = StringInfo.GetTextElementEnumerator(stream.ReadLine() ?? string.Empty);
                     item.MoveNext();)
                {
                    var bytes = encoding.GetBytes(item.GetTextElement());

                    var nextNabe = Nabe.Parse(bytes.AsSpan());

                    if (nextNabe.Position is null)
                    {
                        if (stack.Any())
                        {
                            yield return((byte)Aggregate(stack, nextMod));
                        }

                        nextMod = (int)nextNabe.Base;
                    }
                    else
                    {
                        stack.Push(Wbase59.ToValue(nextNabe));
                    }
                }

                if (!stack.Any())
                {
                    continue;
                }
                yield return((byte)Aggregate(stack, nextMod));
            }
        }