From 2af3233101b109a6ebcb1087d2c47ebce3c4c978 Mon Sep 17 00:00:00 2001 From: npwoods Date: Fri, 23 Jun 2017 19:46:58 -0400 Subject: Changed a few 'const char *' ==> 'const std::string &' in the MAME debugger (#2170) --- src/lib/util/corestr.cpp | 44 +++++++++++++++++++++++++++++--------------- src/lib/util/corestr.h | 1 + 2 files changed, 30 insertions(+), 15 deletions(-) (limited to 'src/lib') diff --git a/src/lib/util/corestr.cpp b/src/lib/util/corestr.cpp index 66b12f6ee04..c8775f1cec0 100644 --- a/src/lib/util/corestr.cpp +++ b/src/lib/util/corestr.cpp @@ -171,27 +171,41 @@ void strreplacechr(std::string& str, char ch, char newch) } } -std::string strtrimspace(std::string& str) +static std::string internal_strtrimspace(std::string& str, bool right_only) { - int start = 0; - for (auto & elem : str) - { - if (!isspace(uint8_t(elem))) break; - start++; - } - int end = str.length(); - if (end > 0) + // identify the start + std::string::iterator start = str.begin(); + if (!right_only) { - for (size_t i = str.length() - 1; i > 0; i--) - { - if (!isspace(uint8_t(str[i]))) break; - end--; - } + start = std::find_if( + str.begin(), + str.end(), + [](char c) { return !isspace(uint8_t(c)); }); } - str = str.substr(start, end-start); + + // identify the end + std::string::iterator end = std::find_if( + str.rbegin(), + std::string::reverse_iterator(start), + [](char c) { return !isspace(uint8_t(c)); }).base(); + + // extract the string + str = end > start + ? str.substr(start - str.begin(), end - start) + : ""; return str; } +std::string strtrimspace(std::string& str) +{ + return internal_strtrimspace(str, false); +} + +std::string strtrimrightspace(std::string& str) +{ + return internal_strtrimspace(str, true); +} + std::string strmakeupper(std::string& str) { std::transform(str.begin(), str.end(), str.begin(), ::toupper); diff --git a/src/lib/util/corestr.h b/src/lib/util/corestr.h index 9a9bce23e99..d31c1e5f8df 100644 --- a/src/lib/util/corestr.h +++ b/src/lib/util/corestr.h @@ -67,6 +67,7 @@ int strcatvprintf(std::string &str, const char *format, va_list args); void strdelchr(std::string& str, char chr); void strreplacechr(std::string& str, char ch, char newch); std::string strtrimspace(std::string& str); +std::string strtrimrightspace(std::string& str); std::string strmakeupper(std::string& str); std::string strmakelower(std::string& str); int strreplace(std::string &str, const std::string& search, const std::string& replace); -- cgit v1.2.3