Skip to content

Commit

Permalink
[api] Added os.path.basename()
Browse files Browse the repository at this point in the history
  • Loading branch information
pajama-coder committed Aug 2, 2024
1 parent 084f958 commit 61e907e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/api/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ OS::RmdirOptions::RmdirOptions(pjs::Object *options) : MkdirOptions(options) {
// OS::Path
//

auto OS::Path::basename(const std::string &path) -> std::string {
return utils::path_basename(path);
}

auto OS::Path::basename(const std::string &path, const std::string &suffix) -> std::string {
auto name = basename(path);
if (utils::ends_with(name, suffix)) name.resize(path.length() - suffix.length());
return name;
}

auto OS::Path::dirname(const std::string &path) -> std::string {
return utils::path_dirname(path);
}
Expand Down Expand Up @@ -481,6 +491,17 @@ template<> void ClassDef<OS::Stats>::init() {
template<> void ClassDef<OS::Path>::init() {
ctor();

// os.path.basename
method("basename", [](Context &ctx, Object *, Value &ret) {
Str *path;
Str *suffix = nullptr;
if (!ctx.arguments(1, &path, &suffix)) return;
ret.set(suffix
? OS::Path::basename(path->str(), suffix->str())
: OS::Path::basename(path->str())
);
});

// os.path.dirname
method("dirname", [](Context &ctx, Object *, Value &ret) {
Str *path;
Expand Down
2 changes: 2 additions & 0 deletions src/api/os.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class OS : public pjs::ObjectTemplate<OS> {

class Path : public pjs::ObjectTemplate<Path> {
public:
static auto basename(const std::string &path) -> std::string;
static auto basename(const std::string &path, const std::string &suffix) -> std::string;
static auto dirname(const std::string &path) -> std::string;
static auto join(int argc, const pjs::Value argv[]) -> std::string;
static auto resolve(int argc, const pjs::Value argv[]) -> std::string;
Expand Down
6 changes: 6 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,12 @@ auto path_dirname(const std::string &path) -> std::string {
return path.substr(0, i);
}

auto path_basename(const std::string &path) -> std::string {
auto i = path.rfind('/');
if (i == std::string::npos) return path;
return path.substr(i + 1);
}

//
// HexEncoder
//
Expand Down
1 change: 1 addition & 0 deletions src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ auto decode_base64url(void *out, const char *inp, int len) -> int;
auto path_join(const std::string &base, const std::string &path) -> std::string;
auto path_normalize(const std::string &path) -> std::string;
auto path_dirname(const std::string &path) -> std::string;
auto path_basename(const std::string &path) -> std::string;

//
// HexEncoder
Expand Down

0 comments on commit 61e907e

Please sign in to comment.