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

com.jetbrains.plugin.structure.ide.StringUtils.kt Maven / Gradle / Ivy

There is a newer version: 3.289
Show newest version
package com.jetbrains.plugin.structure.ide

import java.lang.Integer.min
import java.nio.file.Path

fun getCommonParentDirectory(paths: List): Path? {
  return when (paths.size) {
    0 -> null
    1 -> paths.first()
    else -> {
      val shortestLength = paths.minOf { it.nameCount }
      val longestLength = paths.maxOf { it.nameCount }
      var firstDiff = longestLength

      val firstPath = paths.first()
      index@ for (index in 0 until shortestLength) {
        val c = firstPath[index]
        for (p in paths.drop(1)) {
          if (p[index] != c) {
            firstDiff = index
            break@index
          }
        }
      }
      firstPath.prefixSubpath(min(shortestLength, firstDiff))
    }
  }
}

private fun Path.prefixSubpath(endIndex: Int): Path? {
  if (endIndex == 0) return null
  return subpath(0, endIndex)
}

private operator fun Path.get(index: Int) = getName(index)






© 2015 - 2024 Weber Informatics LLC | Privacy Policy