Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basename, extension, filename, parent #456

Merged
merged 4 commits into from Jul 23, 2017
Merged

Add basename, extension, filename, parent #456

merged 4 commits into from Jul 23, 2017

Conversation

pithonsmear
Copy link
Contributor

@pithonsmear pithonsmear commented Jul 23, 2017

Problem: Missing string methods

Solution: Add some of them

Changes introduced by this pull request:

  • $basename: Print NAME without leading directories
  • $extension: Print extension of NAME
  • $filename: Print NAME without extension (if there is one)
  • $parent: Print parent of NAME (if there is one)

They're not short, and they're not pretty, but they seem to work, they avoid mutability, and they should handle invalid paths without panicking. Trim of trailing slashes based on answers in issue #441.

Implemented them like this for uniformity. Hopefully, if you need to change something, you won't have to do more work than if you just wrote these methods from scratch.

Sorry for the lengthiness. I'm not very experienced with Rust, so maybe there are tricks I don't know, that could save a few lines.
@mmstick
Copy link
Contributor

mmstick commented Jul 23, 2017

The Rust standard library ships abstractions for this purpose, to handle paths in a cross-platform manner.

use std::path::Path;

fn main() {
    let path = "/grand_parent/parent/filename.ext";
    let basename = Path::new(path).file_name()
        .and_then(|os_str| os_str.to_str())
        .unwrap_or(path);
    let extension = Path::new(path).extension()
        .and_then(|os_str| os_str.to_str())
        .unwrap_or(path);
    let file_stem = Path::new(path).file_stem()
        .and_then(|os_str| os_str.to_str())
        .unwrap_or(path);
    let parent = Path::new(path).parent()
        .and_then(|os_str| os_str.to_str())
        .unwrap_or(path);
    println!("basename: {}\nextension: {}\nfile_stem: {}\nparent: {}", basename, extension, file_stem, parent);
}

https://play.rust-lang.org/?gist=1e6605eb6806c41cefaf1949aff214aa&version=stable

You might want to use those instead.

@pithonsmear
Copy link
Contributor Author

Absolutely. Lemme do this one over.

@pithonsmear pithonsmear deleted the patch-1 branch July 23, 2017 16:22
@mmstick
Copy link
Contributor

mmstick commented Jul 23, 2017

No need to close the PR. You can perform a git push origin patch-1 to push any fixes to this branch.

@pithonsmear pithonsmear restored the patch-1 branch July 23, 2017 16:55
@pithonsmear pithonsmear reopened this Jul 23, 2017
@pithonsmear
Copy link
Contributor Author

Ok, looks like I managed to update the branch with the new methods, but there are some other changes that I don't know why happened.

@mmstick
Copy link
Contributor

mmstick commented Jul 23, 2017

Thanks for the work!

@mmstick mmstick merged commit 3a2c3ad into redox-os:master Jul 23, 2017
@pithonsmear
Copy link
Contributor Author

No, thank you. In the end I didn't really do anything of value to the project, since you gave me the solution, but I did learn!

@mmstick
Copy link
Contributor

mmstick commented Jul 23, 2017

@pithonsmear I wouldn't say that what you did wasn't valuable to the project. Gaining experience with Rust, and familiarity with the Ion codebase, gives value to the project in itself -- especially if you continue to submit PRs. Plus, you did the work in integrating the logic needed to get this pushed through, and that's what matters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants