All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.lenses.jdbc4.Utils.kt Maven / Gradle / Ivy

There is a newer version: 3.0.2
Show newest version
package io.lenses.jdbc4

import java.util.regex.Pattern

object Utils {
  fun like(str: String, expr: String): Boolean {
    var regex = buildExpr(expr)
    regex = regex.replace("_", ".").replace("%", ".*?")
    val p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE or Pattern.DOTALL)
    return p.matcher(str).matches()
  }

  fun buildExpr(s: String?): String {
    if (s == null) {
      throw IllegalArgumentException("String cannot be null")
    }

    val len = s.length
    if (len == 0) {
      return ""
    }

    val sb = StringBuilder(len * 2)
    for (i in 0 until len) {
      val c = s[i]
      if ("[](){}.*+?$^|#\\".indexOf(c) != -1) {
        sb.append("\\")
      }
      sb.append(c)
    }
    return sb.toString()
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy