/// <summary> /// Creates the flat text span list. /// </summary> /// <param name="startIndex">The start index.</param> /// <param name="tokens">The tokens.</param> /// <returns></returns> private List<StringSpan> GetFlatSpans(int startIndex, string[] tokens) { if ((tokens == null) || (tokens.Length == 0)) throw new ArgumentNullException("tokens"); if (tokens.Length != 2) throw new ArgumentException("tokens", Local.InvalidArrayLength); string openToken = tokens[0]; int openTokenLength = openToken.Length; string closeToken = tokens[1]; int closeTokenLength = closeToken.Length; // var stringSpans = new List<StringSpan>(); StringSpan span = null; int nextStartIndex = startIndex; int nextEndIndex = -1; do { // locate openning token int startSpanIndex = _s.IndexOf(openToken, nextStartIndex, StringComparison.OrdinalIgnoreCase); if (startSpanIndex == -1) break; // starting position to search for closing token if ((nextEndIndex == -1) || (nextEndIndex < startSpanIndex)) nextEndIndex = startSpanIndex; // locate closing token int endSpanIndex = _s.IndexOf(closeToken, nextEndIndex, StringComparison.OrdinalIgnoreCase); if (endSpanIndex == -1) break; if (span == null) // first span span = new StringSpan(startSpanIndex, endSpanIndex + closeTokenLength - 1); else if (span.Merge(startSpanIndex, endSpanIndex + closeTokenLength - 1)) { // overlapping with current span - do nothing } else { // not-overlapping with current span stringSpans.MergeAdd(span); span = new StringSpan(startSpanIndex, endSpanIndex + closeTokenLength - 1); } nextStartIndex = startSpanIndex + openTokenLength; nextEndIndex = endSpanIndex + closeTokenLength; } while (true); // append last span, if one exists if (span != null) stringSpans.MergeAdd(span); return stringSpans; }
/// <summary> /// Determines whether the specified text span is contain. /// </summary> /// <param name="span">The text span.</param> /// <returns> /// <c>true</c> if the specified text span is contain; otherwise, <c>false</c>. /// </returns> public bool IsContain(StringSpan span) { return(IsContain(span.StartIndex, span.EndIndex)); }
/// <summary> /// Determines whether the specified text span is overlap. /// </summary> /// <param name="span">The text span.</param> /// <returns> /// <c>true</c> if the specified text span is overlap; otherwise, <c>false</c>. /// </returns> public bool IsOverlap(StringSpan span) { return(IsOverlap(span.StartIndex, span.EndIndex)); }
/// <summary> /// Merges the specified text span. /// </summary> /// <param name="span">The text span.</param> /// <returns></returns> public bool Merge(StringSpan span) { return(Merge(span.StartIndex, span.EndIndex)); }
/// <summary> /// Determines whether the specified text span is overlap. /// </summary> /// <param name="span">The text span.</param> /// <returns> /// <c>true</c> if the specified text span is overlap; otherwise, <c>false</c>. /// </returns> public bool IsOverlap(StringSpan span) { return IsOverlap(span.StartIndex, span.EndIndex); }
/// <summary> /// Determines whether the specified text span is contain. /// </summary> /// <param name="span">The text span.</param> /// <returns> /// <c>true</c> if the specified text span is contain; otherwise, <c>false</c>. /// </returns> public bool IsContain(StringSpan span) { return IsContain(span.StartIndex, span.EndIndex); }
/// <summary> /// Merges the specified text span. /// </summary> /// <param name="span">The text span.</param> /// <returns></returns> public bool Merge(StringSpan span) { return Merge(span.StartIndex, span.EndIndex); }
/// <summary> /// Creates the flat text span list. /// </summary> /// <param name="startIndex">The start index.</param> /// <param name="tokenArray">The token array.</param> /// <returns></returns> private List <StringSpan> GetFlatSpans(int startIndex, string[] tokens) { if ((tokens == null) || (tokens.Length == 0)) { throw new ArgumentNullException("tokens"); } if (tokens.Length != 2) { throw new ArgumentException("tokens", Local.InvalidArrayLength); } string openToken = tokens[0]; int openTokenLength = openToken.Length; string closeToken = tokens[1]; int closeTokenLength = closeToken.Length; // var stringSpans = new List <StringSpan>(); StringSpan span = null; int nextStartIndex = startIndex; int nextEndIndex = -1; do { // locate openning token int startSpanIndex = _s.IndexOf(openToken, nextStartIndex, StringComparison.OrdinalIgnoreCase); if (startSpanIndex == -1) { break; } // starting position to search for closing token if ((nextEndIndex == -1) || (nextEndIndex < startSpanIndex)) { nextEndIndex = startSpanIndex; } // locate closing token int endSpanIndex = _s.IndexOf(closeToken, nextEndIndex, StringComparison.OrdinalIgnoreCase); if (endSpanIndex == -1) { break; } if (span == null) { // first span span = new StringSpan(startSpanIndex, endSpanIndex + closeTokenLength - 1); } else if (span.Merge(startSpanIndex, endSpanIndex + closeTokenLength - 1)) { // overlapping with current span - do nothing } else { // not-overlapping with current span stringSpans.MergeAdd(span); span = new StringSpan(startSpanIndex, endSpanIndex + closeTokenLength - 1); } nextStartIndex = startSpanIndex + openTokenLength; nextEndIndex = endSpanIndex + closeTokenLength; } while (true); // append last span, if one exists if (span != null) { stringSpans.MergeAdd(span); } return(stringSpans); }