summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
Diffstat (limited to 'compiler')
-rw-r--r--compiler/pathutils.nim171
1 files changed, 10 insertions, 161 deletions
diff --git a/compiler/pathutils.nim b/compiler/pathutils.nim
index 703467bc4..80c479898 100644
--- a/compiler/pathutils.nim
+++ b/compiler/pathutils.nim
@@ -9,9 +9,8 @@
 
 ## Path handling utilities for Nim. Strictly typed code in order
 ## to avoid the never ending time sink in getting path handling right.
-## Might be a candidate for the stdlib later.
 
-import os, strutils
+import os, strutils, pathnorm
 
 type
   AbsoluteFile* = distinct string
@@ -45,130 +44,9 @@ proc cmpPaths*(x, y: AbsoluteDir): int {.borrow.}
 
 proc createDir*(x: AbsoluteDir) {.borrow.}
 
-type
-  PathIter = object
-    i, prev: int
-    notFirst: bool
-
-proc hasNext(it: PathIter; x: string): bool =
-  it.i < x.len
-
-proc next(it: var PathIter; x: string): (int, int) =
-  it.prev = it.i
-  if not it.notFirst and x[it.i] in {DirSep, AltSep}:
-    # absolute path:
-    inc it.i
-  else:
-    while it.i < x.len and x[it.i] notin {DirSep, AltSep}: inc it.i
-  if it.i > it.prev:
-    result = (it.prev, it.i-1)
-  elif hasNext(it, x):
-    result = next(it, x)
-
-  # skip all separators:
-  while it.i < x.len and x[it.i] in {DirSep, AltSep}: inc it.i
-  it.notFirst = true
-
-iterator dirs(x: string): (int, int) =
-  var it: PathIter
-  while hasNext(it, x): yield next(it, x)
-
-proc isDot(x: string; bounds: (int, int)): bool =
-  bounds[1] == bounds[0] and x[bounds[0]] == '.'
-
-proc isDotDot(x: string; bounds: (int, int)): bool =
-  bounds[1] == bounds[0] + 1 and x[bounds[0]] == '.' and x[bounds[0]+1] == '.'
-
-proc isSlash(x: string; bounds: (int, int)): bool =
-  bounds[1] == bounds[0] and x[bounds[0]] in {DirSep, AltSep}
-
-const canonDirSep = when isMainModule: '/' else: DirSep
-
-proc canon(x: string; result: var string; state: var int) =
-  # state: 0th bit set if isAbsolute path. Other bits count
-  # the number of path components.
-  for b in dirs(x):
-    if (state shr 1 == 0) and isSlash(x, b):
-      result.add canonDirSep
-      state = state or 1
-    elif result.len > (state and 1) and isDotDot(x, b):
-      var d = result.len
-      # f/..
-      while (d-1) > (state and 1) and result[d-1] notin {DirSep, AltSep}:
-        dec d
-      if d > 0: setLen(result, d-1)
-    elif isDot(x, b):
-      discard "discard the dot"
-    elif b[1] >= b[0]:
-      if result.len > 0 and result[^1] notin {DirSep, AltSep}:
-        result.add canonDirSep
-      result.add substr(x, b[0], b[1])
-    inc state, 2
-
-proc canon(x: string): string =
-  # - Turn multiple slashes into single slashes.
-  # - Resolve '/foo/../bar' to '/bar'.
-  # - Remove './' from the path.
-  result = newStringOfCap(x.len)
-  var state = 0
-  canon(x, result, state)
-
-when FileSystemCaseSensitive:
-  template `!=?`(a, b: char): bool = toLowerAscii(a) != toLowerAscii(b)
-else:
-  template `!=?`(a, b: char): bool = a != b
-
-proc relativeTo(full, base: string; sep = canonDirSep): string =
-  if full.len == 0: return ""
-  var f, b: PathIter
-  var ff = (0, -1)
-  var bb = (0, -1) # (int, int)
-  result = newStringOfCap(full.len)
-  # skip the common prefix:
-  while f.hasNext(full) and b.hasNext(base):
-    ff = next(f, full)
-    bb = next(b, base)
-    let diff = ff[1] - ff[0]
-    if diff != bb[1] - bb[0]: break
-    var same = true
-    for i in 0..diff:
-      if full[i + ff[0]] !=? base[i + bb[0]]:
-        same = false
-        break
-    if not same: break
-    ff = (0, -1)
-    bb = (0, -1)
-  #  for i in 0..diff:
-  #    result.add base[i + bb[0]]
-
-  # /foo/bar/xxx/ -- base
-  # /foo/bar/baz  -- full path
-  #   ../baz
-  # every directory that is in 'base', needs to add '..'
-  while true:
-    if bb[1] >= bb[0]:
-      if result.len > 0 and result[^1] != sep:
-        result.add sep
-      result.add ".."
-    if not b.hasNext(base): break
-    bb = b.next(base)
-
-  # add the rest of 'full':
-  while true:
-    if ff[1] >= ff[0]:
-      if result.len > 0 and result[^1] != sep:
-        result.add sep
-      for i in 0..ff[1] - ff[0]:
-        result.add full[i + ff[0]]
-    if not f.hasNext(full): break
-    ff = f.next(full)
-
 when true:
