diff options
author | 2021-01-20 17:46:03 -0500 | |
---|---|---|
committer | 2021-01-20 18:06:15 -0500 | |
commit | 91921618c2e06bd0ed073b8efccd31a127c9012d (patch) | |
tree | d3958b32d8db82540cd31083766b22991eb8e8ae /src/lib/util/corestr.cpp | |
parent | c691b79cce315acdfabe1901cb2b5a1e39957bc1 (diff) |
Much more core std::string_view modernization
- Remove corestr.h from emu.h; update a few source files to not use it at all
- Change strtrimspace, strtrimrightspace and core_filename_extract_* to be pure functions taking a std::string_view by value and returning the same type
- Change strmakeupper and strmakelower to be pure functions taking a std::string_view and constructing a std::string
- Remove the string-modifying version of zippath_parent
- Change tag-based lookup functions in device_t to take std::string_view instead of const std::string & or const char *
- Remove the subdevice tag cache from device_t (since device finders are now recommended) and replace it with a map covering directly owned subdevices only
- Move the working directory setup method out of device_image_interface (only the UI seems to actually use the full version of this)
- Change output_manager to use std::string_view for output name arguments
- Change core_options to accept std::string_view for most name and value arguments (return values are still C strings for now)
- Change miscellaneous other functions to accept std::string_view arguments
- Remove a few string accessor macros from romload.h
- Remove many unnecessary c_str() calls from logging/error messages
Diffstat (limited to 'src/lib/util/corestr.cpp')
-rw-r--r-- | src/lib/util/corestr.cpp | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/src/lib/util/corestr.cpp b/src/lib/util/corestr.cpp index 1e89015d456..aff55470b13 100644 --- a/src/lib/util/corestr.cpp +++ b/src/lib/util/corestr.cpp @@ -11,6 +11,7 @@ #include "corestr.h" #include <algorithm> +#include <iterator> #include <memory> #include <cctype> @@ -125,8 +126,6 @@ bool core_iswildstr(const char *sp) std::string helpers -------------------------------------------------*/ -#include <algorithm> - void strdelchr(std::string& str, char chr) { for (size_t i = 0; i < str.length(); i++) @@ -147,10 +146,10 @@ void strreplacechr(std::string& str, char ch, char newch) } } -static std::string &internal_strtrimspace(std::string& str, bool right_only) +static std::string_view internal_strtrimspace(std::string_view str, bool right_only) { // identify the start - std::string::iterator start = str.begin(); + std::string_view::iterator start = str.begin(); if (!right_only) { start = std::find_if( @@ -160,48 +159,49 @@ static std::string &internal_strtrimspace(std::string& str, bool right_only) } // identify the end - std::string::iterator end = std::find_if( + std::string_view::iterator end = std::find_if( str.rbegin(), - std::string::reverse_iterator(start), + std::string_view::reverse_iterator(start), [](char c) { return !isspace(uint8_t(c)); }).base(); // extract the string - str = end > start + return end > start ? str.substr(start - str.begin(), end - start) - : ""; - return str; + : std::string_view(); } -std::string &strtrimspace(std::string& str) +std::string_view strtrimspace(std::string_view str) { return internal_strtrimspace(str, false); } -std::string &strtrimrightspace(std::string& str) +std::string_view strtrimrightspace(std::string_view str) { return internal_strtrimspace(str, true); } -std::string &strmakeupper(std::string& str) +std::string strmakeupper(std::string_view str) { - std::transform(str.begin(), str.end(), str.begin(), ::toupper); - return str; + std::string result; + std::transform(str.begin(), str.end(), std::back_inserter(result), ::toupper); + return result; } /** - * @fn std::string &strmakelower(std::string& str) + * @fn std::string &strmakelower(std::string_view str) * - * @brief Changes the given string to lower case. + * @brief Returns a lower case version of the given string. * * @param [in,out] str The string to make lower case * - * @return A reference to the original std::string having been changed to lower case + * @return A new std::string having been changed to lower case */ -std::string &strmakelower(std::string& str) +std::string strmakelower(std::string_view str) { - std::transform(str.begin(), str.end(), str.begin(), ::tolower); - return str; + std::string result; + std::transform(str.begin(), str.end(), std::back_inserter(result), ::tolower); + return result; } /** @@ -233,7 +233,7 @@ int strreplace(std::string &str, const std::string& search, const std::string& r namespace util { /** - * @fn double edit_distance(std::u32string const &lhs, std::u32string const &rhs) + * @fn double edit_distance(std::u32string_view lhs, std::u32string_view rhs) * * @brief Compares strings and returns prefix-weighted similarity score (smaller is more similar). * @@ -243,7 +243,7 @@ namespace util { * @return Similarity score ranging from 0.0 (totally dissimilar) to 1.0 (identical). */ -double edit_distance(std::u32string const &lhs, std::u32string const &rhs) +double edit_distance(std::u32string_view lhs, std::u32string_view rhs) { // based on Jaro-Winkler distance // TODO: this breaks if the lengths don't fit in a long int, but that's not a big limitation @@ -251,8 +251,8 @@ double edit_distance(std::u32string const &lhs, std::u32string const &rhs) constexpr double PREFIX_WEIGHT(0.1); constexpr double PREFIX_THRESHOLD(0.7); - std::u32string const &longer((lhs.length() >= rhs.length()) ? lhs : rhs); - std::u32string const &shorter((lhs.length() < rhs.length()) ? lhs : rhs); + std::u32string_view const &longer((lhs.length() >= rhs.length()) ? lhs : rhs); + std::u32string_view const &shorter((lhs.length() < rhs.length()) ? lhs : rhs); // find matches long const range((std::max)(long(longer.length() / 2) - 1, 0L)); |