blob: 3e5fd21b6535b848a5483ab33a3a755387d23a4f [file] [log] [blame]
Songchun Fan3c82a302019-11-29 14:23:45 -08001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <optional>
20#include <string>
21#include <string_view>
22
23namespace android::incremental::path {
24
25namespace details {
26
27class CStrWrapper {
28public:
29 CStrWrapper(std::string_view sv);
30
31 CStrWrapper(const CStrWrapper&) = delete;
32 void operator=(const CStrWrapper&) = delete;
33 CStrWrapper(CStrWrapper&&) = delete;
34 void operator=(CStrWrapper&&) = delete;
35
36 const char* get() const { return mCstr; }
37 operator const char*() const { return get(); }
38
39private:
40 const char* mCstr;
41 std::optional<std::string> mCopy;
42};
43
44void append_next_path(std::string& res, std::string_view c);
45
46} // namespace details
47
48//
49// An std::map<> comparator that makes all nested paths to be ordered before the parents.
50//
51
52struct PathCharsLess {
53 bool operator()(char l, char r) const;
54};
55
56struct PathLess {
57 using is_transparent = void;
58 bool operator()(std::string_view l, std::string_view r) const;
59};
60
61//
62// Returns a zero-terminated version of a passed string view
63// Only makes a copy if it wasn't zero-terminated already
64// Useful for passing string view parameters to system functions.
65//
66inline details::CStrWrapper c_str(std::string_view sv) {
67 return {sv};
68}
69
Yurii Zubrytskyi4a25dfb2020-01-10 11:53:24 -080070std::string_view relativize(std::string_view parent, std::string_view nested);
71inline std::string_view relativize(const char* parent, const char* nested) {
72 return relativize(std::string_view(parent), std::string_view(nested));
73}
74inline std::string_view relativize(std::string_view parent, const char* nested) {
75 return relativize(parent, std::string_view(nested));
76}
77inline std::string_view relativize(const char* parent, std::string_view nested) {
78 return relativize(std::string_view(parent), nested);
79}
80
81std::string_view relativize(std::string&& parent, std::string_view nested) = delete;
82std::string_view relativize(std::string_view parent, std::string&& nested) = delete;
83
Songchun Fan3c82a302019-11-29 14:23:45 -080084bool isAbsolute(std::string_view path);
85std::string normalize(std::string_view path);
86std::string_view dirname(std::string_view path);
87std::string_view basename(std::string_view path);
88std::optional<bool> isEmptyDir(std::string_view dir);
89bool startsWith(std::string_view path, std::string_view prefix);
90
91template <class... Paths>
92std::string join(std::string_view first, std::string_view second, Paths&&... paths) {
93 std::string result;
94 {
95 using std::size;
96 result.reserve(first.size() + second.size() + 1 + (sizeof...(paths) + ... + size(paths)));
97 }
98 result.assign(first);
99 (details::append_next_path(result, second), ..., details::append_next_path(result, paths));
100 return result;
101}
102
103} // namespace android::incremental::path