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

Homework1 #71

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Homework1 #71

wants to merge 4 commits into from

Conversation

Thirdegree
Copy link

Might add more if I think of anything interesting to add.

Copy link
Owner

@david-grs david-grs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice implementation, although you should definitely add a cc fie, and it's a bit too C-ish overall (C arrays, C string functions)

#pragma once

#include <cstring>
#include <iostream>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is iostream needed here? You should exclusively include what is needed , nothing else

#include <cstring>
#include <iostream>
#include <memory>
#include <ostream>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general IOs related header files are heavy and you should include declarations (iosfwd) in header files, definitions (like ostream) in cc. This will also force you to move your class method efinition to the cc, which is better.


private:
const std::size_t mSize;
const std::unique_ptr<char[]> mContents;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was supposed to be on the stack

public:
String(const char s[]) : mSize(std::strlen(s)), mContents(std::make_unique<char[]>(mSize))
{
std::strcpy(mContents.get(), s);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might overflow, you should check the length and throw before copying. I wouldnt use strcpy, it's a C function exported to the std:: NS - we would always prefer algorithms like std::copy

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general the convention would to pass const char* str, size_t len, not just a pointer or array

{
std::strcpy(mContents.get(), s);
}
String() : String("") {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default ctor should be a no-op - this one allocates, which isnt great

return (std::strncmp(c_str(), substr.c_str(), substr.Size()) == 0);
}

const bool EndsWith(const String substr) const
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const ref

const char *c_str() const { return mContents.get(); };

// mainly added these two to help with testing operator+
const bool StartsWith(const String substr) const
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const ref

const std::unique_ptr<char[]> mContents;
};

std::ostream &operator<<(std::ostream &stream, const String &str)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces: std::ostream& .. not std::ostream &

char tmpstr[str1.Size() + str2.Size()];
std::strcpy(tmpstr, str1.c_str());
std::strcat(tmpstr, str2.c_str());
return String(tmpstr);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in the cc file. Also all these C functions are unsafe (risk of buffer overflow).

// extra letter at the start
assert(!addedstr.EndsWith("TThis is a different string"));

}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too long

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

Successfully merging this pull request may close these issues.

2 participants