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
// Copyright © 2020 Alexandra Frydl
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

//! Functions for working with Unicode file system paths.

pub use af_core_macros::{path_join as join, path_normalize as normalize, path_resolve as resolve};

#[doc(inline)]
pub use std::path::{is_separator, MAIN_SEPARATOR as SEPARATOR};

use crate::{env, prelude::*};
use std::cell::RefCell;
use std::path::Path;

thread_local! {
  /// A thread-local buffer for operations that need temporary storage.
  static THREAD_BUFFER: RefCell<String> = default();
}

/// Appends a relative path to a base path.
///
/// If `relative` is an absolute path, the base path is replaced completely.
pub fn append(base: &mut String, relative: &str) {
  if is_absolute(relative) {
    base.replace_range(.., relative);
    return;
  }

  match base.chars().rev().next() {
    None => base.replace_range(.., relative),
    Some(c) if is_separator(c) => base.push_str(relative),
    Some(_) => {
      base.reserve(relative.len() + 1);
      base.push(SEPARATOR);
      base.push_str(relative);
    }
  }
}

/// Returns `true` if the given path is absolute.
pub fn is_absolute(path: &str) -> bool {
  as_std(path).is_absolute()
}

/// Joins a base path and a relative path.
///
/// If `relative` is an absolute path, it is returned unmodified.
pub fn join<'a, 'b>(base: impl PathLike<'b>, relative: impl PathLike<'a>) -> Cow<'a, str> {
  let relative = relative.to_cow();

  if is_absolute(&relative) {
    return relative;
  }

  let mut output = base.to_owned();

  append(&mut output, &relative);

  output.into()
}

/// Returns the last component of the path.
///
/// If `path` is a root or empty path, this function returns `None`.
pub fn last(path: &str) -> Option<&str> {
  as_std(path).file_name()?.to_str()
}

/// Normalizes a path.
pub fn normalize(path: &mut String) {
  THREAD_BUFFER.with(|buffer| {
    let mut buffer = buffer.borrow_mut();

    buffer.clear();

    normalize_into(path, &mut buffer);

    path.replace_range(.., &buffer);
  })
}

/// Returns a normalized version of the given path.
pub fn normalized<'a>(path: impl PathLike<'a>) -> Cow<'a, str> {
  let path = path.to_cow();

  THREAD_BUFFER.with(|buffer| {
    let mut buffer = buffer.borrow_mut();

    buffer.clear();
    normalize_into(&path, &mut buffer);

    match path.as_ref() == *buffer {
      true => path,
      false => buffer.clone().into(),
    }
  })
}

/// Returns the parent of the given path.
///
/// If `path` is a root or empty path, this function returns `None`.
pub fn parent(path: &str) -> Option<&str> {
  as_std(path).parent()?.to_str()
}

/// Removes the last component from the path and returns it.
///
/// If the path is a root or empty path, this function does nothing and returns
/// `None`.
pub fn pop(path: &mut String) -> Option<String> {
  let split_at = parent(&path)?.len();
  let lead_seps = path[split_at..].chars().take_while(|c| is_separator(*c)).count();
  let trail_seps = path.chars().rev().take_while(|c| is_separator(*c)).count();
  let mut last = path.split_off(split_at + lead_seps);

  path.truncate(split_at);
  last.truncate(last.len() - trail_seps);

  Some(last)
}

/// Resolves the given path into an absolute, normalized path.
pub fn resolve(path: &mut String) -> Result<(), env::WorkingPathError> {
  if !is_absolute(&path) {
    let mut buf = env::working_path()?;

    mem::swap(path, &mut buf);
    append(path, &buf);
  }

  normalize(path);

  Ok(())
}

/// Returns an absolute, normalized version of the given path.
pub fn resolved<'a>(path: impl PathLike<'a>) -> Result<Cow<'a, str>, env::WorkingPathError> {
  let mut path = path.to_cow();

  if is_absolute(&path) {
    return Ok(normalized(path));
  }

  resolve(path.to_mut())?;

  Ok(path)
}

/// Returns `true` if the first path starts with the second path.
pub fn starts_with(path: &str, prefix: &str) -> bool {
  as_std(path).starts_with(prefix)
}

/// Returns the given path with a trailing separator if it does not already
/// have one.
pub fn with_trailing_sep<'a>(path: impl PathLike<'a>) -> Cow<'a, str> {
  let mut path = path.to_cow();

  match path.chars().rev().next() {
    Some(c) if is_separator(c) => path,

    _ => {
      path.to_mut().push(SEPARATOR);
      path
    }
  }
}

/// Converts a value into a `&Path`.
pub fn as_std(path: &str) -> &Path {
  path.as_ref()
}

/// Normalizes the given path into the output string.
///
/// The output is expected to already be empty.
fn normalize_into(path: &str, output: &mut String) {
  for component in as_std(path).components() {
    match component {
      std::path::Component::CurDir => continue,
      std::path::Component::Normal(component) => append(output, component.to_str().unwrap()),
      std::path::Component::Prefix(prefix) => output.push_str(prefix.as_os_str().to_str().unwrap()),
      std::path::Component::RootDir => output.push(SEPARATOR),

      std::path::Component::ParentDir => {
        pop(output);
      }
    }
  }
}

/// A trait for values that can be used in path operations.
pub trait PathLike<'a>: Sized {
  /// Converts this value into a `Cow<str>`.
  fn to_cow(self) -> Cow<'a, str>;

  /// Converts this value into a `String`.
  fn to_owned(self) -> String {
    self.to_cow().into()
  }
}

// Implement `PathLike` for common string types.

impl<'a> PathLike<'a> for &'a str {
  fn to_cow(self) -> Cow<'a, str> {
    self.into()
  }
}

impl<'a> PathLike<'a> for &'a &'_ mut str {
  fn to_cow(self) -> Cow<'a, str> {
    (&**self).into()
  }
}

impl<'a> PathLike<'a> for String {
  fn to_cow(self) -> Cow<'a, str> {
    self.into()
  }
}

impl<'a> PathLike<'a> for &'a String {
  fn to_cow(self) -> Cow<'a, str> {
    self.into()
  }
}

impl<'a> PathLike<'a> for &'a &'_ mut String {
  fn to_cow(self) -> Cow<'a, str> {
    (&**self).into()
  }
}

impl<'a> PathLike<'a> for Cow<'a, str> {
  fn to_cow(self) -> Cow<'a, str> {
    self
  }
}

impl<'a> PathLike<'a> for &'a Cow<'_, str> {
  fn to_cow(self) -> Cow<'a, str> {
    self.as_ref().into()
  }
}

impl<'a> PathLike<'a> for &'a &'_ mut Cow<'_, str> {
  fn to_cow(self) -> Cow<'a, str> {
    self.as_ref().into()
  }
}

impl<'a> PathLike<'a> for &'a std::path::PathBuf {
  fn to_cow(self) -> Cow<'a, str> {
    self.to_string_lossy().to_cow()
  }
}