Skip to content

Commit

Permalink
Update CIString.contains to pass the turkey test, drawing on commons-…
Browse files Browse the repository at this point in the history
…lang3's containsIgnoreCase
  • Loading branch information
henricook committed Oct 8, 2022
1 parent f1ae729 commit c4a0a8f
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion core/src/main/scala/org/typelevel/ci/CIString.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,16 @@ final class CIString private (override val toString: String)

def length: Int = toString.length

def contains(other: CIString): Boolean = toString.toLowerCase.contains(other.toString.toLowerCase)
def contains(needle: CIString): Boolean = {
val needleLength = needle.toString.length
val haystack = toString
// There's no point searching haystack indexes beyond which the substring is too small to ever match the needle
val maxHaystackStartingIndex = haystack.length - needleLength

(0 to maxHaystackStartingIndex).exists { i =>
haystack.regionMatches(true, i, needle.toString, 0, needleLength)
}
}

@deprecated("Use toString", "0.1.0")
def value: String = toString
Expand Down

0 comments on commit c4a0a8f

Please sign in to comment.