-  proc eqImpl(x, y: string): bool =
-    when FileSystemCaseSensitive:
-      result = cmpIgnoreCase(canon x, canon y) == 0
-    else:
-      result = canon(x) == canon(y)
+  proc eqImpl(x, y: string): bool {.inline.} =
+    result = cmpPaths(x, y) == 0
 
   proc `==`*(x, y: AbsoluteFile): bool = eqImpl(x.string, y.string)
   proc `==`*(x, y: AbsoluteDir): bool = eqImpl(x.string, y.string)
@@ -180,20 +58,20 @@ when true:
     assert(not isAbsolute(f.string))
     result = AbsoluteFile newStringOfCap(base.string.len + f.string.len)
     var state = 0
-    canon(base.string, result.string, state)
-    canon(f.string, result.string, state)
+    addNormalizePath(base.string, result.string, state)
+    addNormalizePath(f.string, result.string, state)
 
   proc `/`*(base: AbsoluteDir; f: RelativeDir): AbsoluteDir =
     #assert isAbsolute(base.string)
     assert(not isAbsolute(f.string))
     result = AbsoluteDir newStringOfCap(base.string.len + f.string.len)
     var state = 0
-    canon(base.string, result.string, state)
-    canon(f.string, result.string, state)
+    addNormalizePath(base.string, result.string, state)
+    addNormalizePath(f.string, result.string, state)
 
   proc relativeTo*(fullPath: AbsoluteFile, baseFilename: AbsoluteDir;
-                   sep = canonDirSep): RelativeFile =
-    RelativeFile(relativeTo(fullPath.string, baseFilename.string, sep))
+                   sep = DirSep): RelativeFile =
+    RelativeFile(relativePath(fullPath.string, baseFilename.string, sep))
 
   proc toAbsolute*(file: string; base: AbsoluteDir): AbsoluteFile =
     if isAbsolute(file): result = AbsoluteFile(file)
@@ -208,37 +86,8 @@ when true:
   proc writeFile*(x: AbsoluteFile; content: string) {.borrow.}
 
 when isMainModule:
-  doAssert canon"/foo/../bar" == "/bar"
-  doAssert canon"foo/../bar" == "bar"
-
-  doAssert canon"/f/../bar///" == "/bar"
-  doAssert canon"f/..////bar" == "bar"
-
-  doAssert canon"../bar" == "../bar"
-  doAssert canon"/../bar" == "/../bar"
-
-  doAssert canon("foo/../../bar/") == "../bar"
-  doAssert canon("./bla/blob/") == "bla/blob"
-  doAssert canon(".hiddenFile") == ".hiddenFile"
-  doAssert canon("./bla/../../blob/./zoo.nim") == "../blob/zoo.nim"
-
-  doAssert canon("C:/file/to/this/long") == "C:/file/to/this/long"
-  doAssert canon("") == ""
-  doAssert canon("foobar") == "foobar"
-  doAssert canon("f/////////") == "f"
-
-  doAssert relativeTo("/foo/bar//baz.nim", "/foo") == "bar/baz.nim"
-
-  doAssert relativeTo("/Users/me/bar/z.nim", "/Users/other/bad") == "../../me/bar/z.nim"
-
-  doAssert relativeTo("/Users/me/bar/z.nim", "/Users/other") == "../me/bar/z.nim"
-  doAssert relativeTo("/Users///me/bar//z.nim", "//Users/") == "me/bar/z.nim"
-  doAssert relativeTo("/Users/me/bar/z.nim", "/Users/me") == "bar/z.nim"
-  doAssert relativeTo("", "/users/moo") == ""
-  doAssert relativeTo("foo", "") == "foo"
-
   doAssert AbsoluteDir"/Users/me///" / RelativeFile"z.nim" == AbsoluteFile"/Users/me/z.nim"
-  doAssert relativeTo("/foo/bar.nim", "/foo/") == "bar.nim"
+  doAssert relativePath("/foo/bar.nim", "/foo/", '/') == "bar.nim"
 
 when isMainModule and defined(windows):
   let nasty = string(AbsoluteDir(r"C:\Users\rumpf\projects\nim\tests\nimble\nimbleDir\linkedPkgs\pkgB-#head\../../simplePkgs/pkgB-#head/") / RelativeFile"pkgA/module.nim")
