/// <summary> /// scan for the first sequence of chars in string after initial whitespace. /// </summary> /// <param name="InString"></param> /// <param name="InWhitespace"></param> /// <param name="InMaxReturnChars"></param> /// <returns></returns> public static ScanStringResults ScanFirstChars( string InString, Whitespace InWhitespace, int InMaxReturnChars) { int ix = 0; ScanStringResults res = null; ScanCharResults cr = Scanner.ScanNotEqual(InString, 0, InWhitespace.WhitespaceChars); if (cr.ResultPos == -1) { ix = 0; } else { ix = cr.ResultPos; } if (ix >= InString.Length) { res = new ScanStringResults(-1); } else { string fsChars = Stringer.SubstringLenient(InString, ix, InMaxReturnChars); res = new ScanStringResults(fsChars, ix); } return(res); }
/// <summary> /// scan for a string which is enclosed by text before and after it. /// </summary> /// <param name="InString"></param> /// <param name="InBx"></param> /// <param name="InPreText"></param> /// <param name="InPostText"></param> /// <returns></returns> public static ScanEnclosedResults ScanEnclosed( string InString, int InBx, string InPreText, string InPostText) { int ex = 0; int lx = 0; int fx = -1; ScanEnclosedResults res = new ScanEnclosedResults( ); char[] ws = new char[] { ' ', '\t' }; // the enclosing pre text. if ((InPreText == null) || (InPreText.Length == 0)) { res.FoundBx = InBx; } else { fx = InString.IndexOf(InPreText, InBx); if (fx >= 0) { res.PreText = InPreText; res.PreBx = fx; res.FoundBx = res.PreBx + res.PreText.Length; } } // advance past whitespace to the actual enclosed text. if (res.FoundBx >= 0) { res.FoundBx = Scanner.ScanNotEqual(InString, res.FoundBx, ws).ResultPos; } // find the enclosing post text. if ((InPostText != null) && (InPostText.Length > 0)) { if (res.FoundBx >= 0) { fx = InString.IndexOf(InPostText, res.FoundBx); if (fx >= 0) { res.PostText = InPostText; res.PostBx = fx; } } } // isolate the actual found text. if (res.FoundBx >= 0) { if (res.PostBx == -1) { ex = InString.Length - 1; } else { ex = res.PostBx - 1; } lx = ex - res.FoundBx + 1; res.FoundText = InString.Substring(res.FoundBx, lx).TrimEnd(ws); } // return the results. return(res); }
/// <summary> /// Examine the string for its WordClassification content. /// </summary> /// <param name="InWord"></param> /// <param name="InTraits"></param> /// <returns></returns> public static CharObjectPair CalcWordClassification( string InWord, TextTraits InTraits) { int Fx = 0, Ix = 0; WordClassification wc = WordClassification.None; char braceChar = ' '; char ch1 = AcCommon.PullChar(InWord, 0); int Ex = InWord.Length - 1; // is quoted. the word runs to the closing quote. if (Scanner.IsOpenQuoteChar(ch1) == true) { Ix = Scanner.ScanCloseQuote(InWord, 0, InTraits.QuoteEncapsulation); if (Ix == Ex) { wc = WordClassification.Quoted; } else { wc = WordClassification.MixedText; } } // check if the string is a Braced or NameBraced word. if (wc == WordClassification.None) { char[] combo = AcCommon.Concat(InTraits.DelimChars, InTraits.BraceChars); Scanner.ScanCharResults results = Scanner.ScanEqual(InWord, 0, combo); Fx = results.ResultPos; ch1 = results.ResultChar; // found a brace char if ((InTraits.IsOpenBraceChar(ch1) == true) && (InTraits.IsDelimChar(ch1) == false)) { Ix = Scanner.ScanCloseBrace(InWord, Fx); if (Ix == Ex) { braceChar = ch1; if (Fx == 0) { wc = WordClassification.Braced; } else { wc = WordClassification.NameBraced; } } } } // word is all delimeter. if (wc == WordClassification.None) { Fx = Scanner.ScanNotEqual(InWord, 0, InTraits.DelimChars).a; if (Fx >= 0) { wc = WordClassification.Delimeter; } } // check if a numeric string. if (wc == WordClassification.None) { char[] digitChars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '+', '-' }; Fx = Scanner.ScanNotEqual(InWord, 0, digitChars).a; if (Fx == -1) { wc = WordClassification.Numeric; try { double vx = double.Parse(InWord); } catch (Exception) { wc = WordClassification.None; } } } // any delim chars in the string. if not, the string is a name. otherwise, it is // mixed. if (wc == WordClassification.None) { Fx = Scanner.ScanEqual(InWord, 0, InTraits.DelimChars).a; if (Fx == -1) { wc = WordClassification.Name; } else { wc = WordClassification.MixedText; } } return(new CharObjectPair(braceChar, wc)); }