use crate::prelude::*;
impl BetterUrl {
pub(crate) fn path_start(&self) -> usize {
self.details.path_start as usize
}
pub(crate) fn path_after(&self) -> usize {
self.details.query_mark.or(self.details.fragment_mark).map_or(self.len(), |x| x.get() as usize)
}
pub(crate) fn path_range(&self) -> Range<usize> {
self.path_start() .. self.path_after()
}
pub fn path_str(&self) -> &str {
unsafe {
self.serialization.get_unchecked(self.path_range())
}
}
pub fn path_type(&self) -> PathType {
match self.cannot_be_a_base() {
true => PathType::Opaque,
false => match self.scheme_type() {
SchemeType::File => SegmentedPathType::File .into(),
SchemeType::SpecialNotFile => SegmentedPathType::SpecialNotFile.into(),
SchemeType::NonSpecial => SegmentedPathType::NonSpecial .into(),
}
}
}
pub fn segmented_path_type(&self) -> Option<SegmentedPathType> {
self.can_be_a_base().then_some(match self.scheme_type() {
SchemeType::File => SegmentedPathType::File ,
SchemeType::SpecialNotFile => SegmentedPathType::SpecialNotFile,
SchemeType::NonSpecial => SegmentedPathType::NonSpecial ,
})
}
pub fn path(&self) -> Path<'_> {
unsafe {
Path::new_unchecked(self.path_str(), self.path_type())
}
}
pub fn path_is_segmented(&self) -> bool {
self.can_be_a_base()
}
pub fn segmented_path_str(&self) -> Option<&str> {
self.path_is_segmented().then(|| self.path_str())
}
pub fn segmented_path(&self) -> Option<SegmentedPath<'_>> {
Some(unsafe {SegmentedPath::new_unchecked(self.segmented_path_str()?, self.scheme_type().into())})
}
pub fn path_is_opaque(&self) -> bool {
self.cannot_be_a_base()
}
pub fn opaque_path_str(&self) -> Option<&str> {
self.path_is_opaque().then(|| self.path_str())
}
pub fn opaque_path(&self) -> Option<OpaquePath<'_>> {
Some(unsafe {OpaquePath::new_unchecked(self.opaque_path_str()?)})
}
pub fn path_segment_strs(&self) -> Option<SplitSlashes<'_>> {
Some(SplitSlashes(self.segmented_path_str()?.strip_prefix('/')))
}
pub fn path_segments(&self) -> Option<PathSegmentsIter<'_>> {
Some(unsafe {PathSegmentsIter::from_parts(self.path_segment_strs()?, self.scheme_type().into())})
}
pub fn path_segment_str(&self, index: isize) -> Option<&str> {
self.path_segment_strs()?.neg_nth(index)
}
pub fn path_segment(&self, index: isize) -> Option<PathSegment<'_>> {
self.path_segments()?.neg_nth(index)
}
pub fn has_path_segment(&self, index: isize) -> bool {
self.path_segment_strs().is_some_and(|mut x| x.neg_nth(index).is_some())
}
pub fn path_segment_range_str<B: RangeBounds<isize>>(&self, range: B) -> Option<&str> {
SplitSlashes(self.segmented_path_str()?.strip_prefix('/')).range(range)
}
pub fn path_segment_range<B: RangeBounds<isize>>(&self, range: B) -> Option<PathSegments<'_>> {
self.path_segment_range_str(range).map(|x| unsafe {PathSegments::new_unchecked(x, self.scheme_type().into())})
}
}