// ------------------------- Enquote ------------------------------ public static string Enquote( string InValue, char InQuoteChar, QuoteEncapsulation InQem) { int Lx = InValue.Length; int Sx = (Lx * 2) + 2; StringBuilder sb = new StringBuilder(Sx); sb.Append(InQuoteChar); for (int Ix = 0; Ix < Lx; ++Ix) { char ch1 = InValue[Ix]; if (ch1 == InQuoteChar) { if (InQem == QuoteEncapsulation.Double) { sb.Append(ch1); sb.Append(ch1); } else { sb.Append(@"\"); sb.Append(ch1); } } else { sb.Append(ch1); } } sb.Append(InQuoteChar); return(sb.ToString( )); }
public TextTraits(TextTraits Traits) : this() { mWhitespacePatterns.Replace(Traits.mWhitespacePatterns); mNewLinePatterns.Replace(Traits.mNewLinePatterns); mDividerPatterns.Replace(Traits.mDividerPatterns); this.DividerDelimClasses = Traits.DividerDelimClasses; mCommentToEndPatterns.Replace(Traits.mCommentToEndPatterns); mQuotePatterns.Replace(Traits.mQuotePatterns); this.VerbatimLiteralPattern = Traits.VerbatimLiteralPattern; mExpressionPatterns.Replace(Traits.mExpressionPatterns); mEndStmtPatterns.Replace(Traits.mEndStmtPatterns); mOpenNamedBracedPatterns.Replace(Traits.mOpenNamedBracedPatterns); mOpenContentBracedPatterns.Replace(Traits.mOpenContentBracedPatterns); mOpenBracedPatterns = null; mCloseBracedPatterns = null; mPathSepPatterns.Replace(Traits.mPathSepPatterns); mNonWordPatterns = null; mQem = Traits.mQem; mFormSentencesFromWhitespaceDelimWords = Traits.mFormSentencesFromWhitespaceDelimWords; }
// ---------------- DecodeHeaderString_QuotedOnly -------------------------- // decode the header string. If it is quoted, dequote it. Otherwise, return as is. public static string DecodeHeaderString_QuotedOnly(string InString) { QuoteEncapsulation qem = QuoteEncapsulation.Escape; string results = null; if (Stringer.IsQuoted(InString, qem) == true) { results = Stringer.Dequote(InString, qem); } else { results = InString; } return(results); }
// ---------------- DecodeHeaderString_QuotedEncodedEither -------------------------- // decode the string that can be either quoted or contain encoded-words. public static string DecodeHeaderString_QuotedEncodedEither(string InString) { string results = null; QuoteEncapsulation qem = QuoteEncapsulation.Escape; if (Stringer.IsQuoted(InString, qem) == true) { results = DecodeHeaderString_QuotedOnly(InString); } else { results = DecodeHeaderString_EncodedOnly(InString); } return(results); }
// --------------------------- Dequote ------------------------------ public static string Dequote(string InText, QuoteEncapsulation InQem) { int Lx = InText.Length + 2; // 2=quote chars. char QuoteChar = InText[0]; StringBuilder sb = new StringBuilder(Lx); int Ix = 0; int EndIx = InText.Length - 2; while (true) { ++Ix; if (Ix > EndIx) { break; } char ch1 = InText[Ix]; // using the escape method to enclose quote chars. This means the escape char // is used to encapsulate other special characters in the quoted string. // todo: dequote using "QuotedStringTraits" rules. if ((ch1 == '\\') && (InQem == QuoteEncapsulation.Escape) && (Ix < (EndIx - 1))) { sb.Append(MaterializeEscapeChar(InText, Ix)); ++Ix; } // quote char enquoted using the "double the char" method. else if ((ch1 == QuoteChar) && (InQem == QuoteEncapsulation.Double) && (AcCommon.PullChar(InText, Ix + 1) == QuoteChar)) { sb.Append(ch1); ++Ix; } // any other character. append to result string. else { sb.Append(ch1); } } return(sb.ToString( )); }
/// <summary> /// Scan the bounded string for any of the pattern characters, /// bypassing quoted strings within the scan space. /// </summary> /// <param name="InBounded"></param> /// <param name="InBx"></param> /// <param name="InPatternChars"></param> /// <param name="InQem"></param> /// <returns></returns> public static ScanCharResults ScanEqualAny_BypassQuoted( BoundedString InBounded, int InBx, char[] InPatternChars, QuoteEncapsulation InQem) { ScanCharResults res; int Fx; char[] patternChars = Arrayer.Concat <char>(InPatternChars, new char[] { '"', '\'' }); int Ix = InBx; while (true) { res = ScanEqualAny(InBounded, Ix, patternChars); if (res.ResultPos == -1) { break; } else if (IsOpenQuoteChar(res.ResultChar) == true) { Fx = ScanCloseQuote(InBounded, res.ResultPos, InQem); if (Fx == -1) { throw new ApplicationException("End of quoted string not found"); } else if (Fx == InBounded.Ex) { res = new ScanCharResults(-1); break; } else { Ix = Fx + 1; } } else { break; } } return(res); }
// ------------------------ ScanCloseBrace ------------------------- // todo: pass TextTraits to a version of this method. Use recursion for each brace // char found. /// <summary> /// scan for closing brace char that matches the start at open brace char. /// </summary> /// <param name="InString"></param> /// <param name="InBx">scan start position</param> /// <param name="InEx">end position in string</param> /// <param name="InQem"></param> /// <returns></returns> public static int ScanCloseBrace( string InString, int InBx, int InEx, QuoteEncapsulation InQem) { char openBraceChar = InString[InBx]; char closeBraceChar = AcCommon.CalcCloseBraceChar(openBraceChar); int Ix = InBx, Fx = 0; int ParenLevel = 1; if (InEx >= InString.Length) { throw new ApplicationException("ScanCloseBrace end pos exceeds string length"); } while (true) { ++Ix; if (Ix > InEx) { Ix = -1; break; } char ch1 = InString[Ix]; if (ch1 == openBraceChar) { ++ParenLevel; } else if (ch1 == closeBraceChar) { --ParenLevel; if (ParenLevel == 0) { break; } } else if (IsOpenQuoteChar(ch1) == true) { Fx = ScanCloseQuote(InString, Ix, InQem); Ix = Fx; } } return(Ix); }
// --------------------------- ScanCloseQuote ------------------------------ public static int ScanCloseQuote( BoundedString InBounded, int InBx, QuoteEncapsulation InQem) { char QuoteChar = InBounded.String[InBx]; int cloqIx = -1; for (int Ix = InBx + 1; Ix <= InBounded.Ex; ++Ix) { char ch1 = InBounded.String[Ix]; // using the escape method to enclose quote chars. This means the escape char // is used to encapsulate other special characters in the quoted string. // todo: dequote using "QuotedStringTraits" rules. if ((ch1 == '\\') && (InQem == QuoteEncapsulation.Escape) && (Ix < InBounded.Ex)) { ++Ix; } // quote char enquoted using the "double the char" method. else if ( (ch1 == QuoteChar) && (InQem == QuoteEncapsulation.Double) && (Ix < InBounded.Ex) && (InBounded.String[Ix + 1] == QuoteChar)) { ++Ix; } // found the closing quote char. else if (ch1 == QuoteChar) { cloqIx = Ix; break; } } return(cloqIx); }
// ------------------------ ScanCloseBrace ------------------------- // todo: pass TextTraits to a version of this method. Use recursion for each brace // char found. public static int ScanCloseBrace( string InString, int InBx, QuoteEncapsulation InQem) { char openBraceChar = InString[InBx]; char closeBraceChar = AcCommon.CalcCloseBraceChar(openBraceChar); int Ix = InBx, Fx = 0; int Lx = InString.Length; int ParenLevel = 1; while (true) { ++Ix; if (Ix >= Lx) { throw(new TextException( "ScanCloseBrace", "Close brace not found.")); } char ch1 = InString[Ix]; if (ch1 == openBraceChar) { ++ParenLevel; } else if (ch1 == closeBraceChar) { --ParenLevel; if (ParenLevel == 0) { break; } } else if (IsOpenQuoteChar(ch1) == true) { Fx = ScanCloseQuote(InString, Ix, InQem); Ix = Fx; } } return(Ix); }
// --------------------------- IsQuoted -------------------------------- // is the string enclosed in quotes. the quoted string must be properly formed. // 1st char is quote, last char is closing quote, and any enclosed quotes are // properly encapsulated as per the QuoteEncapsulationMethod. public static bool IsQuoted(string InString, QuoteEncapsulation InQem) { if (InString.Length < 2) { return(false); } char ch1 = InString[0]; if ((ch1 != '"') && (ch1 != '\'')) { return(false); } int Fx = Scanner.ScanCloseQuote(InString, 0, InQem); if ((Fx != -1) && (Fx == InString.Length - 1)) { return(true); } else { return(false); } }
public TextTraits SetQuoteEncapsulation(QuoteEncapsulation Value) { mQem = Value; return(this); }
public static string DoubleEnquote(this string Value, QuoteEncapsulation QuoteEncapsulation) { string s1 = Stringer.Enquote(Value, '\"', QuoteEncapsulation); return(s1); }
/// <summary> /// scan for closing brace char that matches the start at open brace char. /// </summary> /// <param name="InString"></param> /// <param name="InBx"></param> /// <param name="InQem"></param> /// <returns></returns> public static int ScanCloseBrace( string InString, int InBx, QuoteEncapsulation InQem) { return(ScanCloseBrace(InString, InBx, InString.Length - 1, InQem)); }