Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:26:59

0001 // Licensed to the Apache Software Foundation (ASF) under one
0002 // or more contributor license agreements.  See the NOTICE file
0003 // distributed with this work for additional information
0004 // regarding copyright ownership.  The ASF licenses this file
0005 // to you under the Apache License, Version 2.0 (the
0006 // "License"); you may not use this file except in compliance
0007 // with the License.  You may obtain a copy of the License at
0008 //
0009 //   http://www.apache.org/licenses/LICENSE-2.0
0010 //
0011 // Unless required by applicable law or agreed to in writing,
0012 // software distributed under the License is distributed on an
0013 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0014 // KIND, either express or implied.  See the License for the
0015 // specific language governing permissions and limitations
0016 // under the License.
0017 
0018 #pragma once
0019 
0020 #include <optional>
0021 #include <string>
0022 #include <string_view>
0023 #include <utility>
0024 #include <vector>
0025 
0026 #include "arrow/type_fwd.h"
0027 
0028 namespace arrow {
0029 namespace fs {
0030 namespace internal {
0031 
0032 constexpr char kSep = '/';
0033 
0034 // Computations on abstract paths (not local paths with system-dependent behaviour).
0035 // Abstract paths are typically used in URIs.
0036 
0037 // Split an abstract path into its individual components.
0038 ARROW_EXPORT
0039 std::vector<std::string> SplitAbstractPath(const std::string& path, char sep = kSep);
0040 
0041 // Slice the individual components of an abstract path and combine them
0042 //
0043 // If offset or length are negative then an empty string is returned
0044 // If offset is >= the number of components then an empty string is returned
0045 // If offset + length is >= the number of components then length is truncated
0046 ARROW_EXPORT
0047 std::string SliceAbstractPath(const std::string& path, int offset, int length,
0048                               char sep = kSep);
0049 
0050 // Return the extension of the file
0051 ARROW_EXPORT std::string GetAbstractPathExtension(const std::string& s);
0052 
0053 // Return the depth (number of components) of an abstract path
0054 //
0055 // Trailing slashes do not count towards depth
0056 // Leading slashes do not count towards depth
0057 //
0058 // The root path ("/") has depth 0
0059 ARROW_EXPORT int GetAbstractPathDepth(std::string_view path);
0060 
0061 // Return the parent directory and basename of an abstract path.  Both values may be
0062 // empty.
0063 ARROW_EXPORT
0064 std::pair<std::string, std::string> GetAbstractPathParent(const std::string& s);
0065 
0066 // Validate an abstract path.
0067 ARROW_EXPORT
0068 Status ValidateAbstractPath(std::string_view path);
0069 
0070 // Validate the components of an abstract path.
0071 ARROW_EXPORT
0072 Status ValidateAbstractPathParts(const std::vector<std::string>& parts);
0073 
0074 // Append a non-empty stem to an abstract path.
0075 ARROW_EXPORT
0076 std::string ConcatAbstractPath(std::string_view base, std::string_view stem);
0077 
0078 // Make path relative to base, if it starts with base.  Otherwise error out.
0079 ARROW_EXPORT
0080 Result<std::string> MakeAbstractPathRelative(const std::string& base,
0081                                              const std::string& path);
0082 
0083 ARROW_EXPORT
0084 std::string EnsureLeadingSlash(std::string_view s);
0085 
0086 ARROW_EXPORT
0087 std::string_view RemoveLeadingSlash(std::string_view s);
0088 
0089 ARROW_EXPORT
0090 std::string EnsureTrailingSlash(std::string_view s);
0091 
0092 /// \brief remove the forward slash (if any) from the given path
0093 /// \param s the input path
0094 /// \param preserve_root if true, allow a path of just "/" to remain unchanged
0095 ARROW_EXPORT
0096 std::string_view RemoveTrailingSlash(std::string_view s, bool preserve_root = false);
0097 
0098 ARROW_EXPORT
0099 Status AssertNoTrailingSlash(std::string_view s);
0100 
0101 inline bool HasTrailingSlash(std::string_view s) {
0102   return !s.empty() && s.back() == kSep;
0103 }
0104 
0105 inline bool HasLeadingSlash(std::string_view s) {
0106   return !s.empty() && s.front() == kSep;
0107 }
0108 
0109 ARROW_EXPORT
0110 bool IsAncestorOf(std::string_view ancestor, std::string_view descendant);
0111 
0112 ARROW_EXPORT
0113 std::optional<std::string_view> RemoveAncestor(std::string_view ancestor,
0114                                                std::string_view descendant);
0115 
0116 /// Return a vector of ancestors between a base path and a descendant.
0117 /// For example,
0118 ///
0119 /// AncestorsFromBasePath("a/b", "a/b/c/d/e") -> ["a/b/c", "a/b/c/d"]
0120 ARROW_EXPORT
0121 std::vector<std::string> AncestorsFromBasePath(std::string_view base_path,
0122                                                std::string_view descendant);
0123 
0124 /// Given a vector of paths of directories which must be created, produce a the minimal
0125 /// subset for passing to CreateDir(recursive=true) by removing redundant parent
0126 /// directories
0127 ARROW_EXPORT
0128 std::vector<std::string> MinimalCreateDirSet(std::vector<std::string> dirs);
0129 
0130 // Join the components of an abstract path.
0131 template <class StringIt>
0132 std::string JoinAbstractPath(StringIt it, StringIt end, char sep = kSep) {
0133   std::string path;
0134   for (; it != end; ++it) {
0135     if (it->empty()) continue;
0136 
0137     if (!path.empty()) {
0138       path += sep;
0139     }
0140     path += *it;
0141   }
0142   return path;
0143 }
0144 
0145 template <class StringRange>
0146 std::string JoinAbstractPath(const StringRange& range, char sep = kSep) {
0147   return JoinAbstractPath(range.begin(), range.end(), sep);
0148 }
0149 
0150 /// Convert slashes to backslashes, on all platforms.  Mostly useful for testing.
0151 ARROW_EXPORT
0152 std::string ToBackslashes(std::string_view s);
0153 
0154 /// Ensure a local path is abstract, by converting backslashes to regular slashes
0155 /// on Windows.  Return the path unchanged on other systems.
0156 ARROW_EXPORT
0157 std::string ToSlashes(std::string_view s);
0158 
0159 ARROW_EXPORT
0160 bool IsEmptyPath(std::string_view s);
0161 
0162 ARROW_EXPORT
0163 bool IsLikelyUri(std::string_view s);
0164 
0165 class ARROW_EXPORT Globber {
0166  public:
0167   ~Globber();
0168   explicit Globber(std::string pattern);
0169   bool Matches(const std::string& path);
0170 
0171  protected:
0172   struct Impl;
0173   std::unique_ptr<Impl> impl_;
0174 };
0175 
0176 }  // namespace internal
0177 }  // namespace fs
0178 }  // namespace arrow