From fe06ccecf5014e9509124d2a4b83cec4239531a8 Mon Sep 17 00:00:00 2001 From: falkTX Date: Sun, 23 May 2021 23:20:40 +0100 Subject: [PATCH] Add String::remove(char) method Signed-off-by: falkTX --- distrho/extra/String.hpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/distrho/extra/String.hpp b/distrho/extra/String.hpp index f74c430e..335cd9b8 100644 --- a/distrho/extra/String.hpp +++ b/distrho/extra/String.hpp @@ -527,6 +527,29 @@ public: return *this; } + /* + * Remove all occurrences of character 'c', shifting and truncating the string as necessary. + */ + String& remove(const char c) noexcept + { + DISTRHO_SAFE_ASSERT_RETURN(c != '\0', *this); + + if (fBufferLen == 0) + return *this; + + for (std::size_t i=0; i < fBufferLen; ++i) + { + if (fBuffer[i] == c) + { + --fBufferLen; + std::memmove(fBuffer+i, fBuffer+i+1, fBufferLen-i); + } + } + + fBuffer[fBufferLen] = '\0'; + return *this; + } + /* * Truncate the string to size 'n'. */