@@ -193,24 +193,6 @@ namespace bee::lua_filesystem {
193
193
}
194
194
}
195
195
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
-
214
196
private:
215
197
void conv_val () {
216
198
#if defined(_WIN32)
@@ -233,6 +215,31 @@ namespace bee::lua_filesystem {
233
215
};
234
216
};
235
217
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
+
236
243
static fs::path& getpath (lua_State* L, int idx) {
237
244
return lua::checkudata<fs::path>(L, idx);
238
245
}
@@ -244,6 +251,13 @@ namespace bee::lua_filesystem {
244
251
return &getpath (L, idx);
245
252
}
246
253
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
+
247
261
namespace path {
248
262
static int constructor (lua_State* L) {
249
263
if (lua_gettop (L) == 0 ) {
@@ -958,8 +972,8 @@ namespace bee::lua_filesystem {
958
972
}
959
973
960
974
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);
963
977
if (!fd) {
964
978
return lua::push_error (L, error::sys_errmsg (" filelock" ));
965
979
}
@@ -974,8 +988,8 @@ namespace bee::lua_filesystem {
974
988
}
975
989
976
990
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);
979
993
if (!fd) {
980
994
return lua::push_error (L, error::sys_errmsg (" fullpath" ));
981
995
}
0 commit comments