aboutsummaryrefslogtreecommitdiff
path: root/src/str.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2020-03-17 01:19:40 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2020-03-17 01:19:40 -0400
commit54d2475f74f30be5e4ae476d9ac0df92f3e6ca12 (patch)
tree43e499373686bb7c19703a2ef14f97be4134a169 /src/str.c
parent26c1d9f9a250c9e75fe7e71a638ba0a4c5bc5b97 (diff)
downloadspmc-54d2475f74f30be5e4ae476d9ac0df92f3e6ca12.tar.gz
Fix endswith() not working correctly
Diffstat (limited to 'src/str.c')
-rw-r--r--src/str.c19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/str.c b/src/str.c
index fb7b4be..5db3adc 100644
--- a/src/str.c
+++ b/src/str.c
@@ -49,9 +49,22 @@ int endswith(const char *sptr, const char *pattern) {
if (!sptr) {
return -1;
}
- size_t sptr_size = strlen(sptr);
- size_t pattern_size = strlen(pattern);
- for (size_t s = sptr_size - pattern_size, p = 0 ; s < sptr_size; s++, p++) {
+ ssize_t sptr_size = strlen(sptr);
+ ssize_t pattern_size = strlen(pattern);
+
+ if (sptr_size == pattern_size) {
+ if (strcmp(sptr, pattern) == 0) {
+ return 1; // yes
+ }
+ return 0; // no
+ }
+
+ ssize_t s = sptr_size - pattern_size;
+ if (s < 0) {
+ return 0;
+ }
+
+ for (size_t p = 0 ; s < sptr_size; s++, p++) {
if (sptr[s] != pattern[p]) {
// sptr does not end with pattern
return 0;