//============================================================ private static int SeekWithoutClamping(this StringView str, int start, SeekMode mode, Ward1D ward) { if (mode == SeekMode.MoveOneChar) { return(start + ward.GetOffset()); } else if (mode == SeekMode.UntilNewline) { if (ward == Ward1D.Backward) { return(str.FindLast('\n', start + 1)); } else if (ward == Ward1D.Forward) { return(str.FindFirst('\n', start)); } } else { Debug.Assert((mode & (SeekMode.LiterallySpaced | SeekMode.PassingQuotation | SeekMode.BracketBalanced)) != 0); if (ward == Ward1D.Backward) { return(str.SeekToSpaceBalanced(start, mode, -1, -1) - 1); } else if (ward == Ward1D.Forward) { return(str.SeekToSpaceBalanced(start + 1, mode, 1, 0)); } } throw new Exception(); }
//============================================================ 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)); }
//============================================================ public static int GetFirstSpaceBoundary(this StringView str) { //return str.FindFirst(ChrCategory.WhitespacePredicate); return(str.FindFirst(ChrCategory.WhitespacePredicate.InvertCondition())); }
public static int FindFirst(this StringView str, char c, int start = 0) { return(str.FindFirst((char chr) => chr == c, start)); }