Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace _469
- {
- class Program
- {
- static void Main(string[] args)
- {
- string book4 = "51595646114145190584521765219727830464879636612527578967212778894388727857" +
- "261185764217614588952196180031651288899751121615127215196805970";//kharos
- string book5 = "572785726118576436467243534527560192856114519199118003646889521991"
- + "18006512889523646721191180035765135347830508434856114"; //IoK
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine(book4.Length + "\n");
- var book = new PascalString(book4);
- book.setSetting(Settings.prefix, 1); //places of the starting section denoting length of seq.
- book.setSetting(Settings.chain, true); //always true for books
- book.setSetting(Settings.ltr, true); // left to right or right to left?
- book.setSetting(Settings.placeholder, (byte)'x'); // if string terminates too early fille with x's
- foreach (string s in book.Parse())
- {
- Console.WriteLine(s + ' ');
- }
- Console.ReadKey();
- }
- static void print(string str, bool newline = false)
- {
- Console.Write(str);
- if (newline) Console.Write('\n');
- }
- }
- /*
- * PascalString Library
- */
- enum Settings { radix, prefix, step, chain, placeholder, ltr, nul, newline }; //is it a chain of multiple strings ? if yes what's delimiter
- class PascalString //parsing pascal style strings (length prefixed) into a human readable form
- {
- private string input;
- public byte prefix = 1, radix = 10, step = sizeof(char) / 2;
- private bool iterate, ltr = true;
- private string nul, newline = null;
- private char placeholder = 'x';
- private List<string> strings = new List<string>();
- public PascalString(string NullTerminated)
- {
- if (true)
- {
- char[] array = NullTerminated.ToCharArray();
- Array.Reverse(array);
- NullTerminated = new String(array);
- }
- input = NullTerminated;
- }
- public string[] Parse()
- {
- int pntr = 0;
- if (!iterate)
- {
- return new string[1] { load(ref pntr, prefix) };
- }
- while (pntr < input.Length)
- {
- //Console.Write(seek(prefix,pntr, ref pntr) + ' ');
- strings.Add(load(ref pntr, prefix));
- }
- string[] MyStr = new string[2];
- // Console.WriteLine("write line");
- return strings.ToArray();
- }
- public bool setSetting(Settings Setting, byte value)
- {
- #region switch
- switch ((byte)Setting)
- {
- case 0:
- radix = value;
- break;
- case 1:
- prefix = value;
- break;
- case 2:
- step = value;
- break;
- case 3:
- iterate = value == 0 ? false : true;
- break;
- case 4:
- placeholder = (char)value;
- break;
- case 5:
- ltr = value == 0 ? false : true;
- break;
- default:
- //throw new System.ArgumentException("Parameter not defined", "original");
- return false;
- }
- #endregion
- return true;
- }
- public bool setSetting(Settings Setting, bool value)
- {
- return setSetting(Setting, (byte)(value ? 1 : 0));
- }
- private string load(ref int offset, int prefix = 1)
- {
- int len = Int32.Parse(input.Substring(offset, prefix));
- offset = prefix + offset + len;
- try
- {
- return len.ToString(new String('0', prefix)) + ' ' + input.Substring(offset - len, len);
- }
- catch
- {
- return len.ToString(new String('0', prefix)) + ' ' + input.Substring(offset - len, input.Length - offset + len)
- + new String(placeholder, offset - input.Length);
- }
- }
- private string seek(int prefix, int offset)
- {
- return input.Substring(offset + prefix, Int32.Parse(input.Substring(offset, offset + prefix)));
- }
- public bool isValid() // is the size advertised same as the actual size
- {
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement