@data-vegle/rbxts-go 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +181 -2
- package/README.rbxtsgo.md +169 -0
- package/THIRD_PARTY_NOTICES.md +745 -0
- package/cli/cli.go +454 -0
- package/compile/compile.go +388 -0
- package/compile/declarations.go +221 -0
- package/compile/incremental.go +349 -0
- package/compile/times.go +9 -0
- package/docs/plugin-api.md +243 -0
- package/go.mod +20 -0
- package/go.sum +24 -0
- package/internal/difftest/difftest.go +530 -0
- package/internal/plugintest/plugintest.go +181 -0
- package/luau/create.go +351 -0
- package/luau/doc.go +71 -0
- package/luau/globals.go +178 -0
- package/luau/jsnum.go +188 -0
- package/luau/kinds.go +128 -0
- package/luau/list.go +227 -0
- package/luau/nodes.go +479 -0
- package/luau/operators.go +46 -0
- package/luau/render.go +134 -0
- package/luau/render_expressions.go +264 -0
- package/luau/render_fields.go +81 -0
- package/luau/render_indexable.go +81 -0
- package/luau/render_state.go +134 -0
- package/luau/render_statements.go +235 -0
- package/luau/render_util.go +296 -0
- package/luau/solve_temp_ids.go +212 -0
- package/luau/typeguards.go +175 -0
- package/luau/util.go +184 -0
- package/luau/visit.go +138 -0
- package/npm/index.d.ts +3 -0
- package/npm/rbxts-go.sh +187 -0
- package/package.json +45 -5
- package/patches/typescript/0001-checker-exports.patch +40 -0
- package/patches/typescript/0002-iterable-arity.patch +15 -0
- package/patches/typescript/0003-context-sensitive-this.patch +15 -0
- package/patches/typescript/0004-printer-keep-parse-tree-parentheses.patch +26 -0
- package/pluginapi/host.go +31 -0
- package/pluginapi/pluginapi.go +153 -0
- package/plugins/LICENSE-rbxts-transformer-setget +18 -0
- package/plugins/apply.go +342 -0
- package/plugins/config.go +431 -0
- package/plugins/fallback.go +84 -0
- package/plugins/fixemit.go +60 -0
- package/plugins/native.go +271 -0
- package/plugins/session.go +236 -0
- package/plugins/setget.go +478 -0
- package/project/copy.go +333 -0
- package/project/include/Promise.lua +2068 -0
- package/project/include/RuntimeLib.lua +260 -0
- package/project/nodepath/nodepath.go +917 -0
- package/project/pathtranslator/pathtranslator.go +214 -0
- package/project/project.go +498 -0
- package/project/rojoresolver/json.go +248 -0
- package/project/rojoresolver/rojoresolver.go +559 -0
- package/project/shared/constants.go +83 -0
- package/project/shared/diagnostic.go +238 -0
- package/project/shared/diagnostics.go +388 -0
- package/project/shared/elapsed.go +14 -0
- package/project/shared/eol_other.go +6 -0
- package/project/shared/eol_windows.go +4 -0
- package/project/shared/kleur.go +40 -0
- package/project/shared/util.go +183 -0
- package/scripts/build-with-plugins.sh +130 -0
- package/scripts/setup-third-party.sh +75 -0
- package/transformer/add_index_diagnostics.go +21 -0
- package/transformer/add_one_if_array_type.go +15 -0
- package/transformer/array_binding_pattern_contains_hoists.go +25 -0
- package/transformer/assert_never.go +24 -0
- package/transformer/assignment.go +64 -0
- package/transformer/call_macros.go +70 -0
- package/transformer/check_variable_hoist.go +125 -0
- package/transformer/clean_module_name.go +28 -0
- package/transformer/constructor_macros.go +122 -0
- package/transformer/convert_to_indexable_expression.go +12 -0
- package/transformer/create_binary_from_operator.go +86 -0
- package/transformer/create_continue_statements.go +68 -0
- package/transformer/create_get_service.go +15 -0
- package/transformer/create_hoist_declaration.go +27 -0
- package/transformer/create_import_expression.go +220 -0
- package/transformer/create_string_from_literal.go +195 -0
- package/transformer/create_truthiness_checks.go +63 -0
- package/transformer/create_type_check.go +9 -0
- package/transformer/ensure_transform_order.go +53 -0
- package/transformer/expression_chain.go +27 -0
- package/transformer/expression_might_mutate.go +60 -0
- package/transformer/find_constructor.go +15 -0
- package/transformer/fixup_whitespace_and_decode_entities.go +377 -0
- package/transformer/get_accessor_for_binding_type.go +160 -0
- package/transformer/get_add_iterable_to_array_builder.go +363 -0
- package/transformer/get_assignable_value.go +15 -0
- package/transformer/get_constant_value_literal.go +20 -0
- package/transformer/get_declared_variables.go +34 -0
- package/transformer/get_extends_node.go +23 -0
- package/transformer/get_flags.go +21 -0
- package/transformer/get_kind_name.go +18 -0
- package/transformer/get_original_symbol_of_node.go +16 -0
- package/transformer/get_source_file_from_module_specifier.go +68 -0
- package/transformer/get_statements.go +12 -0
- package/transformer/has_multiple_definitions.go +18 -0
- package/transformer/hoist_functions_past_final_statement.go +43 -0
- package/transformer/identifier_macros.go +17 -0
- package/transformer/is_blocked_by_try_statement.go +19 -0
- package/transformer/is_method.go +104 -0
- package/transformer/is_symbol_mutable.go +26 -0
- package/transformer/is_symbol_of_value.go +11 -0
- package/transformer/is_used_as_statement.go +25 -0
- package/transformer/is_valid_method_index_without_call.go +36 -0
- package/transformer/macro_manager.go +92 -0
- package/transformer/object_accessor.go +32 -0
- package/transformer/offset.go +58 -0
- package/transformer/pointer.go +53 -0
- package/transformer/property_call_macros.go +1035 -0
- package/transformer/state.go +606 -0
- package/transformer/transform_array_assignment_pattern.go +65 -0
- package/transformer/transform_array_binding_pattern.go +49 -0
- package/transformer/transform_array_literal_expression.go +97 -0
- package/transformer/transform_await_expression.go +12 -0
- package/transformer/transform_binary_expression.go +221 -0
- package/transformer/transform_binding_name.go +25 -0
- package/transformer/transform_block.go +14 -0
- package/transformer/transform_boolean_literal.go +13 -0
- package/transformer/transform_break_statement.go +26 -0
- package/transformer/transform_call_expression.go +266 -0
- package/transformer/transform_class_constructor.go +125 -0
- package/transformer/transform_class_declaration.go +12 -0
- package/transformer/transform_class_expression.go +14 -0
- package/transformer/transform_class_like_declaration.go +355 -0
- package/transformer/transform_conditional_expression.go +70 -0
- package/transformer/transform_continue_statement.go +30 -0
- package/transformer/transform_decorators.go +253 -0
- package/transformer/transform_delete_expression.go +17 -0
- package/transformer/transform_do_statement.go +39 -0
- package/transformer/transform_element_access_expression.go +114 -0
- package/transformer/transform_entity_name.go +21 -0
- package/transformer/transform_enum_declaration.go +135 -0
- package/transformer/transform_export_assignment.go +57 -0
- package/transformer/transform_export_declaration.go +150 -0
- package/transformer/transform_expression.go +120 -0
- package/transformer/transform_expression_statement.go +71 -0
- package/transformer/transform_for_of_statement.go +461 -0
- package/transformer/transform_for_statement.go +599 -0
- package/transformer/transform_function_declaration.go +84 -0
- package/transformer/transform_function_expression.go +48 -0
- package/transformer/transform_identifier.go +194 -0
- package/transformer/transform_if_statement.go +44 -0
- package/transformer/transform_import_declaration.go +146 -0
- package/transformer/transform_import_equals_declaration.go +46 -0
- package/transformer/transform_import_expression.go +36 -0
- package/transformer/transform_initializer.go +22 -0
- package/transformer/transform_interpolated_string_part.go +12 -0
- package/transformer/transform_jsx.go +41 -0
- package/transformer/transform_jsx_attributes.go +110 -0
- package/transformer/transform_jsx_children.go +50 -0
- package/transformer/transform_jsx_element.go +14 -0
- package/transformer/transform_jsx_expression.go +20 -0
- package/transformer/transform_jsx_fragment.go +42 -0
- package/transformer/transform_jsx_self_closing_element.go +13 -0
- package/transformer/transform_jsx_tag_name.go +53 -0
- package/transformer/transform_logical.go +160 -0
- package/transformer/transform_logical_or_coalescing_assignment_expression.go +103 -0
- package/transformer/transform_method_declaration.go +102 -0
- package/transformer/transform_module_declaration.go +143 -0
- package/transformer/transform_new_expression.go +27 -0
- package/transformer/transform_no_substitution_template_literal.go +16 -0
- package/transformer/transform_numeric_literal.go +15 -0
- package/transformer/transform_object_assignment_pattern.go +86 -0
- package/transformer/transform_object_binding_pattern.go +49 -0
- package/transformer/transform_object_literal_expression.go +107 -0
- package/transformer/transform_omitted_expression.go +9 -0
- package/transformer/transform_optional_chain.go +312 -0
- package/transformer/transform_parameters.go +124 -0
- package/transformer/transform_parenthesized_expression.go +17 -0
- package/transformer/transform_property_access_expression.go +37 -0
- package/transformer/transform_property_declaration.go +33 -0
- package/transformer/transform_property_name.go +20 -0
- package/transformer/transform_return_statement.go +83 -0
- package/transformer/transform_source_file.go +230 -0
- package/transformer/transform_spread_element.go +35 -0
- package/transformer/transform_statement.go +84 -0
- package/transformer/transform_statement_list.go +90 -0
- package/transformer/transform_string_literal.go +22 -0
- package/transformer/transform_super_keyword.go +9 -0
- package/transformer/transform_switch_statement.go +145 -0
- package/transformer/transform_tagged_template_expression.go +34 -0
- package/transformer/transform_template_expression.go +35 -0
- package/transformer/transform_this_expression.go +33 -0
- package/transformer/transform_throw_statement.go +18 -0
- package/transformer/transform_try_statement.go +192 -0
- package/transformer/transform_type_expression.go +12 -0
- package/transformer/transform_unary_expression.go +79 -0
- package/transformer/transform_variable_statement.go +178 -0
- package/transformer/transform_void_expression.go +13 -0
- package/transformer/transform_while_statement.go +34 -0
- package/transformer/transform_writable.go +69 -0
- package/transformer/transform_yield_expression.go +56 -0
- package/transformer/type_guards.go +30 -0
- package/transformer/types.go +381 -0
- package/transformer/validate_identifier.go +17 -0
- package/transformer/validate_method_assignment.go +106 -0
- package/transformer/validate_not_any.go +37 -0
- package/transformer/wrap_expression_statement.go +20 -0
- package/transformer/wrap_return_if_lua_tuple.go +63 -0
- package/transformer/wrap_statements_as_generator.go +22 -0
- package/tsbridge/affected.go +141 -0
- package/tsbridge/config.go +71 -0
- package/tsbridge/diagnostics.go +128 -0
- package/tsbridge/doc.go +16 -0
- package/tsbridge/macros.go +207 -0
- package/tsbridge/macrospec.go +30 -0
- package/tsbridge/pool.go +142 -0
- package/tsbridge/project.go +137 -0
- package/tsbridge/reprint.go +120 -0
- package/tsbridge/update.go +128 -0
- package/watch/poll.go +126 -0
- package/watch/session.go +651 -0
- package/watch/watch.go +180 -0
|
@@ -0,0 +1,917 @@
|
|
|
1
|
+
// Package nodepath is a port of Node's path module (lib/path.js, Node 20), for both platforms.
|
|
2
|
+
//
|
|
3
|
+
// path/filepath is not a substitute. They disagree on inputs the compiler sees: Dir("a/b/") is "a/b" and
|
|
4
|
+
// dirname("a/b/") is "a"; Ext(".gitignore") is ".gitignore" and extname(".gitignore") is ""; Rel errors where
|
|
5
|
+
// relative returns a path; and Windows comparison rules differ. Byte-exact paths need the same algorithm, so
|
|
6
|
+
// this is a line by line port, and the tests compare it with real Node on generated inputs.
|
|
7
|
+
//
|
|
8
|
+
// Node works on UTF-16 code units and this works on UTF-8 bytes. Separators, dots and colons are ASCII, so the
|
|
9
|
+
// results match. The one exception is win32 Relative on a character whose lowercase has a different length,
|
|
10
|
+
// such as U+0130: Node's indexes shift there and these don't. No test covers that.
|
|
11
|
+
package nodepath
|
|
12
|
+
|
|
13
|
+
import (
|
|
14
|
+
"os"
|
|
15
|
+
"runtime"
|
|
16
|
+
"strings"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
// Path is one flavor of Node's path module.
|
|
20
|
+
type Path struct {
|
|
21
|
+
Sep string
|
|
22
|
+
win32 bool
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
var (
|
|
26
|
+
// Win32 is path.win32.
|
|
27
|
+
Win32 = &Path{Sep: `\`, win32: true}
|
|
28
|
+
// Posix is path.posix.
|
|
29
|
+
Posix = &Path{Sep: "/"}
|
|
30
|
+
// Native is require("path") on this platform.
|
|
31
|
+
Native = pickNative()
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
func pickNative() *Path {
|
|
35
|
+
if runtime.GOOS == "windows" {
|
|
36
|
+
return Win32
|
|
37
|
+
}
|
|
38
|
+
return Posix
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const (
|
|
42
|
+
charSlash = '/'
|
|
43
|
+
charBackslash = '\\'
|
|
44
|
+
charDot = '.'
|
|
45
|
+
charColon = ':'
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
// at is String.charCodeAt, with -1 standing in for NaN.
|
|
49
|
+
func at(s string, i int) int {
|
|
50
|
+
if i < 0 || i >= len(s) {
|
|
51
|
+
return -1
|
|
52
|
+
}
|
|
53
|
+
return int(s[i])
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
func isSep(code int) bool { return code == charSlash || code == charBackslash }
|
|
57
|
+
func isPosixSep(code int) bool { return code == charSlash }
|
|
58
|
+
func isDeviceRoot(code int) bool {
|
|
59
|
+
return (code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z')
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// normalizeString resolves . and .. elements in a path with directory names.
|
|
63
|
+
func normalizeString(path string, allowAboveRoot bool, separator string, isPathSep func(int) bool) string {
|
|
64
|
+
res := ""
|
|
65
|
+
lastSegmentLength := 0
|
|
66
|
+
lastSlash := -1
|
|
67
|
+
dots := 0
|
|
68
|
+
code := 0
|
|
69
|
+
for i := 0; i <= len(path); i++ {
|
|
70
|
+
if i < len(path) {
|
|
71
|
+
code = at(path, i)
|
|
72
|
+
} else if isPathSep(code) {
|
|
73
|
+
break
|
|
74
|
+
} else {
|
|
75
|
+
code = charSlash
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if isPathSep(code) {
|
|
79
|
+
if lastSlash == i-1 || dots == 1 {
|
|
80
|
+
// no-op
|
|
81
|
+
} else if dots == 2 {
|
|
82
|
+
if len(res) < 2 || lastSegmentLength != 2 || at(res, len(res)-1) != charDot || at(res, len(res)-2) != charDot {
|
|
83
|
+
if len(res) > 2 {
|
|
84
|
+
lastSlashIndex := strings.LastIndex(res, separator)
|
|
85
|
+
if lastSlashIndex == -1 {
|
|
86
|
+
res = ""
|
|
87
|
+
lastSegmentLength = 0
|
|
88
|
+
} else {
|
|
89
|
+
res = res[:lastSlashIndex]
|
|
90
|
+
lastSegmentLength = len(res) - 1 - strings.LastIndex(res, separator)
|
|
91
|
+
}
|
|
92
|
+
lastSlash = i
|
|
93
|
+
dots = 0
|
|
94
|
+
continue
|
|
95
|
+
} else if len(res) != 0 {
|
|
96
|
+
res = ""
|
|
97
|
+
lastSegmentLength = 0
|
|
98
|
+
lastSlash = i
|
|
99
|
+
dots = 0
|
|
100
|
+
continue
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if allowAboveRoot {
|
|
104
|
+
if len(res) > 0 {
|
|
105
|
+
res += separator + ".."
|
|
106
|
+
} else {
|
|
107
|
+
res = ".."
|
|
108
|
+
}
|
|
109
|
+
lastSegmentLength = 2
|
|
110
|
+
}
|
|
111
|
+
} else {
|
|
112
|
+
if len(res) > 0 {
|
|
113
|
+
res += separator + path[lastSlash+1:i]
|
|
114
|
+
} else {
|
|
115
|
+
res = path[lastSlash+1 : i]
|
|
116
|
+
}
|
|
117
|
+
lastSegmentLength = i - lastSlash - 1
|
|
118
|
+
}
|
|
119
|
+
lastSlash = i
|
|
120
|
+
dots = 0
|
|
121
|
+
} else if code == charDot && dots != -1 {
|
|
122
|
+
dots++
|
|
123
|
+
} else {
|
|
124
|
+
dots = -1
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return res
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
func cwd() string {
|
|
131
|
+
dir, err := os.Getwd()
|
|
132
|
+
if err != nil {
|
|
133
|
+
return "/"
|
|
134
|
+
}
|
|
135
|
+
return dir
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
func posixCwd() string {
|
|
139
|
+
if runtime.GOOS == "windows" {
|
|
140
|
+
dir := strings.ReplaceAll(cwd(), `\`, "/")
|
|
141
|
+
return dir[strings.Index(dir, "/"):]
|
|
142
|
+
}
|
|
143
|
+
return cwd()
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Resolve is path.resolve.
|
|
147
|
+
func (p *Path) Resolve(args ...string) string {
|
|
148
|
+
if p.win32 {
|
|
149
|
+
return win32Resolve(args)
|
|
150
|
+
}
|
|
151
|
+
// Resolving stops at the last absolute argument, and normalizing a path that is already normalized returns it.
|
|
152
|
+
// That is the common case by far: the compiler resolves paths it has already resolved.
|
|
153
|
+
if len(args) > 0 && isCleanPosix(args[len(args)-1]) {
|
|
154
|
+
return args[len(args)-1]
|
|
155
|
+
}
|
|
156
|
+
resolvedPath := ""
|
|
157
|
+
resolvedAbsolute := false
|
|
158
|
+
for i := len(args) - 1; i >= -1 && !resolvedAbsolute; i-- {
|
|
159
|
+
var path string
|
|
160
|
+
if i >= 0 {
|
|
161
|
+
path = args[i]
|
|
162
|
+
} else {
|
|
163
|
+
path = posixCwd()
|
|
164
|
+
}
|
|
165
|
+
if len(path) == 0 {
|
|
166
|
+
continue
|
|
167
|
+
}
|
|
168
|
+
resolvedPath = path + "/" + resolvedPath
|
|
169
|
+
resolvedAbsolute = at(path, 0) == charSlash
|
|
170
|
+
}
|
|
171
|
+
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, "/", isPosixSep)
|
|
172
|
+
if resolvedAbsolute {
|
|
173
|
+
return "/" + resolvedPath
|
|
174
|
+
}
|
|
175
|
+
if len(resolvedPath) > 0 {
|
|
176
|
+
return resolvedPath
|
|
177
|
+
}
|
|
178
|
+
return "."
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
func win32Resolve(args []string) string {
|
|
182
|
+
resolvedDevice := ""
|
|
183
|
+
resolvedTail := ""
|
|
184
|
+
resolvedAbsolute := false
|
|
185
|
+
|
|
186
|
+
for i := len(args) - 1; i >= -1; i-- {
|
|
187
|
+
var path string
|
|
188
|
+
if i >= 0 {
|
|
189
|
+
path = args[i]
|
|
190
|
+
if len(path) == 0 {
|
|
191
|
+
continue
|
|
192
|
+
}
|
|
193
|
+
} else if len(resolvedDevice) == 0 {
|
|
194
|
+
path = cwd()
|
|
195
|
+
} else {
|
|
196
|
+
path = os.Getenv("=" + resolvedDevice)
|
|
197
|
+
if path == "" {
|
|
198
|
+
path = cwd()
|
|
199
|
+
}
|
|
200
|
+
if strings.ToLower(sliceTo(path, 2)) != strings.ToLower(resolvedDevice) && at(path, 2) == charBackslash {
|
|
201
|
+
path = resolvedDevice + `\`
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
length := len(path)
|
|
206
|
+
rootEnd := 0
|
|
207
|
+
device := ""
|
|
208
|
+
isAbsolute := false
|
|
209
|
+
code := at(path, 0)
|
|
210
|
+
|
|
211
|
+
if length == 1 {
|
|
212
|
+
if isSep(code) {
|
|
213
|
+
rootEnd = 1
|
|
214
|
+
isAbsolute = true
|
|
215
|
+
}
|
|
216
|
+
} else if isSep(code) {
|
|
217
|
+
isAbsolute = true
|
|
218
|
+
if isSep(at(path, 1)) {
|
|
219
|
+
j := 2
|
|
220
|
+
last := j
|
|
221
|
+
for j < length && !isSep(at(path, j)) {
|
|
222
|
+
j++
|
|
223
|
+
}
|
|
224
|
+
if j < length && j != last {
|
|
225
|
+
firstPart := path[last:j]
|
|
226
|
+
last = j
|
|
227
|
+
for j < length && isSep(at(path, j)) {
|
|
228
|
+
j++
|
|
229
|
+
}
|
|
230
|
+
if j < length && j != last {
|
|
231
|
+
last = j
|
|
232
|
+
for j < length && !isSep(at(path, j)) {
|
|
233
|
+
j++
|
|
234
|
+
}
|
|
235
|
+
if j == length || j != last {
|
|
236
|
+
device = `\\` + firstPart + `\` + path[last:j]
|
|
237
|
+
rootEnd = j
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
} else {
|
|
242
|
+
rootEnd = 1
|
|
243
|
+
}
|
|
244
|
+
} else if isDeviceRoot(code) && at(path, 1) == charColon {
|
|
245
|
+
device = path[:2]
|
|
246
|
+
rootEnd = 2
|
|
247
|
+
if length > 2 && isSep(at(path, 2)) {
|
|
248
|
+
isAbsolute = true
|
|
249
|
+
rootEnd = 3
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if len(device) > 0 {
|
|
254
|
+
if len(resolvedDevice) > 0 {
|
|
255
|
+
if strings.ToLower(device) != strings.ToLower(resolvedDevice) {
|
|
256
|
+
continue
|
|
257
|
+
}
|
|
258
|
+
} else {
|
|
259
|
+
resolvedDevice = device
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if resolvedAbsolute {
|
|
264
|
+
if len(resolvedDevice) > 0 {
|
|
265
|
+
break
|
|
266
|
+
}
|
|
267
|
+
} else {
|
|
268
|
+
resolvedTail = path[rootEnd:] + `\` + resolvedTail
|
|
269
|
+
resolvedAbsolute = isAbsolute
|
|
270
|
+
if isAbsolute && len(resolvedDevice) > 0 {
|
|
271
|
+
break
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, `\`, isSep)
|
|
277
|
+
if resolvedAbsolute {
|
|
278
|
+
return resolvedDevice + `\` + resolvedTail
|
|
279
|
+
}
|
|
280
|
+
if result := resolvedDevice + resolvedTail; result != "" {
|
|
281
|
+
return result
|
|
282
|
+
}
|
|
283
|
+
return "."
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
func sliceTo(s string, end int) string {
|
|
287
|
+
if end > len(s) {
|
|
288
|
+
return s
|
|
289
|
+
}
|
|
290
|
+
return s[:end]
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Normalize is path.normalize.
|
|
294
|
+
func (p *Path) Normalize(path string) string {
|
|
295
|
+
if p.win32 {
|
|
296
|
+
return win32Normalize(path)
|
|
297
|
+
}
|
|
298
|
+
if len(path) == 0 {
|
|
299
|
+
return "."
|
|
300
|
+
}
|
|
301
|
+
if isCleanPosix(path) {
|
|
302
|
+
return path
|
|
303
|
+
}
|
|
304
|
+
isAbsolute := at(path, 0) == charSlash
|
|
305
|
+
trailingSeparator := at(path, len(path)-1) == charSlash
|
|
306
|
+
path = normalizeString(path, !isAbsolute, "/", isPosixSep)
|
|
307
|
+
if len(path) == 0 {
|
|
308
|
+
if isAbsolute {
|
|
309
|
+
return "/"
|
|
310
|
+
}
|
|
311
|
+
if trailingSeparator {
|
|
312
|
+
return "./"
|
|
313
|
+
}
|
|
314
|
+
return "."
|
|
315
|
+
}
|
|
316
|
+
if trailingSeparator {
|
|
317
|
+
path += "/"
|
|
318
|
+
}
|
|
319
|
+
if isAbsolute {
|
|
320
|
+
return "/" + path
|
|
321
|
+
}
|
|
322
|
+
return path
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
func win32Normalize(path string) string {
|
|
326
|
+
length := len(path)
|
|
327
|
+
if length == 0 {
|
|
328
|
+
return "."
|
|
329
|
+
}
|
|
330
|
+
rootEnd := 0
|
|
331
|
+
device := ""
|
|
332
|
+
hasDevice := false
|
|
333
|
+
isAbsolute := false
|
|
334
|
+
code := at(path, 0)
|
|
335
|
+
|
|
336
|
+
if length == 1 {
|
|
337
|
+
if isPosixSep(code) {
|
|
338
|
+
return `\`
|
|
339
|
+
}
|
|
340
|
+
return path
|
|
341
|
+
}
|
|
342
|
+
if isSep(code) {
|
|
343
|
+
isAbsolute = true
|
|
344
|
+
if isSep(at(path, 1)) {
|
|
345
|
+
j := 2
|
|
346
|
+
last := j
|
|
347
|
+
for j < length && !isSep(at(path, j)) {
|
|
348
|
+
j++
|
|
349
|
+
}
|
|
350
|
+
if j < length && j != last {
|
|
351
|
+
firstPart := path[last:j]
|
|
352
|
+
last = j
|
|
353
|
+
for j < length && isSep(at(path, j)) {
|
|
354
|
+
j++
|
|
355
|
+
}
|
|
356
|
+
if j < length && j != last {
|
|
357
|
+
last = j
|
|
358
|
+
for j < length && !isSep(at(path, j)) {
|
|
359
|
+
j++
|
|
360
|
+
}
|
|
361
|
+
if j == length {
|
|
362
|
+
return `\\` + firstPart + `\` + path[last:] + `\`
|
|
363
|
+
}
|
|
364
|
+
if j != last {
|
|
365
|
+
device = `\\` + firstPart + `\` + path[last:j]
|
|
366
|
+
hasDevice = true
|
|
367
|
+
rootEnd = j
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
} else {
|
|
372
|
+
rootEnd = 1
|
|
373
|
+
}
|
|
374
|
+
} else if isDeviceRoot(code) && at(path, 1) == charColon {
|
|
375
|
+
device = path[:2]
|
|
376
|
+
hasDevice = true
|
|
377
|
+
rootEnd = 2
|
|
378
|
+
if length > 2 && isSep(at(path, 2)) {
|
|
379
|
+
isAbsolute = true
|
|
380
|
+
rootEnd = 3
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
tail := ""
|
|
385
|
+
if rootEnd < length {
|
|
386
|
+
tail = normalizeString(path[rootEnd:], !isAbsolute, `\`, isSep)
|
|
387
|
+
}
|
|
388
|
+
if len(tail) == 0 && !isAbsolute {
|
|
389
|
+
tail = "."
|
|
390
|
+
}
|
|
391
|
+
if len(tail) > 0 && isSep(at(path, length-1)) {
|
|
392
|
+
tail += `\`
|
|
393
|
+
}
|
|
394
|
+
if !hasDevice {
|
|
395
|
+
if isAbsolute {
|
|
396
|
+
return `\` + tail
|
|
397
|
+
}
|
|
398
|
+
return tail
|
|
399
|
+
}
|
|
400
|
+
if isAbsolute {
|
|
401
|
+
return device + `\` + tail
|
|
402
|
+
}
|
|
403
|
+
return device + tail
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// IsAbsolute is path.isAbsolute.
|
|
407
|
+
func (p *Path) IsAbsolute(path string) bool {
|
|
408
|
+
if !p.win32 {
|
|
409
|
+
return len(path) > 0 && at(path, 0) == charSlash
|
|
410
|
+
}
|
|
411
|
+
if len(path) == 0 {
|
|
412
|
+
return false
|
|
413
|
+
}
|
|
414
|
+
code := at(path, 0)
|
|
415
|
+
return isSep(code) || (len(path) > 2 && isDeviceRoot(code) && at(path, 1) == charColon && isSep(at(path, 2)))
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// Join is path.join.
|
|
419
|
+
func (p *Path) Join(args ...string) string {
|
|
420
|
+
if len(args) == 0 {
|
|
421
|
+
return "."
|
|
422
|
+
}
|
|
423
|
+
// One allocation for the joined string, where `joined += sep + arg` makes one per argument.
|
|
424
|
+
size := 0
|
|
425
|
+
firstPart := ""
|
|
426
|
+
hasJoined := false
|
|
427
|
+
for _, arg := range args {
|
|
428
|
+
if len(arg) > 0 {
|
|
429
|
+
if !hasJoined {
|
|
430
|
+
firstPart = arg
|
|
431
|
+
hasJoined = true
|
|
432
|
+
}
|
|
433
|
+
size += len(arg) + len(p.Sep)
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
if !hasJoined {
|
|
437
|
+
return "."
|
|
438
|
+
}
|
|
439
|
+
var builder strings.Builder
|
|
440
|
+
builder.Grow(size)
|
|
441
|
+
for _, arg := range args {
|
|
442
|
+
if len(arg) > 0 {
|
|
443
|
+
if builder.Len() > 0 {
|
|
444
|
+
builder.WriteString(p.Sep)
|
|
445
|
+
}
|
|
446
|
+
builder.WriteString(arg)
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
joined := builder.String()
|
|
450
|
+
if !p.win32 {
|
|
451
|
+
return p.Normalize(joined)
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
needsReplace := true
|
|
455
|
+
slashCount := 0
|
|
456
|
+
if isSep(at(firstPart, 0)) {
|
|
457
|
+
slashCount++
|
|
458
|
+
firstLen := len(firstPart)
|
|
459
|
+
if firstLen > 1 && isSep(at(firstPart, 1)) {
|
|
460
|
+
slashCount++
|
|
461
|
+
if firstLen > 2 {
|
|
462
|
+
if isSep(at(firstPart, 2)) {
|
|
463
|
+
slashCount++
|
|
464
|
+
} else {
|
|
465
|
+
needsReplace = false
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
if needsReplace {
|
|
471
|
+
for slashCount < len(joined) && isSep(at(joined, slashCount)) {
|
|
472
|
+
slashCount++
|
|
473
|
+
}
|
|
474
|
+
if slashCount >= 2 {
|
|
475
|
+
joined = `\` + joined[slashCount:]
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return win32Normalize(joined)
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// Relative is path.relative.
|
|
482
|
+
func (p *Path) Relative(from, to string) string {
|
|
483
|
+
if from == to {
|
|
484
|
+
return ""
|
|
485
|
+
}
|
|
486
|
+
if p.win32 {
|
|
487
|
+
return win32Relative(from, to)
|
|
488
|
+
}
|
|
489
|
+
from = p.Resolve(from)
|
|
490
|
+
to = p.Resolve(to)
|
|
491
|
+
if from == to {
|
|
492
|
+
return ""
|
|
493
|
+
}
|
|
494
|
+
const fromStart = 1
|
|
495
|
+
fromEnd := len(from)
|
|
496
|
+
fromLen := fromEnd - fromStart
|
|
497
|
+
const toStart = 1
|
|
498
|
+
toLen := len(to) - toStart
|
|
499
|
+
|
|
500
|
+
length := min(fromLen, toLen)
|
|
501
|
+
lastCommonSep := -1
|
|
502
|
+
i := 0
|
|
503
|
+
for ; i < length; i++ {
|
|
504
|
+
fromCode := at(from, fromStart+i)
|
|
505
|
+
if fromCode != at(to, toStart+i) {
|
|
506
|
+
break
|
|
507
|
+
} else if fromCode == charSlash {
|
|
508
|
+
lastCommonSep = i
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
if i == length {
|
|
512
|
+
if toLen > length {
|
|
513
|
+
if at(to, toStart+i) == charSlash {
|
|
514
|
+
return to[toStart+i+1:]
|
|
515
|
+
}
|
|
516
|
+
if i == 0 {
|
|
517
|
+
return to[toStart+i:]
|
|
518
|
+
}
|
|
519
|
+
} else if fromLen > length {
|
|
520
|
+
if at(from, fromStart+i) == charSlash {
|
|
521
|
+
lastCommonSep = i
|
|
522
|
+
} else if i == 0 {
|
|
523
|
+
lastCommonSep = 0
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
out := ""
|
|
529
|
+
for i = fromStart + lastCommonSep + 1; i <= fromEnd; i++ {
|
|
530
|
+
if i == fromEnd || at(from, i) == charSlash {
|
|
531
|
+
if len(out) == 0 {
|
|
532
|
+
out += ".."
|
|
533
|
+
} else {
|
|
534
|
+
out += "/.."
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return out + to[toStart+lastCommonSep:]
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
func win32Relative(from, to string) string {
|
|
542
|
+
fromOrig := win32Resolve([]string{from})
|
|
543
|
+
toOrig := win32Resolve([]string{to})
|
|
544
|
+
if fromOrig == toOrig {
|
|
545
|
+
return ""
|
|
546
|
+
}
|
|
547
|
+
from = lowerSameLength(fromOrig)
|
|
548
|
+
to = lowerSameLength(toOrig)
|
|
549
|
+
if from == to {
|
|
550
|
+
return ""
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
fromStart := 0
|
|
554
|
+
for fromStart < len(from) && at(from, fromStart) == charBackslash {
|
|
555
|
+
fromStart++
|
|
556
|
+
}
|
|
557
|
+
fromEnd := len(from)
|
|
558
|
+
for fromEnd-1 > fromStart && at(from, fromEnd-1) == charBackslash {
|
|
559
|
+
fromEnd--
|
|
560
|
+
}
|
|
561
|
+
fromLen := fromEnd - fromStart
|
|
562
|
+
|
|
563
|
+
toStart := 0
|
|
564
|
+
for toStart < len(to) && at(to, toStart) == charBackslash {
|
|
565
|
+
toStart++
|
|
566
|
+
}
|
|
567
|
+
toEnd := len(to)
|
|
568
|
+
for toEnd-1 > toStart && at(to, toEnd-1) == charBackslash {
|
|
569
|
+
toEnd--
|
|
570
|
+
}
|
|
571
|
+
toLen := toEnd - toStart
|
|
572
|
+
|
|
573
|
+
length := min(fromLen, toLen)
|
|
574
|
+
lastCommonSep := -1
|
|
575
|
+
i := 0
|
|
576
|
+
for ; i < length; i++ {
|
|
577
|
+
fromCode := at(from, fromStart+i)
|
|
578
|
+
if fromCode != at(to, toStart+i) {
|
|
579
|
+
break
|
|
580
|
+
} else if fromCode == charBackslash {
|
|
581
|
+
lastCommonSep = i
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
if i != length {
|
|
586
|
+
if lastCommonSep == -1 {
|
|
587
|
+
return toOrig
|
|
588
|
+
}
|
|
589
|
+
} else {
|
|
590
|
+
if toLen > length {
|
|
591
|
+
if at(to, toStart+i) == charBackslash {
|
|
592
|
+
return toOrig[toStart+i+1:]
|
|
593
|
+
}
|
|
594
|
+
if i == 2 {
|
|
595
|
+
return toOrig[toStart+i:]
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
if fromLen > length {
|
|
599
|
+
if at(from, fromStart+i) == charBackslash {
|
|
600
|
+
lastCommonSep = i
|
|
601
|
+
} else if i == 2 {
|
|
602
|
+
lastCommonSep = 3
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
if lastCommonSep == -1 {
|
|
606
|
+
lastCommonSep = 0
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
out := ""
|
|
611
|
+
for i = fromStart + lastCommonSep + 1; i <= fromEnd; i++ {
|
|
612
|
+
if i == fromEnd || at(from, i) == charBackslash {
|
|
613
|
+
if len(out) == 0 {
|
|
614
|
+
out += ".."
|
|
615
|
+
} else {
|
|
616
|
+
out += `\..`
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
toStart += lastCommonSep
|
|
622
|
+
if len(out) > 0 {
|
|
623
|
+
return out + toOrig[toStart:toEnd]
|
|
624
|
+
}
|
|
625
|
+
if at(toOrig, toStart) == charBackslash {
|
|
626
|
+
toStart++
|
|
627
|
+
}
|
|
628
|
+
return toOrig[toStart:toEnd]
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// lowerSameLength lowercases, keeping the byte length, so indexes into the result also fit the original.
|
|
632
|
+
func lowerSameLength(s string) string {
|
|
633
|
+
lower := strings.ToLower(s)
|
|
634
|
+
if len(lower) == len(s) {
|
|
635
|
+
return lower
|
|
636
|
+
}
|
|
637
|
+
var b strings.Builder
|
|
638
|
+
for _, r := range s {
|
|
639
|
+
mapped := strings.ToLower(string(r))
|
|
640
|
+
if len(mapped) == len(string(r)) {
|
|
641
|
+
b.WriteString(mapped)
|
|
642
|
+
} else {
|
|
643
|
+
b.WriteRune(r)
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return b.String()
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
// Dirname is path.dirname.
|
|
650
|
+
func (p *Path) Dirname(path string) string {
|
|
651
|
+
if p.win32 {
|
|
652
|
+
return win32Dirname(path)
|
|
653
|
+
}
|
|
654
|
+
if len(path) == 0 {
|
|
655
|
+
return "."
|
|
656
|
+
}
|
|
657
|
+
hasRoot := at(path, 0) == charSlash
|
|
658
|
+
end := -1
|
|
659
|
+
matchedSlash := true
|
|
660
|
+
for i := len(path) - 1; i >= 1; i-- {
|
|
661
|
+
if at(path, i) == charSlash {
|
|
662
|
+
if !matchedSlash {
|
|
663
|
+
end = i
|
|
664
|
+
break
|
|
665
|
+
}
|
|
666
|
+
} else {
|
|
667
|
+
matchedSlash = false
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
if end == -1 {
|
|
671
|
+
if hasRoot {
|
|
672
|
+
return "/"
|
|
673
|
+
}
|
|
674
|
+
return "."
|
|
675
|
+
}
|
|
676
|
+
if hasRoot && end == 1 {
|
|
677
|
+
return "//"
|
|
678
|
+
}
|
|
679
|
+
return path[:end]
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
func win32Dirname(path string) string {
|
|
683
|
+
length := len(path)
|
|
684
|
+
if length == 0 {
|
|
685
|
+
return "."
|
|
686
|
+
}
|
|
687
|
+
rootEnd := -1
|
|
688
|
+
offset := 0
|
|
689
|
+
code := at(path, 0)
|
|
690
|
+
|
|
691
|
+
if length == 1 {
|
|
692
|
+
if isSep(code) {
|
|
693
|
+
return path
|
|
694
|
+
}
|
|
695
|
+
return "."
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
if isSep(code) {
|
|
699
|
+
rootEnd = 1
|
|
700
|
+
offset = 1
|
|
701
|
+
if isSep(at(path, 1)) {
|
|
702
|
+
j := 2
|
|
703
|
+
last := j
|
|
704
|
+
for j < length && !isSep(at(path, j)) {
|
|
705
|
+
j++
|
|
706
|
+
}
|
|
707
|
+
if j < length && j != last {
|
|
708
|
+
last = j
|
|
709
|
+
for j < length && isSep(at(path, j)) {
|
|
710
|
+
j++
|
|
711
|
+
}
|
|
712
|
+
if j < length && j != last {
|
|
713
|
+
last = j
|
|
714
|
+
for j < length && !isSep(at(path, j)) {
|
|
715
|
+
j++
|
|
716
|
+
}
|
|
717
|
+
if j == length {
|
|
718
|
+
return path
|
|
719
|
+
}
|
|
720
|
+
if j != last {
|
|
721
|
+
rootEnd = j + 1
|
|
722
|
+
offset = j + 1
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
} else if isDeviceRoot(code) && at(path, 1) == charColon {
|
|
728
|
+
if length > 2 && isSep(at(path, 2)) {
|
|
729
|
+
rootEnd = 3
|
|
730
|
+
} else {
|
|
731
|
+
rootEnd = 2
|
|
732
|
+
}
|
|
733
|
+
offset = rootEnd
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
end := -1
|
|
737
|
+
matchedSlash := true
|
|
738
|
+
for i := length - 1; i >= offset; i-- {
|
|
739
|
+
if isSep(at(path, i)) {
|
|
740
|
+
if !matchedSlash {
|
|
741
|
+
end = i
|
|
742
|
+
break
|
|
743
|
+
}
|
|
744
|
+
} else {
|
|
745
|
+
matchedSlash = false
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
if end == -1 {
|
|
750
|
+
if rootEnd == -1 {
|
|
751
|
+
return "."
|
|
752
|
+
}
|
|
753
|
+
end = rootEnd
|
|
754
|
+
}
|
|
755
|
+
return path[:end]
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
// Basename is path.basename(path, suffix). Pass "" for no suffix: Node treats an empty suffix the same way.
|
|
759
|
+
func (p *Path) Basename(path, suffix string) string {
|
|
760
|
+
isPathSep := isPosixSep
|
|
761
|
+
start := 0
|
|
762
|
+
if p.win32 {
|
|
763
|
+
isPathSep = isSep
|
|
764
|
+
if len(path) >= 2 && isDeviceRoot(at(path, 0)) && at(path, 1) == charColon {
|
|
765
|
+
start = 2
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
end := -1
|
|
769
|
+
matchedSlash := true
|
|
770
|
+
|
|
771
|
+
if len(suffix) > 0 && len(suffix) <= len(path) {
|
|
772
|
+
if suffix == path {
|
|
773
|
+
return ""
|
|
774
|
+
}
|
|
775
|
+
extIdx := len(suffix) - 1
|
|
776
|
+
firstNonSlashEnd := -1
|
|
777
|
+
for i := len(path) - 1; i >= start; i-- {
|
|
778
|
+
code := at(path, i)
|
|
779
|
+
if isPathSep(code) {
|
|
780
|
+
if !matchedSlash {
|
|
781
|
+
start = i + 1
|
|
782
|
+
break
|
|
783
|
+
}
|
|
784
|
+
} else {
|
|
785
|
+
if firstNonSlashEnd == -1 {
|
|
786
|
+
matchedSlash = false
|
|
787
|
+
firstNonSlashEnd = i + 1
|
|
788
|
+
}
|
|
789
|
+
if extIdx >= 0 {
|
|
790
|
+
if code == at(suffix, extIdx) {
|
|
791
|
+
extIdx--
|
|
792
|
+
if extIdx == -1 {
|
|
793
|
+
end = i
|
|
794
|
+
}
|
|
795
|
+
} else {
|
|
796
|
+
extIdx = -1
|
|
797
|
+
end = firstNonSlashEnd
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
if start == end {
|
|
803
|
+
end = firstNonSlashEnd
|
|
804
|
+
} else if end == -1 {
|
|
805
|
+
end = len(path)
|
|
806
|
+
}
|
|
807
|
+
return path[start:end]
|
|
808
|
+
}
|
|
809
|
+
for i := len(path) - 1; i >= start; i-- {
|
|
810
|
+
if isPathSep(at(path, i)) {
|
|
811
|
+
if !matchedSlash {
|
|
812
|
+
start = i + 1
|
|
813
|
+
break
|
|
814
|
+
}
|
|
815
|
+
} else if end == -1 {
|
|
816
|
+
matchedSlash = false
|
|
817
|
+
end = i + 1
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
if end == -1 {
|
|
821
|
+
return ""
|
|
822
|
+
}
|
|
823
|
+
return path[start:end]
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
// Extname is path.extname.
|
|
827
|
+
func (p *Path) Extname(path string) string {
|
|
828
|
+
isPathSep := isPosixSep
|
|
829
|
+
start := 0
|
|
830
|
+
startPart := 0
|
|
831
|
+
if p.win32 {
|
|
832
|
+
isPathSep = isSep
|
|
833
|
+
if len(path) >= 2 && at(path, 1) == charColon && isDeviceRoot(at(path, 0)) {
|
|
834
|
+
start = 2
|
|
835
|
+
startPart = 2
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
startDot := -1
|
|
839
|
+
end := -1
|
|
840
|
+
matchedSlash := true
|
|
841
|
+
preDotState := 0
|
|
842
|
+
|
|
843
|
+
for i := len(path) - 1; i >= start; i-- {
|
|
844
|
+
code := at(path, i)
|
|
845
|
+
if isPathSep(code) {
|
|
846
|
+
if !matchedSlash {
|
|
847
|
+
startPart = i + 1
|
|
848
|
+
break
|
|
849
|
+
}
|
|
850
|
+
continue
|
|
851
|
+
}
|
|
852
|
+
if end == -1 {
|
|
853
|
+
matchedSlash = false
|
|
854
|
+
end = i + 1
|
|
855
|
+
}
|
|
856
|
+
if code == charDot {
|
|
857
|
+
if startDot == -1 {
|
|
858
|
+
startDot = i
|
|
859
|
+
} else if preDotState != 1 {
|
|
860
|
+
preDotState = 1
|
|
861
|
+
}
|
|
862
|
+
} else if startDot != -1 {
|
|
863
|
+
preDotState = -1
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
if startDot == -1 || end == -1 || preDotState == 0 || (preDotState == 1 && startDot == end-1 && startDot == startPart+1) {
|
|
868
|
+
return ""
|
|
869
|
+
}
|
|
870
|
+
return path[startDot:end]
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
// isCleanPosix reports whether p is an absolute posix path in the form Resolve returns: it starts with one slash, has
|
|
874
|
+
// no empty, "." or ".." segment, and no trailing slash unless it is the root.
|
|
875
|
+
func isCleanPosix(p string) bool {
|
|
876
|
+
if len(p) == 0 || p[0] != '/' {
|
|
877
|
+
return false
|
|
878
|
+
}
|
|
879
|
+
if len(p) == 1 {
|
|
880
|
+
return true
|
|
881
|
+
}
|
|
882
|
+
segmentStart := 1
|
|
883
|
+
for i := 1; i <= len(p); i++ {
|
|
884
|
+
if i < len(p) && p[i] != '/' {
|
|
885
|
+
continue
|
|
886
|
+
}
|
|
887
|
+
segment := p[segmentStart:i]
|
|
888
|
+
if segment == "" || segment == "." || segment == ".." {
|
|
889
|
+
return false
|
|
890
|
+
}
|
|
891
|
+
segmentStart = i + 1
|
|
892
|
+
}
|
|
893
|
+
return true
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
// RelativeStartsWithDotDot answers strings.HasPrefix(p.Relative(from, to), "..") without building the relative path,
|
|
897
|
+
// when it can: for the posix flavor, with both paths already absolute and normalized. ok is false when it can't, and
|
|
898
|
+
// the caller must then call Relative. This is the test behind isPathDescendantOf, which runs for every type the
|
|
899
|
+
// transformer asks about, and Relative allocates four strings per call.
|
|
900
|
+
func (p *Path) RelativeStartsWithDotDot(from, to string) (startsWithDotDot bool, ok bool) {
|
|
901
|
+
if p.win32 || !isCleanPosix(from) || !isCleanPosix(to) {
|
|
902
|
+
return false, false
|
|
903
|
+
}
|
|
904
|
+
if from == to {
|
|
905
|
+
// Relative returns "".
|
|
906
|
+
return false, true
|
|
907
|
+
}
|
|
908
|
+
if from == "/" {
|
|
909
|
+
return strings.HasPrefix(to[1:], ".."), true
|
|
910
|
+
}
|
|
911
|
+
if len(to) > len(from) && to[len(from)] == '/' && to[:len(from)] == from {
|
|
912
|
+
// to is inside from, so the relative path is the rest of to. A name such as "..x" still starts with "..".
|
|
913
|
+
return strings.HasPrefix(to[len(from)+1:], ".."), true
|
|
914
|
+
}
|
|
915
|
+
// to is outside from, so the relative path starts by going up.
|
|
916
|
+
return true, true
|
|
917
|
+
}
|