rtik K. Agaram <vc@akkartik.com> 2015-02-19 16:46:47 -0800 788 - reorg, showing off how tangle makes it' href='/akkartik/mu/commit/cpp/013arithmetic?h=main&id=8d7839b99723553c08b33c30980a001e7a3557b6'>8d7839b9 ^
57699011 ^
8d7839b9 ^
88be3dbc ^
8d7839b9 ^
bc643692 ^

5497090a ^
8d7839b9 ^
57699011 ^
8d7839b9 ^
0487a30e ^

bc643692 ^
0487a30e ^


a0d7a155 ^
5f98a10c ^
a0d7a155 ^


5f98a10c ^
a0d7a155 ^

5f98a10c ^
a0d7a155 ^


5f98a10c ^
a0d7a155 ^
1848b18f ^

8d7839b9 ^
795f5244 ^
e236973b ^
42b31beb ^
a0d7a155 ^
795f5244 ^
a0d7a155 ^


4e49b29e ^
a0d7a155 ^
795f5244 ^
a0d7a155 ^



795f5244 ^
e4630643 ^

5fdd8e96 ^
795f5244 ^
a17ffca8 ^
f88883e2 ^
a0d7a155 ^



fca0ebbe ^
a0d7a155 ^
827898fc ^
cfb142b9 ^
827898fc ^
42b31beb ^

8d7839b9 ^
88be3dbc ^
8d7839b9 ^
bc643692 ^
8d7839b9 ^
57699011 ^
8d7839b9 ^
88be3dbc ^
8d7839b9 ^
bc643692 ^

5497090a ^
8d7839b9 ^
57699011 ^
8d7839b9 ^
0487a30e ^

bc643692 ^
0487a30e ^


1848b18f ^

8d7839b9 ^
795f5244 ^
e236973b ^
a0d7a155 ^


795f5244 ^
a0d7a155 ^



795f5244 ^
a0d7a155 ^

5fdd8e96 ^
795f5244 ^
a0d7a155 ^



8d7839b9 ^
42b31beb ^
fca0ebbe ^
ac0e9db5 ^
827898fc ^
0487a30e ^

827898fc ^
42b31beb ^

8d7839b9 ^
88be3dbc ^
8d7839b9 ^
bc643692 ^
8d7839b9 ^
57699011 ^
8d7839b9 ^
88be3dbc ^
8d7839b9 ^
bc643692 ^

5497090a ^
8d7839b9 ^
57699011 ^
8d7839b9 ^
0487a30e ^

bc643692 ^
0487a30e ^


1848b18f ^

8d7839b9 ^
795f5244 ^
e236973b ^
42b31beb ^
a0d7a155 ^
795f5244 ^
e4630643 ^

a0d7a155 ^

795f5244 ^
a0d7a155 ^



795f5244 ^
a0d7a155 ^

5fdd8e96 ^
795f5244 ^
a17ffca8 ^
f88883e2 ^
a0d7a155 ^



fca0ebbe ^
a0d7a155 ^
827898fc ^
cfb142b9 ^
827898fc ^
42b31beb ^

8d7839b9 ^
88be3dbc ^
8d7839b9 ^
bc643692 ^
8d7839b9 ^
57699011 ^
8d7839b9 ^
88be3dbc ^
8d7839b9 ^
bc643692 ^

5497090a ^
8d7839b9 ^
57699011 ^
8d7839b9 ^
0487a30e ^

bc643692 ^
0487a30e ^


5497090a ^

1848b18f ^

8d7839b9 ^
795f5244 ^
e236973b ^
42b31beb ^
a0d7a155 ^
795f5244 ^
e4630643 ^

a0d7a155 ^
795f5244 ^
a17ffca8 ^
f88883e2 ^
a0d7a155 ^
795f5244 ^
a0d7a155 ^


5fdd8e96 ^
795f5244 ^
a0d7a155 ^







a17ffca8 ^


5f98a10c ^
a17ffca8 ^



5497090a ^
827898fc ^

42b31beb ^

8d7839b9 ^
88be3dbc ^
8d7839b9 ^
bc643692 ^
8d7839b9 ^
57699011 ^
57699011 ^
8d7839b9 ^
88be3dbc ^
8d7839b9 ^
bc643692 ^

5497090a ^
8d7839b9 ^
57699011 ^
57699011 ^
fca0ebbe ^


bc643692 ^
fca0ebbe ^

ac0e9db5 ^
a17ffca8 ^






5f98a10c ^
a17ffca8 ^



5f98a10c ^
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316