private string RepeatChars(string doublingString, string fromString) { var doublingChars = (from char c in fromString where !Separators.Contains(c) select c) .Distinct() .ToList(); StringBuilder stringBuilder = new StringBuilder(); foreach (var c in doublingString) { stringBuilder.Append(c, doublingChars.Contains(c) ? 2 : 1); } return(stringBuilder.ToString()); }
/// <summary> /// Get the start index of the word being with the caret. /// </summary> /// <returns></returns> private int GetWordStartIndex() { int charIndex = SelectionStart; while (charIndex > 0) { if (!Separators.Contains(Text[charIndex - 1])) { charIndex--; } else { break; } } return(charIndex); }
/// <summary> /// Get the end index of the word being with the caret. /// </summary> /// <returns></returns> private int GetWordEndIndex() { int charIndex = SelectionStart; while (charIndex < Text.Length) { if (!Separators.Contains(Text[charIndex])) { charIndex++; } else { break; } } charIndex--; return(charIndex); }
public IEnumerable <char> Decode(string stringToDecode) { if (stringToDecode.Any(n => !Alphabet.Contains(n) && !Separators.Contains(n))) { throw new Exception("Finded symbol not from alphabet"); } string s = string.Empty; for (int i = 0; i < stringToDecode.Length; i++) { if (Separators.Any(n => n == stringToDecode[i])) { s += stringToDecode[i]; } else { s += Alphabet[LookupTable[i % KeyWord.Length].IndexOf(stringToDecode[i])]; } } return(s); }
public override bool CheckOnCloseMarker(char symbol, bool isLastSymbol = false) { if (linkExpected) { if (!Separators.Contains(symbol) && symbol != '(') { return(true); } else if (symbol == '(') { linkExpected = false; } } if (canClose && Separators.Contains(symbol) || symbol.ToString() == CloseMarker && isLastSymbol) { return(true); } canClose = symbol.ToString() == CloseMarker; linkExpected = symbol == ']' || linkExpected; return(false); }
/// <summary> /// Checks to see if a character is any of the known special characters. /// </summary> /// <param name="character"></param> /// <returns>True if the character is special. Otherwize false.</returns> public bool IsSpecial(char character) { return(Separators.Contains(character)); }
public void Execute(List <string> input) { var mode = TokenizerMode.normal; int lineNumber = 0; int charNumber = 0; Results = new List <List <ExoToken> >(); var tokenLine = new List <ExoToken>(); var isLastWordOperator = false; var exitMode = false; string currentWord = string.Empty; // Line Parsing Section for (lineNumber = 0; lineNumber < input.Count; lineNumber++) { // start of new line var currentLine = input[lineNumber]; tokenLine = new List <ExoToken>(); // Character Parsing Section for (charNumber = 0; charNumber < currentLine.Length; charNumber++) { char c = currentLine[charNumber]; DecideCharacterHandling(c); } if (mode == TokenizerMode.quoted) { // ERROR: forgot to close quote!; Debug.WriteLine($"Missing Quotes: {lineNumber}"); } FinishLastWord(); mode = TokenizerMode.normal; isLastWordOperator = false; exitMode = false; Results.Add(tokenLine); } // Character handling logic, abstracted to keep internal loop clean void DecideCharacterHandling(char c) { CheckForModeSwitch(c); if (Separators.Contains(c) && mode == TokenizerMode.normal) { FinishLastWord(); } else { HandleNormalCharacters(c); } } void CheckForModeSwitch(char c) { if (mode == TokenizerMode.comment) { return; } if (c == CommentCharacter && mode != TokenizerMode.quoted) { FinishLastWord(); mode = TokenizerMode.comment; return; } if (c == QuoteCharacter) { if (mode == TokenizerMode.quoted) { exitMode = true; return; } FinishLastWord(); mode = TokenizerMode.quoted; return; } } // handling spaces and tabulators void HandleNormalCharacters(char c) { if (SimpleOperators.Contains(c) && mode == TokenizerMode.normal) { // expand existing operators if (isLastWordOperator) { if (tokenLine.Last().TryExpandOperator(c)) { // work performed in TryExpand return; } } // split and identify new operators FinishLastWord(); currentWord += c; FinishLastWord(); tokenLine.Last().IsOperator = true; isLastWordOperator = true; return; } isLastWordOperator = false; currentWord += c; if (exitMode) { exitMode = false; FinishLastWord(); mode = TokenizerMode.normal; } } // finish the current word and add it to the CURRENT token handling list void FinishLastWord() { if (currentWord == string.Empty) { return; } var token = new ExoToken() { value = currentWord, line = lineNumber, type = mode }; tokenLine.Add(token); currentWord = string.Empty; } }
public void Highlight() { parsing = true; rtfHeader.Length = 0; fontStyles.Clear(); BeginUpdate(); //Save caret and scrollbars position Win32.POINT scrollPosition = GetScrollPos(); int caretPosition = SelectionStart; StringBuilder rtfBody = new StringBuilder(); //Dictionary that saves the index of font/color in font/color table. Dictionary <string, int> fonts = new Dictionary <string, int>(); Dictionary <Color, int> colors = new Dictionary <Color, int>(); //Add RTF header rtfHeader.Append(@"{\rtf1\ansi\deff0"); //Create color table, loaded from color's descriptors. colors = AddColorTable(rtfHeader); //Create font table rtfHeader.Append(@"{\fonttbl "); //Add default font of the textbox. AddFontToTable(rtfHeader, Font, fonts); //Add the defaults font tags to fontStyles.SaveFontStyle(Font); //Parsing text in the rtf body rtfBody.Append(@"\viewkind4\uc1\pard\ltrpar").Append("\n"); //Set default color and font for the text SetDefaultSetting(rtfBody, colors, fonts); separatorArray = separators.ToArray(); //Replace some specified symbols that has meaning in RTF text string[] lines = Text.Replace(@"\", @"\\").Replace("{", @"\{").Replace("}", @"\}").Split('\n'); //Scan every line of input text. for (int lineCounter = 0; lineCounter < lines.Length; lineCounter++) { if (lineCounter != 0) { AddNewLine(rtfBody); } string line = lines[lineCounter]; //Put every word of line to an array. string[] tokens = CaseSensitive ? line.Split(separatorArray) : line.ToUpper().Split(separatorArray); if (tokens.Length == 0) { AddTextToRTFBody(rtfBody, line); AddNewLine(rtfBody); continue; } int tokenCounter = 0; for (int i = 0; i < line.Length;) { char currentChar = line[i]; if (Separators.Contains(currentChar)) { rtfBody.Append(currentChar); i++; } else { if (tokenCounter >= tokens.Length) { break; } string currentToken = tokens[tokenCounter]; tokenCounter++; bool isPlainTextToken = true; foreach (var item in descriptors) { string stringToCompare = CaseSensitive ? item.token : item.token.ToUpper(); bool match = false; //Check if the current token matches any of descriptors according to the DescriptorRecognition property. switch (item.descriptorRecognition) { case DescriptorRecognition.WholeWord: if (stringToCompare == currentToken) { match = true; } break; case DescriptorRecognition.StartsWith: if (currentToken.StartsWith(stringToCompare)) { match = true; } break; case DescriptorRecognition.IsNumber: double number = 0; //To check if it is a real number. if (currentToken.Trim('/') != currentToken) { match = false; break; } string tokenRemovedSlash = currentToken.Replace("/", ""); if (double.TryParse(currentToken, out number) || double.TryParse(tokenRemovedSlash, out number)) { match = true; } break; default: break; } if (!match) { //If doesn't match, continue to check another item in descriptors. continue; } //Found the item of descriptors that matches. isPlainTextToken = false; //Open a "block" that contains the text we are going to add //and its style tags rtfBody.Append("{"); //Apply the font and color for the text. SetDescriptorSetting(rtfBody, item, colors, fonts); string textToFormat = ""; switch (item.highlightType) { //We will find which text to format. case HighlightType.ToEOW: textToFormat = line.Substring(i, currentToken.Length); i += currentToken.Length; break; case HighlightType.ToEOL: //textToFormat = line.Substring(i, line.Length); textToFormat = line.Remove(0, i); i = line.Length; break; case HighlightType.ToCloseToken: { StringBuilder sbOfTextToFormat = new StringBuilder(); //Find the close token int highlightStartFrom = i + item.token.Length; while ((line.IndexOf(item.closeToken, highlightStartFrom) < 0) && (lineCounter < lines.Length)) { //Add text between the boundary of two token. sbOfTextToFormat.Append(line.Remove(0, i)); /*Use for the line containing open token.*/ lineCounter++; if (lineCounter < lines.Length) { //Not the last line. AddNewLine(sbOfTextToFormat); line = lines[lineCounter]; i = highlightStartFrom = 0; } else { i = highlightStartFrom = line.Length; } } bool hasCloseToken = (line.IndexOf(item.closeToken, highlightStartFrom) < 0) ? false : true; if (hasCloseToken) { int closeTokenIndex = line.IndexOf(item.closeToken, highlightStartFrom); sbOfTextToFormat.Append(line.Substring(i, closeTokenIndex + item.closeToken.Length - i)); //Because we might skip some token since add it to text to format. //--> We need to generate the list of tokens in line again. line = line.Remove(0, closeTokenIndex + item.closeToken.Length); tokenCounter = 0; tokens = CaseSensitive ? line.Split(separatorArray) : line.ToUpper().Split(separatorArray); i = 0; } textToFormat = sbOfTextToFormat.ToString(); } break; default: break; } AddTextToRTFBody(rtfBody, textToFormat); //Close the block contains the text formated. rtfBody.Append("}"); break; } //End of foreach(var item in descriptors). if (isPlainTextToken) { AddTextToRTFBody(rtfBody, line.Substring(i, currentToken.Length)); i += currentToken.Length; } } } //End for(int i = 0; i < line.Length;). } //End of for(int lineCounter = 0; lineCounter < lines.Length; lineCounter++) //Close rtf header. rtfHeader.Append("\n}\n"); //Join the rtf header with the rtf body. //Then show it to the text box. string rtfString = rtfHeader.ToString() + rtfBody.ToString(); this.Rtf = rtfString; //Restore caret and scrollbars location. SelectionStart = caretPosition; SelectionLength = 0; SetScrollPos(scrollPosition); this.Focus(); FinishUpdate(); parsing = false; }