Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using static System.Console;
- using static System.IO.File;
- namespace ArrayBracketPositioning
- {
- public static class Program
- {
- public static readonly string[] CSharpKeywords = { "bool", "int", "double", "short", "long", "decimal", "string", "uint", "ulong", "ushort", "byte", "sbyte", "char" };
- public static string fileName;
- public static string[] lines;
- public static void Main(string[] args)
- {
- WriteLineWithColor("Please enter the name of the file to convert the array bracket appearance from C# to C++.", ConsoleColor.Yellow);
- ForegroundColor = ConsoleColor.Cyan;
- fileName = ReadLine();
- lines = ReadAllLines(fileName);
- for (int i = 0; i < lines.Length; i++)
- {
- if (lines[i].Length > 0)
- {
- int indent = 0;
- int offset = 0;
- while (lines[i][offset] == ' ' || lines[i][offset] == '\t')
- {
- if (lines[i][offset] == ' ')
- indent++;
- else if (lines[i][offset] == '\t')
- indent += 4;
- offset++;
- }
- lines[i] = lines[i].Substring(offset);
- string[] words = lines[i].Split(' ');
- //bool isInQuotes = false;
- for (int j = 0; j < words.Length - 1; j++)
- {
- //if (!isInQuotes)
- //{
- string word = words[j].Split('(').Last();
- if (!words[j].StartsWith("//"))
- {
- if (word.EndsWith("[]"))
- if (CSharpKeywords.Contains(word.Remove(word.Length - 2)))
- {
- words[j] = words[j].Remove(words[j].Length - 2);
- string[] parameter = words[j + 1].Split('=');
- int indexToInsert = parameter[0].Length;
- while (parameter[0][indexToInsert - 1] == ',')
- indexToInsert--;
- parameter[0] = parameter[0].Insert(indexToInsert, "[]");
- words[j + 1] = parameter.Combine("=");
- j++;
- }
- //for (int k = 0; k < words[j].Length; k++)
- // if (words[j][k] == '"')
- // if ((k > 0 && words[j][k - 1] != '\\') || k == 0)
- // isInQuotes = !isInQuotes;
- }
- else break;
- //}
- //else
- // for (int k = 0; k < words[j].Length; k++)
- // if (words[j][k] == '"')
- // if ((k > 0 && words[j][k - 1] != '\\') || k == 0)
- // isInQuotes = !isInQuotes;
- }
- StringBuilder s = new StringBuilder();
- for (int j = 0; j < indent; j++)
- s.Append(" ");
- s.Append(words.Combine(" "));
- lines[i] = s.ToString();
- }
- }
- WriteAllLines(fileName, lines);
- WriteLineWithColor("\nThe array brackets have been successfully converted from C# to C++.", ConsoleColor.Green);
- ReadKey();
- }
- public static void WriteWithColor(string s, ConsoleColor c)
- {
- ForegroundColor = c;
- Write(s);
- }
- public static void WriteLineWithColor(string s, ConsoleColor c)
- {
- ForegroundColor = c;
- WriteLine(s);
- }
- }
- public static class Extensions
- {
- public static int Find(this string s, string match)
- {
- for (int i = 0; i <= s.Length - match.Length; i++)
- {
- string sub = s.Substring(i, match.Length);
- if (sub == match)
- return i;
- }
- return -1;
- }
- public static int Find(this string s, string match, int occurence)
- {
- int occurences = 0;
- for (int i = 0; i <= s.Length - match.Length; i++)
- {
- string sub = s.Substring(i, match.Length);
- if (sub == match)
- {
- occurences++;
- if (occurences == occurence)
- return i;
- }
- }
- return -1;
- }
- public static int Find(this string s, string match, int start, int end)
- {
- for (int i = start; i <= end - match.Length; i++)
- {
- string sub = s.Substring(i, match.Length);
- if (sub == match)
- return i;
- }
- return -1;
- }
- public static int GetLastNumber(this string s)
- {
- int n = 0;
- bool isLastNumber = false;
- for (int i = 0; i < s.Length; i++)
- {
- isLastNumber = int.TryParse(s.Substring(i), out n);
- if (isLastNumber)
- return n;
- }
- throw new ArgumentException("The string has no number in the end.");
- }
- public static int[] FindAll(this string s, string match)
- {
- List<int> indices = new List<int>();
- for (int i = 0; i <= s.Length - match.Length; i++)
- {
- string sub = s.Substring(i, match.Length);
- if (sub == match)
- indices.Add(i);
- }
- return indices.ToArray();
- }
- public static int[] FindAll(this string s, string match, int start, int end)
- {
- List<int> indices = new List<int>();
- for (int i = start; i <= end - match.Length; i++)
- {
- string sub = s.Substring(i, match.Length);
- if (sub == match)
- indices.Add(i);
- }
- return indices.ToArray();
- }
- public static string FixBase64String(this string s)
- {
- int lastNumber = 0;
- int lastInvalidCharacter = 0;
- bool continueChecking = true;
- while (continueChecking)
- {
- if (s[s.Length - lastNumber - 1].IsNumber())
- lastNumber++;
- else if (!s[s.Length - lastNumber - 1].IsBase64Character())
- lastInvalidCharacter = ++lastNumber;
- else
- continueChecking = false;
- }
- s = s.Substring(0, s.Length - lastInvalidCharacter);
- while (s.Length % 4 != 0)
- s += "=";
- return s;
- }
- public static string Substring(this string s, string from, string to)
- {
- int startIndex = s.Find(from) + from.Length;
- int endIndex = s.Find(to);
- int length = endIndex - startIndex;
- return s.Substring(startIndex, length);
- }
- public static string RemoveLastNumber(this string s)
- {
- string l = s;
- for (int i = 0; i < s.Length; i++)
- {
- l = s.Substring(i);
- if (int.TryParse(l, out int shit))
- return s.Substring(0, i);
- }
- return s;
- }
- public static string Replace(this string originalString, string stringToReplaceWith, int startIndex, int length)
- {
- string result = originalString;
- result = result.Remove(startIndex, length);
- result = result.Insert(startIndex, stringToReplaceWith);
- return result;
- }
- public static string ReplaceWholeWord(this string originalString, string oldString, string newString)
- {
- for (int i = originalString.Length - oldString.Length; i >= 0; i--)
- {
- if (originalString.Substring(i, oldString.Length) == oldString)
- if ((i > 0 ? (!originalString[i - 1].IsLetterOrNumber()) : true) && (i < originalString.Length - oldString.Length ? (!originalString[i + oldString.Length].IsLetterOrNumber()) : true))
- {
- originalString = originalString.Replace(newString, i, oldString.Length);
- i -= oldString.Length;
- }
- }
- return originalString;
- }
- public static bool ContainsWholeWord(this string s, string match)
- {
- for (int i = s.Length - match.Length; i >= 0; i--)
- {
- if (s.Substring(i, match.Length) == match)
- if ((i > 0 ? (!s[i - 1].IsLetterOrNumber()) : true) && (i < s.Length - match.Length ? (!s[i + match.Length].IsLetterOrNumber()) : true))
- return true;
- }
- return false;
- }
- public static bool MatchesStringCaseFree(this string s, string match)
- {
- if (s.Length != match.Length)
- return false;
- for (int i = 0; i < s.Length; i++)
- {
- if (s[i].IsUpperCaseLetter())
- if (s[i] + 32 != match[i] && s[i] != match[i])
- return false;
- else if (s[i].IsLowerCaseLetter())
- if (s[i] - 32 != match[i] && s[i] != match[i])
- return false;
- }
- return true;
- }
- public static bool IsBase64Character(this char c)
- {
- return IsNumber(c) || IsLetter(c) || c == '/' || c == '+' || c == '=';
- }
- public static bool IsLetter(this char c) => (c >= 65 && c <= 90 || c >= 97 && c <= 122);
- public static bool IsLowerCaseLetter(this char c) => (c >= 97 && c <= 122);
- public static bool IsNumber(this char c) => (c >= 48 && c <= 57);
- public static bool IsUpperCaseLetter(this char c) => (c >= 65 && c <= 90);
- public static bool IsLetterOrNumber(this char c) => IsLetter(c) || IsNumber(c);
- public static int[] FindOccurences(this string[] a, string match)
- {
- if (a != null)
- {
- List<int> occurences = new List<int>();
- for (int i = 0; i < a.Length; i++)
- if (a[i] == match)
- occurences.Add(i);
- return occurences.ToArray();
- }
- else return new int[0];
- }
- public static string[] Replace(this string[] a, char oldChar, char newChar)
- {
- for (int i = 0; i < a.Length; i++)
- a[i] = a[i].Replace(oldChar, newChar);
- return a;
- }
- public static string[] Replace(this string[] a, string oldString, string newString)
- {
- for (int i = 0; i < a.Length; i++)
- a[i] = a[i].Replace(oldString, newString);
- return a;
- }
- public static string[] ReplaceWholeWord(this string[] a, string oldString, string newString)
- {
- for (int i = 0; i < a.Length; i++)
- a[i] = a[i].ReplaceWholeWord(oldString, newString);
- return a;
- }
- public static string[] RemoveEmptyElements(this string[] a)
- {
- List<string> result = new List<string>();
- for (int i = 0; i < a.Length; i++)
- if (a[i].Length > 0)
- result.Add(a[i]);
- return result.ToArray();
- }
- public static bool ContainsAtLeast(this string[] a, string match)
- {
- if (a != null)
- {
- for (int i = 0; i < a.Length; i++)
- if (a[i].Contains(match))
- return true;
- return false;
- }
- return false;
- }
- public static bool ContainsAtLeastWholeWord(this string[] a, string match)
- {
- if (a != null)
- {
- for (int i = 0; i < a.Length; i++)
- if (a[i].ContainsWholeWord(match))
- return true;
- return false;
- }
- return false;
- }
- public static string Combine(this string[] s)
- {
- StringBuilder str = new StringBuilder();
- for (int i = 0; i < s.Length; i++)
- str = str.Append(s[i]);
- return str.ToString();
- }
- public static string Combine(this string[] s, string separator)
- {
- if (s.Length > 0)
- {
- StringBuilder str = new StringBuilder();
- str = str.Append(s[0]);
- for (int i = 1; i < s.Length; i++)
- str = str.Append(separator + s[i]);
- return str.ToString();
- }
- else return "";
- }
- public static string[,] Split(this string[] s, char separator)
- {
- List<string[]> separated = new List<string[]>();
- for (int i = 0; i < s.Length; i++)
- separated.Add(s[i].Split(separator));
- return separated.ToTwoDimensionalArray();
- }
- public static T[] GetInnerArray<T>(this T[,] ar, int innerArrayIndex)
- {
- T[] innerAr = new T[ar.GetLength(1)];
- for (int i = 0; i < innerAr.Length; i++)
- innerAr[i] = ar[innerArrayIndex, i];
- return innerAr;
- }
- public static int[] GetLengths<T>(this List<T[]> l)
- {
- int[] lengths = new int[l.Count];
- for (int i = 0; i < l.Count; i++)
- lengths[i] = l[i].Length;
- return lengths;
- }
- public static int[] GetLengths(this int[,] ar)
- {
- int[] lengths = new int[ar.Length];
- for (int i = 0; i < ar.Length; i++)
- lengths[i] = ar.GetInnerArray(i).Length;
- return lengths;
- }
- public static int[] GetLengths(this string[,] ar)
- {
- int[] lengths = new int[ar.GetLength(0)];
- for (int i = 0; i < lengths.Length; i++)
- lengths[i] = ar.GetInnerArray(i).Length;
- return lengths;
- }
- public static T[,] ToTwoDimensionalArray<T>(this List<T[]> l)
- {
- T[,] ar = new T[l.Count, l.GetLengths().Max()];
- for (int i = 0; i < l.Count; i++)
- for (int j = 0; j < l[i].Length; j++)
- ar[i, j] = l[i][j];
- return ar;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement