/// <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); }
public WordValue(string InValue, TextTraits InTraits) { CharObjectPair results = Stringer.CalcWordClassification(InValue, InTraits); CommonConstruct( InValue, (WordClassification)results.b, InTraits, results.a); }
/// <summary> /// add a value to the end of the string. /// </summary> /// <param name="InValue"></param> /// <returns></returns> public void Append(string InValue) { // prepare the value to be added to the CSV string. string prepValue; if (InValue == null) { InValue = ""; } if ((ShouldQuoteValue(InValue) == true) || (InValue.Length == 0)) { prepValue = Stringer.Enquote(InValue, cQuoteChar, mTraits.QuoteEncapsulation); if (EncapsulateValues == true) { prepValue = "_qv(" + prepValue + ")"; } } else { prepValue = InValue; } // add the value to the string. AppendAsIs(prepValue); }
/// <summary> /// add a value to the end of the string. /// </summary> /// <param name="InValue"></param> /// <returns></returns> public CsvString Add(string InValue) { // prepare the value to be added to the CSV string. string prepValue; if (InValue == null) { prepValue = ""; } else if ((ShouldQuoteValue(InValue) == true) || (InValue.Length == 0)) { prepValue = Stringer.Enquote(InValue, '"', QuoteEncapsulation.Escape); if (EncapsulateValues == true) { prepValue = "_qv(" + prepValue + ")"; } } else { prepValue = InValue; } // add the value to the string. AddString(prepValue); return(this); }
public bool IsWhitespace(string InString, int InIx, int InEx) { if (Stringer.CompareSubstringEqualAny( InString, InIx, InEx, WhitespacePatterns.StringArray) != -1) { return(true); } else { return(false); } }
/// <summary> /// Return an object holding the CsvString value. /// Object can be a string, CsvString or string[]. /// </summary> /// <returns></returns> public object ToObject( ) { if (IsAtValue == false) { return(null); } else if (mAtFinalEmptyValue == true) { return(null); } else if (mCsr.Word == null) { return(null); } else if (mCsr.Word.Class == WordClassification.NamedBraced) { string braceName = mCsr.Word.BraceName; if (braceName == "_qv") { string text = mCsr.Word.BracedText; return(Stringer.Dequote(text, mCsr.TextTraits.QuoteEncapsulation)); } // a string[] is stored as a sequence of strings in CsvString form. else if (braceName == "_sa") // string array { CsvString vlu = new CsvString( ); vlu.String = mCsr.Word.BracedText; return(vlu.ToStringArray( )); } // is a string of comma seperated values. else if (braceName == "_csv") { CsvString vlu = new CsvString( ); vlu.String = mCsr.Word.BracedText; return(vlu); } else { return(mCsr.Word.BracedText); } } else { return(mCsr.Word.ToString( )); } }
// --------------------------- ScanReverseNotEqual------------------------------- // scan reverse for the first location in string not equal to all of the // pattern characters. public static int ScanReverseNotEqual( string InString, int InBx, int InEx, int InIx, ScanPatterns InNotEqualPatterns) { int foundIx = InIx + 1; while (true) { foundIx -= 1; if (foundIx < InBx) { foundIx = -1; break; } int remLx = InEx - foundIx + 1; char ch1 = InString[foundIx]; // the current char is not equal any of the pattern lead chars. int patIx = Array.IndexOf <char>(InNotEqualPatterns.LeadChars, ch1); if (patIx == -1) { break; } // lead char matches. check the entire pattern string for equality. ScanPattern equalPat = null; ScanPattern pat = InNotEqualPatterns.ScanPatternsArray[patIx]; while (pat != null) { bool rv = Stringer.CompareSubstringEqual(InString, foundIx, InEx, pat.PatternValue); if (rv == true) { equalPat = pat; break; } pat = pat.NextSameLeadChar; } // none of the patterns fully match the substring at the current location. if (equalPat == null) { break; } } return(foundIx); }
/// <summary> /// Return the encapsulated value in string form. /// </summary> /// <returns></returns> public override string ToString( ) { if (IsAtValue == false) { return(null); } else if (mAtFinalEmptyValue == true) { return(null); } else if (mCsr.Word == null) { return(""); } else if (mCsr.Word.Class == WordClassification.NamedBraced) { string braceName = mCsr.Word.BraceName; if (braceName == "_qv") { string text = mCsr.Word.BracedText; return(Stringer.Dequote(text, mCsr.TextTraits.QuoteEncapsulation)); } else { return(mCsr.Word.BracedText); } } else if (mCsr.Word.IsQuoted == true) { return(mCsr.Word.DequotedValue); } else { return(mCsr.Word.Value); } }
// -------------------- ScanWord_IsolateWord --------------------------- // We have a word starting at InBx. Scan to the end of the word. // Returns the word in the InOutResults parm. // Returns the word delim in the return argument. private static ScanPatternResults ScanWord_IsolateWord( BoundedString InBoundedString, int InBx, ref WordCursor InOutResults, TextTraits Traits) { int Bx, Ix, Lx; string wordText; ScanPatternResults spr = null; Bx = InBx; char ch1 = InBoundedString.String[Bx]; // is start of a verbatim string literal if ((Traits.VerbatimLiteralPattern != null) && (Traits.VerbatimLiteralPattern.Match(InBoundedString, Bx))) { } // is quoted. the word runs to the closing quote. else if (IsOpenQuoteChar(ch1) == true) { Ix = ScanCloseQuote(InBoundedString.String, Bx, Traits.QuoteEncapsulation); if ((Ix == -1) || (Ix > InBoundedString.Ex)) { throw (new ApplicationException( "Closing quote not found starting at position " + Bx.ToString() + " in " + InBoundedString.String)); } Lx = Ix - Bx + 1; wordText = InBoundedString.String.Substring(Bx, Lx); InOutResults.SetWord(wordText, WordClassification.Quoted, Bx); // setup the non word which follows the closing quote. Bx = Ix + 1; if (InBoundedString.IsOutsideBounds(Bx)) { spr = new ScanPatternResults(-1); } else { // the char that follows the closing quote must be a delim spr = ScanEqualAny(InBoundedString, Bx, Traits.NonWordPatterns); if (spr.FoundPos != Bx) { throw new ApplicationException( "invalid char follows close quote at pos " + Ix.ToString() + " in " + Stringer.Head(InBoundedString.String, 80)); } } } else { // Scan the string for any of the non word patterns spcfd in Traits. DelimClassification sprdc = DelimClassification.None; spr = ScanEqualAny(InBoundedString, Bx, Traits.NonWordPatterns); if (spr.IsNotFound == false) { sprdc = spr.FoundPat.DelimClassification; } // a quote character within the name. this is an error. if (sprdc == DelimClassification.Quote) { throw new ApplicationException( "quote character immed follows name character at position " + spr.FoundPos.ToString() + " in " + InBoundedString.String); } // no delim found. all word to the end of the string. else if (spr.IsNotFound) { wordText = InBoundedString.Substring(Bx); InOutResults.SetWord(wordText, WordClassification.Identifier, InBx); } // found an open named brace char else if (sprdc == DelimClassification.OpenNamedBraced) { ScanWord_IsolateWord_Braced( InBoundedString, Bx, spr, ref InOutResults, Traits); } // delim is same position as the word. so there is no word, only a delim. else if (spr.FoundPos == InBx) { if (Scanner.IsOpenBraced(sprdc)) { InOutResults.SetWord( spr.FoundPat.PatternValue, WordClassification.OpenContentBraced, Bx, spr.FoundPat.LeadChar); } // start of CommentToEnd comment. This is a word, not a delim. Find the // end of the comment and set the delim to that end position. else if (sprdc == DelimClassification.CommentToEnd) { spr = ScanWord_IsolateWord_CommentToEnd( InBoundedString, spr.FoundPos, ref InOutResults, Traits); } else { InOutResults.SetNullWord(); } } // we have a word that ends with a delim. else { Lx = spr.FoundPos - InBx; wordText = InBoundedString.Substring(InBx, Lx); InOutResults.SetWord(wordText, WordClassification.Identifier, InBx); } } // return ScanPatternResults of the delim that ends the word. return(spr); }
// -------------------------------- ScanNotEqual ------------------------ public static ScanPatternResults ScanNotEqual( string InString, int InIx, int InEx, ScanPatterns InScanPatterns) { // step thru the string 1 char at a time. int stringIx = InIx; while (true) { if (stringIx > InEx) { stringIx = -1; break; } char ch1 = InString[stringIx]; // the current char is not equal any of the pattern lead chars. int patIx = Array.IndexOf <char>(InScanPatterns.LeadChars, ch1); if (patIx == -1) { break; } ScanPattern equalPat = null; ScanPattern pat = InScanPatterns.ScanPatternsArray[patIx]; while (pat != null) { bool rv = Stringer.CompareSubstringEqual(InString, stringIx, InEx, pat.PatternValue); if (rv == true) { if (equalPat == null) { equalPat = pat; } // Matching pattern already found, but this pattern also matches and it is // longer. Always return the longer pattern. else if (pat.PatternValue.Length > equalPat.PatternValue.Length) { equalPat = pat; } } pat = pat.NextSameLeadChar; } // check for the substring at the current location in string as not equal any // of the ScanNotEqual pattern strings. if (equalPat == null) { break; } // advance past the whitespace string. stringIx += equalPat.PatternValue.Length; } // return the scan results ScanPatternResults spr = null; if (stringIx == -1) { spr = new ScanPatternResults(-1); } else { spr = new ScanPatternResults(stringIx, InString[stringIx]); } spr.ScannedString = InString; spr.ScanStartIx = InIx; spr.ScanBoundsEx = InEx; return(spr); }