11cb0ef41Sopenharmony_ci// unix absolute paths are also absolute on win32, so we use this for both
21cb0ef41Sopenharmony_ciconst { isAbsolute, parse } = require('path').win32
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// returns [root, stripped]
51cb0ef41Sopenharmony_ci// Note that windows will think that //x/y/z/a has a "root" of //x/y, and in
61cb0ef41Sopenharmony_ci// those cases, we want to sanitize it to x/y/z/a, not z/a, so we strip /
71cb0ef41Sopenharmony_ci// explicitly if it's the first character.
81cb0ef41Sopenharmony_ci// drive-specific relative paths on Windows get their root stripped off even
91cb0ef41Sopenharmony_ci// though they are not absolute, so `c:../foo` becomes ['c:', '../foo']
101cb0ef41Sopenharmony_cimodule.exports = path => {
111cb0ef41Sopenharmony_ci  let r = ''
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  let parsed = parse(path)
141cb0ef41Sopenharmony_ci  while (isAbsolute(path) || parsed.root) {
151cb0ef41Sopenharmony_ci    // windows will think that //x/y/z has a "root" of //x/y/
161cb0ef41Sopenharmony_ci    // but strip the //?/C:/ off of //?/C:/path
171cb0ef41Sopenharmony_ci    const root = path.charAt(0) === '/' && path.slice(0, 4) !== '//?/' ? '/'
181cb0ef41Sopenharmony_ci      : parsed.root
191cb0ef41Sopenharmony_ci    path = path.slice(root.length)
201cb0ef41Sopenharmony_ci    r += root
211cb0ef41Sopenharmony_ci    parsed = parse(path)
221cb0ef41Sopenharmony_ci  }
231cb0ef41Sopenharmony_ci  return [r, path]
241cb0ef41Sopenharmony_ci}
25