/// <summary> /// Gets the index of the first occurrence of any element in the specified array. /// </summary> /// <param name="rope">The target rope.</param> /// <param name="anyOf">Array of characters being searched.</param> /// <param name="startIndex">Start index of the search.</param> /// <param name="length">Length of the area to search.</param> /// <returns>The first index where any character was found; or -1 if no occurrence was found.</returns> public static int IndexOfAny(this Rope <char> rope, char[] anyOf, int startIndex, int length) { if (rope == null) { throw new ArgumentNullException("rope"); } if (anyOf == null) { throw new ArgumentNullException("anyOf"); } rope.VerifyRange(startIndex, length); while (length > 0) { var entry = rope.FindNodeUsingCache(startIndex).PeekOrDefault(); char[] contents = entry.node.contents; int startWithinNode = startIndex - entry.nodeStartIndex; int nodeLength = Math.Min(entry.node.length, startWithinNode + length); for (int i = startIndex - entry.nodeStartIndex; i < nodeLength; i++) { char element = contents[i]; foreach (char needle in anyOf) { if (element == needle) { return(entry.nodeStartIndex + i); } } } length -= nodeLength - startWithinNode; startIndex = entry.nodeStartIndex + nodeLength; } return(-1); }
/// <summary> /// Retrieves the text for a portion of the rope and writes it to the specified text writer. /// Runs in O(lg N + M), where M=<paramref name="length"/>. /// </summary> /// <exception cref="ArgumentOutOfRangeException">offset or length is outside the valid range.</exception> /// <remarks> /// This method counts as a read access and may be called concurrently to other read accesses. /// </remarks> public static void WriteTo(this Rope <char> rope, TextWriter output, int startIndex, int length) { if (rope == null) { throw new ArgumentNullException("rope"); } if (output == null) { throw new ArgumentNullException("output"); } rope.VerifyRange(startIndex, length); rope.root.WriteTo(startIndex, output, length); }
/// <summary> /// Gets the index of the last occurrence of the search text. /// </summary> public static int LastIndexOf(this Rope <char> rope, string searchText, int startIndex, int length, StringComparison comparisonType) { if (rope == null) { throw new ArgumentNullException("rope"); } if (searchText == null) { throw new ArgumentNullException("searchText"); } rope.VerifyRange(startIndex, length); int pos = rope.ToString(startIndex, length).LastIndexOf(searchText, comparisonType); if (pos < 0) { return(-1); } else { return(pos + startIndex); } }