Advertisement
AlFas

Convert array bracket appearance from C# to C++

Oct 5th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using static System.Console;
  8. using static System.IO.File;
  9.  
  10. namespace ArrayBracketPositioning
  11. {
  12.     public static class Program
  13.     {
  14.         public static readonly string[] CSharpKeywords = { "bool", "int", "double", "short", "long", "decimal", "string", "uint", "ulong", "ushort", "byte", "sbyte", "char" };
  15.         public static string fileName;
  16.         public static string[] lines;
  17.        
  18.         public static void Main(string[] args)
  19.         {
  20.             WriteLineWithColor("Please enter the name of the file to convert the array bracket appearance from C# to C++.", ConsoleColor.Yellow);
  21.             ForegroundColor = ConsoleColor.Cyan;
  22.             fileName = ReadLine();
  23.             lines = ReadAllLines(fileName);
  24.             for (int i = 0; i < lines.Length; i++)
  25.             {
  26.                 if (lines[i].Length > 0)
  27.                 {
  28.                     int indent = 0;
  29.                     int offset = 0;
  30.                     while (lines[i][offset] == ' ' || lines[i][offset] == '\t')
  31.                     {
  32.                         if (lines[i][offset] == ' ')
  33.                             indent++;
  34.                         else if (lines[i][offset] == '\t')
  35.                             indent += 4;
  36.                         offset++;
  37.                     }
  38.                     lines[i] = lines[i].Substring(offset);
  39.                     string[] words = lines[i].Split(' ');
  40.                     //bool isInQuotes = false;
  41.                     for (int j = 0; j < words.Length - 1; j++)
  42.                     {
  43.                         //if (!isInQuotes)
  44.                         //{
  45.                             string word = words[j].Split('(').Last();
  46.                             if (!words[j].StartsWith("//"))
  47.                             {
  48.                                 if (word.EndsWith("[]"))
  49.                                     if (CSharpKeywords.Contains(word.Remove(word.Length - 2)))
  50.                                     {
  51.                                         words[j] = words[j].Remove(words[j].Length - 2);
  52.                                         string[] parameter = words[j + 1].Split('=');
  53.                                         int indexToInsert = parameter[0].Length;
  54.                                         while (parameter[0][indexToInsert - 1] == ',')
  55.                                             indexToInsert--;
  56.                                         parameter[0] = parameter[0].Insert(indexToInsert, "[]");
  57.                                         words[j + 1] = parameter.Combine("=");
  58.                                         j++;
  59.                                     }
  60.                                 //for (int k = 0; k < words[j].Length; k++)
  61.                                 //    if (words[j][k] == '"')
  62.                                 //        if ((k > 0 && words[j][k - 1] != '\\') || k == 0)
  63.                                 //            isInQuotes = !isInQuotes;
  64.                             }
  65.                             else break;
  66.                         //}
  67.                         //else
  68.                         //    for (int k = 0; k < words[j].Length; k++)
  69.                         //        if (words[j][k] == '"')
  70.                         //            if ((k > 0 && words[j][k - 1] != '\\') || k == 0)
  71.                         //                isInQuotes = !isInQuotes;
  72.                     }
  73.                    
  74.                     StringBuilder s = new StringBuilder();
  75.                     for (int j = 0; j < indent; j++)
  76.                         s.Append(" ");
  77.                     s.Append(words.Combine(" "));
  78.                     lines[i] = s.ToString();
  79.                 }
  80.             }
  81.             WriteAllLines(fileName, lines);
  82.             WriteLineWithColor("\nThe array brackets have been successfully converted from C# to C++.", ConsoleColor.Green);
  83.             ReadKey();
  84.         }
  85.  
  86.         public static void WriteWithColor(string s, ConsoleColor c)
  87.         {
  88.             ForegroundColor = c;
  89.             Write(s);
  90.         }
  91.         public static void WriteLineWithColor(string s, ConsoleColor c)
  92.         {
  93.             ForegroundColor = c;
  94.             WriteLine(s);
  95.         }
  96.     }
  97.     public static class Extensions
  98.     {
  99.         public static int Find(this string s, string match)
  100.         {
  101.             for (int i = 0; i <= s.Length - match.Length; i++)
  102.             {
  103.                 string sub = s.Substring(i, match.Length);
  104.                 if (sub == match)
  105.                     return i;
  106.             }
  107.             return -1;
  108.         }
  109.         public static int Find(this string s, string match, int occurence)
  110.         {
  111.             int occurences = 0;
  112.             for (int i = 0; i <= s.Length - match.Length; i++)
  113.             {
  114.                 string sub = s.Substring(i, match.Length);
  115.                 if (sub == match)
  116.                 {
  117.                     occurences++;
  118.                     if (occurences == occurence)
  119.                         return i;
  120.                 }
  121.             }
  122.             return -1;
  123.         }
  124.         public static int Find(this string s, string match, int start, int end)
  125.         {
  126.             for (int i = start; i <= end - match.Length; i++)
  127.             {
  128.                 string sub = s.Substring(i, match.Length);
  129.                 if (sub == match)
  130.                     return i;
  131.             }
  132.             return -1;
  133.         }
  134.         public static int GetLastNumber(this string s)
  135.         {
  136.             int n = 0;
  137.             bool isLastNumber = false;
  138.             for (int i = 0; i < s.Length; i++)
  139.             {
  140.                 isLastNumber = int.TryParse(s.Substring(i), out n);
  141.                 if (isLastNumber)
  142.                     return n;
  143.             }
  144.             throw new ArgumentException("The string has no number in the end.");
  145.         }
  146.         public static int[] FindAll(this string s, string match)
  147.         {
  148.             List<int> indices = new List<int>();
  149.             for (int i = 0; i <= s.Length - match.Length; i++)
  150.             {
  151.                 string sub = s.Substring(i, match.Length);
  152.                 if (sub == match)
  153.                     indices.Add(i);
  154.             }
  155.             return indices.ToArray();
  156.         }
  157.         public static int[] FindAll(this string s, string match, int start, int end)
  158.         {
  159.             List<int> indices = new List<int>();
  160.             for (int i = start; i <= end - match.Length; i++)
  161.             {
  162.                 string sub = s.Substring(i, match.Length);
  163.                 if (sub == match)
  164.                     indices.Add(i);
  165.             }
  166.             return indices.ToArray();
  167.         }
  168.         public static string FixBase64String(this string s)
  169.         {
  170.             int lastNumber = 0;
  171.             int lastInvalidCharacter = 0;
  172.             bool continueChecking = true;
  173.             while (continueChecking)
  174.             {
  175.                 if (s[s.Length - lastNumber - 1].IsNumber())
  176.                     lastNumber++;
  177.                 else if (!s[s.Length - lastNumber - 1].IsBase64Character())
  178.                     lastInvalidCharacter = ++lastNumber;
  179.                 else
  180.                     continueChecking = false;
  181.             }
  182.             s = s.Substring(0, s.Length - lastInvalidCharacter);
  183.             while (s.Length % 4 != 0)
  184.                 s += "=";
  185.             return s;
  186.         }
  187.         public static string Substring(this string s, string from, string to)
  188.         {
  189.             int startIndex = s.Find(from) + from.Length;
  190.             int endIndex = s.Find(to);
  191.             int length = endIndex - startIndex;
  192.             return s.Substring(startIndex, length);
  193.         }
  194.         public static string RemoveLastNumber(this string s)
  195.         {
  196.             string l = s;
  197.             for (int i = 0; i < s.Length; i++)
  198.             {
  199.                 l = s.Substring(i);
  200.                 if (int.TryParse(l, out int shit))
  201.                     return s.Substring(0, i);
  202.             }
  203.             return s;
  204.         }
  205.         public static string Replace(this string originalString, string stringToReplaceWith, int startIndex, int length)
  206.         {
  207.             string result = originalString;
  208.             result = result.Remove(startIndex, length);
  209.             result = result.Insert(startIndex, stringToReplaceWith);
  210.             return result;
  211.         }
  212.         public static string ReplaceWholeWord(this string originalString, string oldString, string newString)
  213.         {
  214.             for (int i = originalString.Length - oldString.Length; i >= 0; i--)
  215.             {
  216.                 if (originalString.Substring(i, oldString.Length) == oldString)
  217.                     if ((i > 0 ? (!originalString[i - 1].IsLetterOrNumber()) : true) && (i < originalString.Length - oldString.Length ? (!originalString[i + oldString.Length].IsLetterOrNumber()) : true))
  218.                     {
  219.                         originalString = originalString.Replace(newString, i, oldString.Length);
  220.                         i -= oldString.Length;
  221.                     }
  222.             }
  223.             return originalString;
  224.         }
  225.         public static bool ContainsWholeWord(this string s, string match)
  226.         {
  227.             for (int i = s.Length - match.Length; i >= 0; i--)
  228.             {
  229.                 if (s.Substring(i, match.Length) == match)
  230.                     if ((i > 0 ? (!s[i - 1].IsLetterOrNumber()) : true) && (i < s.Length - match.Length ? (!s[i + match.Length].IsLetterOrNumber()) : true))
  231.                         return true;
  232.             }
  233.             return false;
  234.         }
  235.         public static bool MatchesStringCaseFree(this string s, string match)
  236.         {
  237.             if (s.Length != match.Length)
  238.                 return false;
  239.             for (int i = 0; i < s.Length; i++)
  240.             {
  241.                 if (s[i].IsUpperCaseLetter())
  242.                     if (s[i] + 32 != match[i] && s[i] != match[i])
  243.                         return false;
  244.                     else if (s[i].IsLowerCaseLetter())
  245.                         if (s[i] - 32 != match[i] && s[i] != match[i])
  246.                             return false;
  247.             }
  248.             return true;
  249.         }
  250.         public static bool IsBase64Character(this char c)
  251.         {
  252.             return IsNumber(c) || IsLetter(c) || c == '/' || c == '+' || c == '=';
  253.         }
  254.         public static bool IsLetter(this char c) => (c >= 65 && c <= 90 || c >= 97 && c <= 122);
  255.         public static bool IsLowerCaseLetter(this char c) => (c >= 97 && c <= 122);
  256.         public static bool IsNumber(this char c) => (c >= 48 && c <= 57);
  257.         public static bool IsUpperCaseLetter(this char c) => (c >= 65 && c <= 90);
  258.         public static bool IsLetterOrNumber(this char c) => IsLetter(c) || IsNumber(c);
  259.  
  260.         public static int[] FindOccurences(this string[] a, string match)
  261.         {
  262.             if (a != null)
  263.             {
  264.                 List<int> occurences = new List<int>();
  265.                 for (int i = 0; i < a.Length; i++)
  266.                     if (a[i] == match)
  267.                         occurences.Add(i);
  268.                 return occurences.ToArray();
  269.             }
  270.             else return new int[0];
  271.         }
  272.         public static string[] Replace(this string[] a, char oldChar, char newChar)
  273.         {
  274.             for (int i = 0; i < a.Length; i++)
  275.                 a[i] = a[i].Replace(oldChar, newChar);
  276.             return a;
  277.         }
  278.         public static string[] Replace(this string[] a, string oldString, string newString)
  279.         {
  280.             for (int i = 0; i < a.Length; i++)
  281.                 a[i] = a[i].Replace(oldString, newString);
  282.             return a;
  283.         }
  284.         public static string[] ReplaceWholeWord(this string[] a, string oldString, string newString)
  285.         {
  286.             for (int i = 0; i < a.Length; i++)
  287.                 a[i] = a[i].ReplaceWholeWord(oldString, newString);
  288.             return a;
  289.         }
  290.         public static string[] RemoveEmptyElements(this string[] a)
  291.         {
  292.             List<string> result = new List<string>();
  293.             for (int i = 0; i < a.Length; i++)
  294.                 if (a[i].Length > 0)
  295.                     result.Add(a[i]);
  296.             return result.ToArray();
  297.         }
  298.         public static bool ContainsAtLeast(this string[] a, string match)
  299.         {
  300.             if (a != null)
  301.             {
  302.                 for (int i = 0; i < a.Length; i++)
  303.                     if (a[i].Contains(match))
  304.                         return true;
  305.                 return false;
  306.             }
  307.             return false;
  308.         }
  309.         public static bool ContainsAtLeastWholeWord(this string[] a, string match)
  310.         {
  311.             if (a != null)
  312.             {
  313.                 for (int i = 0; i < a.Length; i++)
  314.                     if (a[i].ContainsWholeWord(match))
  315.                         return true;
  316.                 return false;
  317.             }
  318.             return false;
  319.         }
  320.         public static string Combine(this string[] s)
  321.         {
  322.             StringBuilder str = new StringBuilder();
  323.             for (int i = 0; i < s.Length; i++)
  324.                 str = str.Append(s[i]);
  325.             return str.ToString();
  326.         }
  327.         public static string Combine(this string[] s, string separator)
  328.         {
  329.             if (s.Length > 0)
  330.             {
  331.                 StringBuilder str = new StringBuilder();
  332.                 str = str.Append(s[0]);
  333.                 for (int i = 1; i < s.Length; i++)
  334.                     str = str.Append(separator + s[i]);
  335.                 return str.ToString();
  336.             }
  337.             else return "";
  338.         }
  339.         public static string[,] Split(this string[] s, char separator)
  340.         {
  341.             List<string[]> separated = new List<string[]>();
  342.             for (int i = 0; i < s.Length; i++)
  343.                 separated.Add(s[i].Split(separator));
  344.             return separated.ToTwoDimensionalArray();
  345.         }
  346.  
  347.         public static T[] GetInnerArray<T>(this T[,] ar, int innerArrayIndex)
  348.         {
  349.             T[] innerAr = new T[ar.GetLength(1)];
  350.             for (int i = 0; i < innerAr.Length; i++)
  351.                 innerAr[i] = ar[innerArrayIndex, i];
  352.             return innerAr;
  353.         }
  354.         public static int[] GetLengths<T>(this List<T[]> l)
  355.         {
  356.             int[] lengths = new int[l.Count];
  357.             for (int i = 0; i < l.Count; i++)
  358.                 lengths[i] = l[i].Length;
  359.             return lengths;
  360.         }
  361.         public static int[] GetLengths(this int[,] ar)
  362.         {
  363.             int[] lengths = new int[ar.Length];
  364.             for (int i = 0; i < ar.Length; i++)
  365.                 lengths[i] = ar.GetInnerArray(i).Length;
  366.             return lengths;
  367.         }
  368.         public static int[] GetLengths(this string[,] ar)
  369.         {
  370.             int[] lengths = new int[ar.GetLength(0)];
  371.             for (int i = 0; i < lengths.Length; i++)
  372.                 lengths[i] = ar.GetInnerArray(i).Length;
  373.             return lengths;
  374.         }
  375.  
  376.         public static T[,] ToTwoDimensionalArray<T>(this List<T[]> l)
  377.         {
  378.             T[,] ar = new T[l.Count, l.GetLengths().Max()];
  379.             for (int i = 0; i < l.Count; i++)
  380.                 for (int j = 0; j < l[i].Length; j++)
  381.                     ar[i, j] = l[i][j];
  382.             return ar;
  383.         }
  384.     }
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement