public static int LastIndexOf(this CharSpan chars, char ch) { int i = chars.Length - 1; while (i >= 0 && chars[i] != ch) { i--; } return(i); }
public static CharSpan TrimLeft(this CharSpan chars) { int skip = 0; while (skip < chars.Length && char.IsWhiteSpace(chars[skip])) { skip++; } return(chars.Substring(skip)); }
public static CharSpan TrimRight(this CharSpan chars) { int length = chars.Length; while (length != 0 && char.IsWhiteSpace(chars[length - 1])) { length--; } return(chars.Substring(0, length)); }
/// <summary> /// Finds last whitespace character and returns substring that follows. /// </summary> public static CharSpan LastWord(this CharSpan chars) { for (int i = chars.Length - 1; i >= 0; i--) { if (char.IsWhiteSpace(chars[i])) { return(Substring(chars, i + 1)); } } return(chars); }
public static CharSpan Substring(this CharSpan chars, int index, int length) { if (length == 0) { return(CharSpan.Empty); } if (length > chars.Length - index) { throw new ArgumentOutOfRangeException(); } return(new CharSpan(chars.Buffer, chars.Start + index, length)); }
public static bool StartsWith(this CharSpan chars, string text) { if (text.Length > chars.Length) { return(false); } for (int i = 0; i < text.Length; i++) { if (text[i] != chars[i]) { return(false); } } return(true); }
public bool Equals(CharSpan other) { if (other != null && other.Length == Length) { for (int i = 0; i < Length; i++) { if (other[i] != this[i]) { return(false); } } return(true); } return(false); }
public static bool EndsWith(this CharSpan chars, CharSpan text) { if (text.Length > chars.Length) { return(false); } for (int i = 0; i < text.Length; i++) { if (text[i] != chars[chars.Length - text.Length + i]) { return(false); } } return(true); }
public static CharSpan Substring(this CharSpan chars, int index) => index == 0 ? chars : index >= chars.Length ? CharSpan.Empty : new CharSpan(chars.Buffer, chars.Start + index, chars.Length - index);
/// <summary> /// Gets last character of the span or '\0' if span is empty. /// </summary> public static char LastChar(this CharSpan chars) { return(chars.Length != 0 ? chars[chars.Length - 1] : '\0'); }
public static CharSpan Trim(this CharSpan chars) => chars.TrimLeft().TrimRight();