Skip to content

Commit b3a4254

Browse files
committed
优化
1 parent 8cd07ba commit b3a4254

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

binding/lua_filesystem.cpp

+36-22
Original file line numberDiff line numberDiff line change
@@ -193,24 +193,6 @@ namespace bee::lua_filesystem {
193193
}
194194
}
195195

196-
path_view to_view() {
197-
switch (st) {
198-
case status::ptr:
199-
return ptr->native();
200-
case status::str:
201-
#if defined(_WIN32)
202-
conv_val();
203-
return val.native();
204-
#else
205-
return str;
206-
#endif
207-
case status::val:
208-
return val.native();
209-
default:
210-
std::unreachable();
211-
}
212-
}
213-
214196
private:
215197
void conv_val() {
216198
#if defined(_WIN32)
@@ -233,6 +215,31 @@ namespace bee::lua_filesystem {
233215
};
234216
};
235217

218+
class path_view_ref {
219+
public:
220+
path_view_ref(const fs::path* ptr) noexcept
221+
: view { ptr->native() } {
222+
}
223+
path_view_ref(zstring_view view) noexcept
224+
#if defined(_WIN32)
225+
: str(wtf8::u2w(view))
226+
, view { str }
227+
#else
228+
: view { view }
229+
#endif
230+
{
231+
}
232+
operator path_view() const noexcept {
233+
return view;
234+
}
235+
236+
private:
237+
#if defined(_WIN32)
238+
std::wstring str;
239+
#endif
240+
path_view view;
241+
};
242+
236243
static fs::path& getpath(lua_State* L, int idx) {
237244
return lua::checkudata<fs::path>(L, idx);
238245
}
@@ -244,6 +251,13 @@ namespace bee::lua_filesystem {
244251
return &getpath(L, idx);
245252
}
246253

254+
static path_view_ref getpathview(lua_State* L, int idx) {
255+
if (lua_type(L, idx) == LUA_TSTRING) {
256+
return lua::checkstrview(L, idx);
257+
}
258+
return &getpath(L, idx);
259+
}
260+
247261
namespace path {
248262
static int constructor(lua_State* L) {
249263
if (lua_gettop(L) == 0) {
@@ -958,8 +972,8 @@ namespace bee::lua_filesystem {
958972
}
959973

960974
static int filelock(lua_State* L) {
961-
auto path = getpathref(L, 1);
962-
auto fd = file_handle::lock(path.to_view());
975+
auto path = getpathview(L, 1);
976+
auto fd = file_handle::lock(path);
963977
if (!fd) {
964978
return lua::push_error(L, error::sys_errmsg("filelock"));
965979
}
@@ -974,8 +988,8 @@ namespace bee::lua_filesystem {
974988
}
975989

976990
static int fullpath(lua_State* L) {
977-
auto path = getpathref(L, 1);
978-
auto fd = file_handle::open_link(path.to_view());
991+
auto path = getpathview(L, 1);
992+
auto fd = file_handle::open_link(path);
979993
if (!fd) {
980994
return lua::push_error(L, error::sys_errmsg("fullpath"));
981995
}

0 commit comments

Comments
 (0)