From 5d4c1b7e9d1fff2c0578953680119066045dd338 Mon Sep 17 00:00:00 2001 From: Misa Date: Fri, 26 Feb 2021 15:29:37 -0800 Subject: [PATCH] Refactor endsWith() to not use the STL There's not really any reason for this function to use heap-allocated strings. So I've refactored it to not do that. I would've used SDL_strrstr(), if it existed. It does not appear to exist. But that's okay. --- desktop_version/src/FileSystemUtils.cpp | 2 +- desktop_version/src/UtilityClass.cpp | 14 +++++++------- desktop_version/src/UtilityClass.h | 2 +- desktop_version/src/editor.cpp | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index 1991f1f4..2a2668d5 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -204,7 +204,7 @@ void FILESYSTEM_mountassets(const char* path) FILESYSTEM_mount(zippath.c_str()); graphics.reloadresources(); FILESYSTEM_assetsmounted = true; - } else if (zip_path != "data.zip" && !endsWith(zip_path, "/data.zip") && endsWith(zip_path, ".zip")) { + } else if (zip_path != "data.zip" && !endsWith(zip_path.c_str(), "/data.zip") && endsWith(zip_path.c_str(), ".zip")) { printf("Custom asset directory is .zip at %s\n", zip_path.c_str()); PHYSFS_File* zip = PHYSFS_openRead(zip_path.c_str()); zip_path += ".data.zip"; diff --git a/desktop_version/src/UtilityClass.cpp b/desktop_version/src/UtilityClass.cpp index 52bcb8e0..ab241691 100644 --- a/desktop_version/src/UtilityClass.cpp +++ b/desktop_version/src/UtilityClass.cpp @@ -323,15 +323,15 @@ bool is_positive_num(const char* str, const bool hex) return true; } -bool endsWith(const std::string& str, const std::string& suffix) +bool endsWith(const char* str, const char* suffix) { - if (str.size() < suffix.size()) + const size_t str_size = SDL_strlen(str); + const size_t suffix_size = SDL_strlen(suffix); + + if (str_size < suffix_size) { return false; } - return str.compare( - str.size() - suffix.size(), - suffix.size(), - suffix - ) == 0; + + return SDL_strcmp(&str[str_size - suffix_size], suffix) == 0; } diff --git a/desktop_version/src/UtilityClass.h b/desktop_version/src/UtilityClass.h index ebd1411b..fde9b1f2 100644 --- a/desktop_version/src/UtilityClass.h +++ b/desktop_version/src/UtilityClass.h @@ -26,7 +26,7 @@ bool is_number(const char* str); bool is_positive_num(const char* str, const bool hex); -bool endsWith(const std::string& str, const std::string& suffix); +bool endsWith(const char* str, const char* suffix); #define INBOUNDS_VEC(index, vector) ((int) index >= 0 && (int) index < (int) vector.size()) #define INBOUNDS_ARR(index, array) ((int) index >= 0 && (int) index < (int) SDL_arraysize(array)) diff --git a/desktop_version/src/editor.cpp b/desktop_version/src/editor.cpp index 29718f5a..1d9d25ba 100644 --- a/desktop_version/src/editor.cpp +++ b/desktop_version/src/editor.cpp @@ -82,7 +82,7 @@ static void levelZipCallback(const char* filename) { std::string filename_ = filename; - if (endsWith(filename_, ".zip")) + if (endsWith(filename_.c_str(), ".zip")) { PHYSFS_File* zip = PHYSFS_openRead(filename_.c_str()); @@ -1822,7 +1822,7 @@ bool editorclass::load(std::string& _path) // the linefeed + the extremely specific amount of // whitespace at the end of the contents. - if (endsWith(text, "\n ")) // linefeed + exactly 12 spaces + if (endsWith(text.c_str(), "\n ")) // linefeed + exactly 12 spaces { // 12 spaces + 1 linefeed = 13 chars text = text.substr(0, text.length()-13);