mirror of
https://github.com/nushell/nushell.git
synced 2025-08-15 00:22:26 +02:00
If save
-ing with non-existing parent dir, return directory_not_found (#15961)
This commit is contained in:
@ -768,6 +768,30 @@ impl EngineState {
|
||||
&[0u8; 0]
|
||||
}
|
||||
|
||||
/// If the span's content starts with the given prefix, return two subspans
|
||||
/// corresponding to this prefix, and the rest of the content.
|
||||
pub fn span_match_prefix(&self, span: Span, prefix: &[u8]) -> Option<(Span, Span)> {
|
||||
let contents = self.get_span_contents(span);
|
||||
|
||||
if contents.starts_with(prefix) {
|
||||
span.split_at(prefix.len())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// If the span's content ends with the given postfix, return two subspans
|
||||
/// corresponding to the rest of the content, and this postfix.
|
||||
pub fn span_match_postfix(&self, span: Span, prefix: &[u8]) -> Option<(Span, Span)> {
|
||||
let contents = self.get_span_contents(span);
|
||||
|
||||
if contents.ends_with(prefix) {
|
||||
span.split_at(span.len() - prefix.len())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the global config from the engine state.
|
||||
///
|
||||
/// Use [`Stack::get_config()`] instead whenever the `Stack` is available, as it takes into
|
||||
|
@ -132,6 +132,45 @@ impl Span {
|
||||
Self::new(self.start - offset, self.end - offset)
|
||||
}
|
||||
|
||||
/// Return length of the slice.
|
||||
pub fn len(&self) -> usize {
|
||||
self.end - self.start
|
||||
}
|
||||
|
||||
/// Indicate if slice has length 0.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.start == self.end
|
||||
}
|
||||
|
||||
/// Return another span fully inside the [`Span`].
|
||||
///
|
||||
/// `start` and `end` are relative to `self.start`, and must lie within the `Span`.
|
||||
/// In other words, both `start` and `end` must be `<= self.len()`.
|
||||
pub fn subspan(&self, offset_start: usize, offset_end: usize) -> Option<Self> {
|
||||
let len = self.len();
|
||||
|
||||
if offset_start > len || offset_end > len || offset_start > offset_end {
|
||||
None
|
||||
} else {
|
||||
Some(Self::new(
|
||||
self.start + offset_start,
|
||||
self.start + offset_end,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Return two spans that split the ['Span'] at the given position.
|
||||
pub fn split_at(&self, offset: usize) -> Option<(Self, Self)> {
|
||||
if offset < self.len() {
|
||||
Some((
|
||||
Self::new(self.start, self.start + offset),
|
||||
Self::new(self.start + offset, self.end),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn contains(&self, pos: usize) -> bool {
|
||||
self.start <= pos && pos < self.end
|
||||
}
|
||||
|
Reference in New Issue
Block a user