OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// A comprehensive, cross-platform path manipulation library. | 5 /// A comprehensive, cross-platform path manipulation library. |
6 /// | 6 /// |
7 /// ## Installing ## | 7 /// ## Installing ## |
8 /// | 8 /// |
9 /// Use [pub][] to install this package. Add the following to your | 9 /// Use [pub][] to install this package. Add the following to your |
10 /// `pubspec.yaml` file. | 10 /// `pubspec.yaml` file. |
(...skipping 1025 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1036 /// directories. | 1036 /// directories. |
1037 List<String> parts; | 1037 List<String> parts; |
1038 | 1038 |
1039 /// The path separators preceding each part. | 1039 /// The path separators preceding each part. |
1040 /// | 1040 /// |
1041 /// The first one will be an empty string unless the root requires a separator | 1041 /// The first one will be an empty string unless the root requires a separator |
1042 /// between it and the path. The last one will be an empty string unless the | 1042 /// between it and the path. The last one will be an empty string unless the |
1043 /// path ends with a trailing separator. | 1043 /// path ends with a trailing separator. |
1044 List<String> separators; | 1044 List<String> separators; |
1045 | 1045 |
1046 /// The file extension of the last part, or "" if it doesn't have one. | 1046 /// The file extension of the last non-empty part, or "" if it doesn't have |
| 1047 /// one. |
1047 String get extension => _splitExtension()[1]; | 1048 String get extension => _splitExtension()[1]; |
1048 | 1049 |
1049 /// `true` if this is an absolute path. | 1050 /// `true` if this is an absolute path. |
1050 bool get isAbsolute => root != null; | 1051 bool get isAbsolute => root != null; |
1051 | 1052 |
1052 _ParsedPath(this.style, this.root, this.isRootRelative, this.parts, | 1053 _ParsedPath(this.style, this.root, this.isRootRelative, this.parts, |
1053 this.separators); | 1054 this.separators); |
1054 | 1055 |
1055 String get basename { | 1056 String get basename { |
1056 var copy = this.clone(); | 1057 var copy = this.clone(); |
1057 copy.removeTrailingSeparators(); | 1058 copy.removeTrailingSeparators(); |
1058 if (copy.parts.isEmpty) return root == null ? '' : root; | 1059 if (copy.parts.isEmpty) return root == null ? '' : root; |
1059 return copy.parts.last; | 1060 return copy.parts.last; |
1060 } | 1061 } |
1061 | 1062 |
1062 String get basenameWithoutExtension { | 1063 String get basenameWithoutExtension => _splitExtension()[0]; |
1063 var copy = this.clone(); | |
1064 copy.removeTrailingSeparators(); | |
1065 if (copy.parts.isEmpty) return root == null ? '' : root; | |
1066 return copy._splitExtension()[0]; | |
1067 } | |
1068 | 1064 |
1069 bool get hasTrailingSeparator => | 1065 bool get hasTrailingSeparator => |
1070 !parts.isEmpty && (parts.last == '' || separators.last != ''); | 1066 !parts.isEmpty && (parts.last == '' || separators.last != ''); |
1071 | 1067 |
1072 void removeTrailingSeparators() { | 1068 void removeTrailingSeparators() { |
1073 while (!parts.isEmpty && parts.last == '') { | 1069 while (!parts.isEmpty && parts.last == '') { |
1074 parts.removeLast(); | 1070 parts.removeLast(); |
1075 separators.removeLast(); | 1071 separators.removeLast(); |
1076 } | 1072 } |
1077 if (separators.length > 0) separators[separators.length - 1] = ''; | 1073 if (separators.length > 0) separators[separators.length - 1] = ''; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1130 if (root != null) builder.write(root); | 1126 if (root != null) builder.write(root); |
1131 for (var i = 0; i < parts.length; i++) { | 1127 for (var i = 0; i < parts.length; i++) { |
1132 builder.write(separators[i]); | 1128 builder.write(separators[i]); |
1133 builder.write(parts[i]); | 1129 builder.write(parts[i]); |
1134 } | 1130 } |
1135 builder.write(separators.last); | 1131 builder.write(separators.last); |
1136 | 1132 |
1137 return builder.toString(); | 1133 return builder.toString(); |
1138 } | 1134 } |
1139 | 1135 |
1140 /// Splits the last part of the path into a two-element list. The first is | 1136 /// Splits the last non-empty part of the path into a `[basename, extension`] |
1141 /// the name of the file without any extension. The second is the extension | 1137 /// pair. |
1142 /// or "" if it has none. | 1138 /// |
| 1139 /// Returns a two-element list. The first is the name of the file without any |
| 1140 /// extension. The second is the extension or "" if it has none. |
1143 List<String> _splitExtension() { | 1141 List<String> _splitExtension() { |
1144 if (parts.isEmpty) return ['', '']; | 1142 var file = parts.lastWhere((p) => p != '', orElse: () => null); |
1145 | 1143 |
1146 var file = parts.last; | 1144 if (file == null) return ['', '']; |
1147 if (file == '..') return ['..', '']; | 1145 if (file == '..') return ['..', '']; |
1148 | 1146 |
1149 var lastDot = file.lastIndexOf('.'); | 1147 var lastDot = file.lastIndexOf('.'); |
1150 | 1148 |
1151 // If there is no dot, or it's the first character, like '.bashrc', it | 1149 // If there is no dot, or it's the first character, like '.bashrc', it |
1152 // doesn't count. | 1150 // doesn't count. |
1153 if (lastDot <= 0) return [file, '']; | 1151 if (lastDot <= 0) return [file, '']; |
1154 | 1152 |
1155 return [file.substring(0, lastDot), file.substring(lastDot)]; | 1153 return [file.substring(0, lastDot), file.substring(lastDot)]; |
1156 } | 1154 } |
1157 | 1155 |
1158 _ParsedPath clone() => new _ParsedPath( | 1156 _ParsedPath clone() => new _ParsedPath( |
1159 style, root, isRootRelative, | 1157 style, root, isRootRelative, |
1160 new List.from(parts), new List.from(separators)); | 1158 new List.from(parts), new List.from(separators)); |
1161 } | 1159 } |
OLD | NEW |