Skip to content

Instantly share code, notes, and snippets.

@juliarose
Created June 20, 2024 05:50
Show Gist options
  • Save juliarose/81fa81a3eefa15386cf948f98a325fe2 to your computer and use it in GitHub Desktop.
Save juliarose/81fa81a3eefa15386cf948f98a325fe2 to your computer and use it in GitHub Desktop.
/**
* @brief Checks if a string ends with a given suffix.
*
* @param fullString The string to check.
* @param ending The suffix to check for.
* @return True if the string ends with the suffix, false otherwise.
*/
bool hasEnding(std::string const &fullString, std::string const &ending) {
// Sourced from https://stackoverflow.com/questions/874134/find-out-if-string-ends-with-another-string-in-c
int fullStringLen = fullString.length();
int endingLen = ending.length();
// If the ending is longer than the full string, it can't be a suffix
if (endingLen > fullStringLen) {
return false;
}
int offset = fullString.compare(fullStringLen - endingLen, endingLen, ending);
return 0 == offset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment