aboutsummaryrefslogtreecommitdiffstats
path: root/util/folder/pathsplit.h
blob: af14c672d4bd1758d7a84c16a659765a6633d12e (plain) (blame)
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
#pragma once

#include <util/generic/vector.h>
#include <util/generic/strbuf.h>
#include <util/generic/string.h> 
#include <util/string/ascii.h>

//do not own any data
struct TPathSplitStore: public TVector<TStringBuf> { 
    TStringBuf Drive;
    bool IsAbsolute = false;

    void AppendComponent(const TStringBuf comp);
    TStringBuf Extension() const;

protected:
    TString DoReconstruct(const TStringBuf slash) const; 

    inline void DoAppendHint(size_t hint) {
        reserve(size() + hint);
    }
};

struct TPathSplitTraitsUnix: public TPathSplitStore {
    static constexpr char MainPathSep = '/';

    inline TString Reconstruct() const { 
        return DoReconstruct(TStringBuf("/"));
    }

    static constexpr bool IsPathSep(const char c) noexcept {
        return c == '/';
    }

    static inline bool IsAbsolutePath(const TStringBuf path) noexcept {
        return path && IsPathSep(path[0]);
    }

    void DoParseFirstPart(const TStringBuf part);
    void DoParsePart(const TStringBuf part);
};

struct TPathSplitTraitsWindows: public TPathSplitStore {
    static constexpr char MainPathSep = '\\';

    inline TString Reconstruct() const { 
        return DoReconstruct(TStringBuf("\\"));
    }

    static constexpr bool IsPathSep(char c) noexcept {
        return c == '/' || c == '\\';
    }

    static inline bool IsAbsolutePath(const TStringBuf path) noexcept {
        return path && (IsPathSep(path[0]) || (path.size() > 1 && path[1] == ':' && IsAsciiAlpha(path[0]) && (path.size() == 2 || IsPathSep(path[2]))));
    }

    void DoParseFirstPart(const TStringBuf part);
    void DoParsePart(const TStringBuf part);
};

#if defined(_unix_)
using TPathSplitTraitsLocal = TPathSplitTraitsUnix;
#else
using TPathSplitTraitsLocal = TPathSplitTraitsWindows;
#endif

template <class TTraits>
class TPathSplitBase: public TTraits {
public:
    inline TPathSplitBase() = default;

    inline TPathSplitBase(const TStringBuf part) {
        this->ParseFirstPart(part);
    }

    inline TPathSplitBase& AppendHint(size_t hint) {
        this->DoAppendHint(hint);

        return *this;
    }

    inline TPathSplitBase& ParseFirstPart(const TStringBuf part) {
        this->DoParseFirstPart(part);

        return *this;
    }

    inline TPathSplitBase& ParsePart(const TStringBuf part) {
        this->DoParsePart(part);

        return *this;
    }

    template <class It>
    inline TPathSplitBase& AppendMany(It b, It e) {
        this->AppendHint(e - b);

        while (b != e) {
            this->AppendComponent(*b++);
        }

        return *this;
    }
};

using TPathSplit = TPathSplitBase<TPathSplitTraitsLocal>;
using TPathSplitUnix = TPathSplitBase<TPathSplitTraitsUnix>;
using TPathSplitWindows = TPathSplitBase<TPathSplitTraitsWindows>;

TString JoinPaths(const TPathSplit& p1, const TPathSplit& p2); 

TStringBuf CutExtension(const TStringBuf fileName);