Skip to content

Commit

Permalink
STRING: Add function to check if a string ends with a given string
Browse files Browse the repository at this point in the history
  • Loading branch information
criezy committed Apr 10, 2019
1 parent 7727388 commit 9f2f405
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
8 changes: 8 additions & 0 deletions str.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ bool String::startsWith(const String& s) const {
return strncmp(data_->str_, s.data_->str_, s.data_->size_) == 0;
}

bool String::endsWith(const String& s) const {
if (s.isEmpty() || s.data_ == data_)
return true;
else if (s.length() > length())
return false;
return strncmp(data_->str_ + data_->size_ - s.data_->size_, s.data_->str_, s.data_->size_) == 0;
}

/*! \fn String String::left(int to) const
*
* Return the left part of this string up to and including the
Expand Down
1 change: 1 addition & 0 deletions str.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class String {
bool contains(const String &x) const;
bool contains(const char *x) const;
bool startsWith(const String&) const;
bool endsWith(const String&) const;

const char* const c_str() const;
int length() const;
Expand Down

0 comments on commit 9f2f405

Please sign in to comment.