-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.cpp
193 lines (170 loc) · 4.1 KB
/
path.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "path.h"
#include "ustr8.h"
#include <ostream>
using namespace pun;
const char* Path::PHP_NAME = "Pun\\Path";
const char* WINSEP = "\\";
const char* UNIXSEP = "/";
#ifdef _WIN32
const char* DS = "\\";
const char* ODS = "/";
#else
const char* DS = "/";
const char* ODS = "\\";
#endif
std::string Path::OSPathSep(DS);
std::string Path::OtherSep(ODS);
void
Path::setup_ext(Php::Extension& ext)
{
Php::Class<Path> cpath(Path::PHP_NAME);
cpath.method<&Path::endSep> ("endSep");
cpath.method<&Path::noEndSep> ("noEndSep");
cpath.method<&Path::native> ("native");
cpath.method<&Path::sep> ("sep");
cpath.method<&Path::startsWith> ("startsWith");
cpath.method<&Path::endsWith> ("endsWith");
ext.add(std::move(cpath));
}
Php::Value
Path::sep()
{
return Path::OSPathSep;
}
static bool arg0_view(svx::string_view& vw, Php::Value& arg, bool& isu8)
{
if (arg.isString()) {
vw = svx::string_view((const char*) arg, arg.size());
isu8 = false;
return true;
}
else {
UStr8* u8 = UStr8::get_UStr8(arg);
if (u8 != nullptr) {
vw = u8->fn_getView();
isu8 = true;
return true;
}
else {
return false;
}
}
}
bool
Path::fn_native(svx::string_view& src, std::string& result){
svx::string_view wrong(ODS, 1);
svx::string_view right(DS, 1);
return ( pun::replaceAll(src,wrong,right,result) > 0);
}
Php::Value
Path::startsWith(Php::Parameters& param)
{
if(param.size() < 2) {
return false;
}
Php::Value& arg0 = param[0];
Php::Value& arg1 = param[1];
svx::string_view src;
svx::string_view bit;
bool isu8;
if (arg0_view(src, arg0,isu8) && arg0_view( bit, arg1, isu8))
{
auto bsize = bit.size();
return (src.substr(0,bsize) == bit) ? true : false;
}
return false;
}
Php::Value
Path::endsWith(Php::Parameters& param)
{
if(param.size() < 2) {
return false;
}
Php::Value& arg0 = param[0];
Php::Value& arg1 = param[1];
svx::string_view src;
svx::string_view bit;
bool isu8;
if (arg0_view(src, arg0,isu8) && arg0_view( bit, arg1, isu8))
{
auto bsize = bit.size();
auto slen = src.size();
if (bsize > slen)
return false;
return (src.substr(slen-bsize,bsize) == bit) ? true : false;
}
return false;
}
Php::Value
Path::native(Php::Parameters& param)
{
if (param.size() == 0)
return Php::Value();
svx::string_view src;
bool isu8 = false;
Php::Value& arg = param[0];
if (!arg0_view(src, arg, isu8))
return Php::Value();//null
std::string result;
if (fn_native(src, result))
{
if (isu8) {
return UStr8::make_UStr8(result);
}
else {
return result;
}
}
return arg;
}
Php::Value
Path::endSep(Php::Parameters& param)
{
if (param.size() == 0)
return Php::Value();
svx::string_view src;
bool isu8 = false;
Php::Value& arg = param[0];
if (!arg0_view(src, arg, isu8))
return Php::Value();
if (src.size() > 0) {
auto last = src.back();
if (last == DS[0])
return arg;
std::string result;
result.reserve(src.size()+1);
result.assign(src.data(), src.size());
result += DS[0];
if (isu8)
return UStr8::make_UStr8(result);
else
return result;
}
else {
return Path::OSPathSep;
}
}
Php::Value
Path::noEndSep(Php::Parameters& param)
{
if (param.size() == 0)
return Php::Value();
svx::string_view src;
bool isu8 = false;
Php::Value& arg = param[0];
if (!arg0_view(src, arg, isu8))
return Php::Value();
if (src.size() > 0) {
auto last = src.back();
if (last == DS[0]) {
src.remove_suffix(1);
std::string result(src.data(), src.size());
if (isu8)
return UStr8::make_UStr8(result);
else
return result;
}
return arg;
}
return Php::Value();
}