//------------------------------------------------------------ // ??? //public static int Balance(this StringView str, int indexOfOpeningBracketOrQuote) { // var literalStatus = new LiteralStatus(); // for (int i = indexOfOpeningBracketOrQuote; i != str.Length; ++i) { // if (str[i].IsBracket()) { // literalStatus.PushBracket(str[i].GetBracketType()); // if (literalStatus.Effective) { // return i + 1; // } // } // else if (str[i].IsQuote()) { // literalStatus.PushQuote(str[i].GetQuoteType()); // if (literalStatus.Effective) { // return i + 1; // } // } // } // return str.Length; //} //============================================================ public static StringView StripBoundaryWhitespaces(this StringView str) { int p = str.GetFirstSpaceBoundary(); int n = str.GetLastSpaceBoundary(); return(str.GetSubView(p, n)); }
//============================================================ public static IEnumerable <StringView> GetWords(this StringView str, Predicate <char> whitespacePred) { int p = str.GetFirstSpaceBoundary(); int n = str.GetLastSpaceBoundary(); // if all are whitespaces if (p > n) { yield break; } for (int i = p; i != n; ++i) { if (str[i] == ' ') { yield return(str.GetSubView(p, i)); p = i = str.FindFirst((char c) => c != ' ', i); } } yield return(str.GetSubView(p, n)); }
// delimitiers: ' " ( [ { ) ] } space // two continuous splitter can form a word boundary without presence of space // result of Bracketed or Quoted may not balanced or matched public static IEnumerable <(BalancedTermType termType, StringView termStr)> EachTerm(this StringView str) { int n = str.Length; int p, i; i = p = str.NextNonSpace(0); while (i != n) { char c = str[i]; if (c == ' ') { yield return(BalancedTermType.Spaced, str.GetSubView(p, i)); i = p = str.NextNonSpace(i); } else if (c.IsOpeningBracket()) { if (p != i) { yield return(BalancedTermType.Spaced, str.GetSubView(p, i)); p = i; } if ((i = str.BalanceBracket(i)) == str.Length) { throw new Exception("bracket are not balanced"); } yield return(BalancedTermType.Bracketed, str.GetSubView(p, i + 1)); i = p = str.NextNonSpace(i + 1); } else if (c.IsQuote()) { if (p != i) { yield return(BalancedTermType.Spaced, str.GetSubView(p, i)); p = i; } if ((i = str.BalanceQuote(i)) == str.Length) { throw new Exception("quote are not balanced"); } yield return(BalancedTermType.Quoted, str.GetSubView(p, i + 1)); i = p = str.NextNonSpace(i + 1); } else { ++i; } } if (i != p) { yield return(BalancedTermType.Spaced, str.GetSubView(p, i)); } }
public static StringView WithoutOutermostSpaces(this StringView str) { return(str.GetSubView(str.GetFirstSpaceBoundary(), str.GetLastSpaceBoundary())); }