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

convert UTIL_TrimRight/TrimLeft to use std string internally #121

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions core/metamod_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,28 @@ const char *UTIL_GetExtension(const char *file)
return NULL;
}

// https://stackoverflow.com/a/217605
// trim from start (in place)
static inline void ltrim(std::string& s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
}

// trim from end (in place)
static inline void rtrim(std::string& s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), s.end());
}

void UTIL_TrimLeft(char *buffer)
{
std::string s(buffer);
ltrim(s);
strcpy(buffer, s.c_str());

#if 0
sapphonie marked this conversation as resolved.
Show resolved Hide resolved
/* Let's think of this as our iterator */
char *i = buffer;

Expand All @@ -88,10 +108,16 @@ void UTIL_TrimLeft(char *buffer)
memmove(buffer, i, (strlen(i) + 1) * sizeof(char));
}
}
#endif
}

void UTIL_TrimRight(char *buffer)
{
std::string s(buffer);
rtrim(s);
strcpy(buffer, s.c_str());

#if 0
/* Make sure buffer isn't null */
if (buffer)
{
Expand All @@ -108,6 +134,7 @@ void UTIL_TrimRight(char *buffer)
}
}
}
#endif
}

bool UTIL_PathCmp(const char *path1, const char *path2)
Expand Down