1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.history.core;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
public final class Paths {
public static final char DELIM = '/';
private static boolean myIsCaseSensitive;
static {
useSystemCaseSensitivity();
}
public static String getNameOf(String path) {
int i = path.lastIndexOf(DELIM);
if (i == -1 || path.length() == 1) return path;
return path.substring(i + 1);
}
public static String getParentOf(String path) {
int i = path.lastIndexOf(DELIM);
if (i == -1) return "";
if (i == 0) i = 1;
return path.substring(0, i);
}
public static String appended(String path, String child) {
path = appendParent(path);
return path + child;
}
public static String renamed(String path, String newName) {
return appended(getParentOf(path), newName);
}
public static String reparented(String path, String newParentPath) {
return appended(newParentPath, getNameOf(path));
}
public static String relativeIfUnder(String path, String root) {
if (!isParent(root, path)) return null;
path = path.substring(root.length());
if (path.length() == 0) return "";
if (path.charAt(0) != DELIM) return null;
return path.substring(1);
}
public static Iterable<String> split(String path) {
String root = FileUtil.extractRootPath(path);
if (root == null) return splitInner(path);
if (root.length() + 1 == path.length() && path.endsWith(":///")) {
return Collections.singleton(root);
}
Iterable<String> tail = splitInner(path.substring(root.length()));
return ContainerUtil.concat(Collections.singleton(root), tail);
}
@NotNull
private static List<String> splitInner(String path) {
if (path.isEmpty()) return Collections.emptyList();
int s = 0;
int e = path.length();
if (path.charAt(0) == '/') ++s;
if (e > s && path.charAt(e - 1) == '/') --e;
return StringUtil.split(path.substring(s, e), String.valueOf(DELIM), true, false);
}
public static boolean isParent(String parent, String path) {
if (equals(parent, path)) return true;
parent = appendParent(parent);
return myIsCaseSensitive ? path.startsWith(parent) : StringUtil.startsWithIgnoreCase(path, parent);
}
private static String appendParent(String parent) {
if (parent.isEmpty()) return parent;
if (parent.charAt(parent.length() - 1) != DELIM) parent += DELIM;
return parent;
}
public static boolean isParentOrChild(String p1, String p2) {
return isParent(p1, p2) || isParent(p2, p1);
}
public static boolean equals(String p1, String p2) {
return myIsCaseSensitive ? p1.equals(p2) : p1.equalsIgnoreCase(p2);
}
public static void setCaseSensitive(boolean b) {
myIsCaseSensitive = b;
}
public static boolean isCaseSensitive() {
return myIsCaseSensitive;
}
public static void useSystemCaseSensitivity() {
myIsCaseSensitive = SystemInfo.isFileSystemCaseSensitive;
}
}
|