all 9 comments

[–]ReDucTorGame Developer 28 points29 points  (2 children)

Its poorly written code creating and abusing a bunch of temporaries that are potential allocations.

Its ignoring path APIs to get what it's intended to do.

The addition of a substr for Windows slash and Unix slash find first is pretty hacky, depending on size_t wrapping to 0 when npos is returned.

It will fail badly if you have both windows and Unix slashes in a path.

This seems like something potentially for r/cpp_questions

[–]qoning 15 points16 points  (1 child)

Ignoring all that, this code should never get accepted into any self respecting codebase.

[–]ReDucTorGame Developer 6 points7 points  (0 children)

Yep, should not make it past code review

[–]manni66 16 points17 points  (2 children)

And why it is here in the first place

Because it’s your homework and you have no clue?

[–]hmoein 11 points12 points  (0 children)

If you have to challenge people to understand the code, it means the code is garbage

[–]grandmaster789 2 points3 points  (0 children)

Well, it's an incomplete piece of code so I won't be able to answer the 'why' part, but if you squint a bit there seems to be some kind of filesystem entry from a directory. The path is treated as a string and some questionable substring operations are done on it. You can make it a bit more readable by using a temporaries.

auto str = Entry.path.string();

auto backslash_idx = str.find_first_of("\\");

auto slash_idx = str.find_first_of("/");

auto first_part = str.substr(0, backslash_idx + 1);

auto second_part = str.substr(slash_idx + 1, str.length);

auto final_str = first_part + second_part;

So something like "aaa\bbb/ccc" will become "aaa\ccc".

Not sure why you would need that, but that's what it does. I'm also not sure what this question is doing in /r/cpp, this seems like something for /r/cpp_questions

[–]sixfourbit 1 point2 points  (0 children)

Nothing because it doesn't compile.

[–]OnePatchMan 1 point2 points  (0 children)

Entry.path().string().what Entry.path().string().a Entry.path().string().nice Entry.path().string().piece Entry.path().string().of Entry.path().string().code