pathnorm.nim 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2018 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## OS-Path normalization. Used by `os.nim` but also
  10. ## generally useful for dealing with paths.
  11. ##
  12. ## Unstable API.
  13. # Yes, this uses import here, not include so that
  14. # we don't end up exporting these symbols from pathnorm and os:
  15. import std/private/osseps
  16. type
  17. PathIter* = object
  18. i, prev: int
  19. notFirst: bool
  20. proc hasNext*(it: PathIter; x: string): bool =
  21. it.i < x.len
  22. proc next*(it: var PathIter; x: string): (int, int) =
  23. it.prev = it.i
  24. if not it.notFirst and x[it.i] in {DirSep, AltSep}:
  25. # absolute path:
  26. inc it.i
  27. else:
  28. while it.i < x.len and x[it.i] notin {DirSep, AltSep}: inc it.i
  29. if it.i > it.prev:
  30. result = (it.prev, it.i-1)
  31. elif hasNext(it, x):
  32. result = next(it, x)
  33. # skip all separators:
  34. while it.i < x.len and x[it.i] in {DirSep, AltSep}: inc it.i
  35. it.notFirst = true
  36. iterator dirs(x: string): (int, int) =
  37. var it = default PathIter
  38. while hasNext(it, x): yield next(it, x)
  39. proc isDot(x: string; bounds: (int, int)): bool =
  40. bounds[1] == bounds[0] and x[bounds[0]] == '.'
  41. proc isDotDot(x: string; bounds: (int, int)): bool =
  42. bounds[1] == bounds[0] + 1 and x[bounds[0]] == '.' and x[bounds[0]+1] == '.'
  43. proc isSlash(x: string; bounds: (int, int)): bool =
  44. bounds[1] == bounds[0] and x[bounds[0]] in {DirSep, AltSep}
  45. when doslikeFileSystem:
  46. import std/private/ntpath
  47. proc addNormalizePath*(x: string; result: var string; state: var int;
  48. dirSep = DirSep) =
  49. ## Low level proc. Undocumented.
  50. when doslikeFileSystem: # Add Windows drive at start without normalization
  51. var x = x
  52. if result == "":
  53. let (drive, file) = splitDrive(x)
  54. x = file
  55. result.add drive
  56. for c in result.mitems:
  57. if c in {DirSep, AltSep}:
  58. c = dirSep
  59. # state: 0th bit set if isAbsolute path. Other bits count
  60. # the number of path components.
  61. var it: PathIter
  62. it.notFirst = (state shr 1) > 0
  63. if it.notFirst:
  64. while it.i < x.len and x[it.i] in {DirSep, AltSep}: inc it.i
  65. while hasNext(it, x):
  66. let b = next(it, x)
  67. if (state shr 1 == 0) and isSlash(x, b):
  68. if result.len == 0 or result[result.len - 1] notin {DirSep, AltSep}:
  69. result.add dirSep
  70. state = state or 1
  71. elif isDotDot(x, b):
  72. if (state shr 1) >= 1:
  73. var d = result.len
  74. # f/..
  75. # We could handle stripping trailing sep here: foo// => foo like this:
  76. # while (d-1) > (state and 1) and result[d-1] in {DirSep, AltSep}: dec d
  77. # but right now we instead handle it inside os.joinPath
  78. # strip path component: foo/bar => foo
  79. while (d-1) > (state and 1) and result[d-1] notin {DirSep, AltSep}:
  80. dec d
  81. if d > 0:
  82. setLen(result, d-1)
  83. dec state, 2
  84. else:
  85. if result.len > 0 and result[result.len - 1] notin {DirSep, AltSep}:
  86. result.add dirSep
  87. result.add substr(x, b[0], b[1])
  88. elif isDot(x, b):
  89. discard "discard the dot"
  90. elif b[1] >= b[0]:
  91. if result.len > 0 and result[result.len - 1] notin {DirSep, AltSep}:
  92. result.add dirSep
  93. result.add substr(x, b[0], b[1])
  94. inc state, 2
  95. if result == "" and x != "": result = "."
  96. proc normalizePath*(path: string; dirSep = DirSep): string =
  97. runnableExamples:
  98. when defined(posix):
  99. doAssert normalizePath("./foo//bar/../baz") == "foo/baz"
  100. ## - Turns multiple slashes into single slashes.
  101. ## - Resolves `'/foo/../bar'` to `'/bar'`.
  102. ## - Removes `'./'` from the path, but `"foo/.."` becomes `"."`.
  103. result = newStringOfCap(path.len)
  104. var state = 0
  105. addNormalizePath(path, result, state, dirSep)