dart-lang / core

This repository is home to core Dart packages.
https://pub.dev/publishers/dart.dev
BSD 3-Clause "New" or "Revised" License
19 stars 7 forks source link

basename function can't recognize Windows '\\' path on Android devices. #547

Closed FengChendian closed 2 weeks ago

FengChendian commented 3 months ago

I tested it on Android14.

print('**********************');
print(p.basename('E:\\agent_reply.wav'));

image

lrhn commented 2 weeks ago

This does look like it's working as intended.

The path package uses the local system's behavior unless you ask for a different behavior by creating a Context. It's states in the first paragraph of https://pub.dev/documentation/path/latest/

FengChendian commented 2 weeks ago

This does look like it's working as intended.

The path package uses the local system's behavior unless you ask for a different behavior by creating a Context. It's states in the first paragraph of https://pub.dev/documentation/path/latest/

But the doc also says that:

How cross-platform is this? We believe this library handles most of the corner cases of Windows paths

Does it just work on windows platform or windows context for processing Windows paths? So, if I don't know the system of target path, how do I get a correct base name?

For example, I want to process paths which may come from various system. Users may give a Windows' path when they use macOS

var name = getUserInput();
print(p.basename(name));

Should I use some code like if path.contains('\\')?

lrhn commented 2 weeks ago

It only supports Windows paths as default on Windows platforms. The default is the behavior of native paths on the current platform. On other platforms, you need to ask for Windows behavior by creating a var paths = Context(style: Style.windows) and using that for doing path operations. That also means that on other platforms, a //UNC/foo/bar path is treated correctly according to the local platform behavior, without (possibly surprisingly) using Window behavior.

What you should do depends on which behavior you want. If you don't know which system a user provided path comes from, you probably need to do some kind of detection, otherwise you cannot treat it correctly. Whether that is .contains(r'\') or something more intricate (since Windows paths can use / too, but c:/foo/ is an absolute path on Windows, and /foo/ is not, the opposite of what other systems would say.

FengChendian commented 2 weeks ago

It only supports Windows paths as default on Windows platforms. The default is the behavior of native paths on the current platform. On other platforms, you need to ask for Windows behavior by creating a var paths = Context(style: Style.windows) and using that for doing path operations. That also means that on other platforms, a //UNC/foo/bar path is treated correctly according to the local platform behavior, without (possibly surprisingly) using Window behavior.

What you should do depends on which behavior you want. If you don't know which system a user provided path comes from, you probably need to do some kind of detection, otherwise you cannot treat it correctly. Whether that is .contains(r'\') or something more intricate (since Windows paths can use / too, but c:/foo/ is an absolute path on Windows, and /foo/ is not, the opposite of what other systems would say.

Thanks for your reply! I think I should add a regex check to analyze paths which are provided by users.