Skip to content

Commit

Permalink
Add String::urlDecode() method
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <[email protected]>
  • Loading branch information
falkTX committed Oct 5, 2024
1 parent c7fb0ca commit dcf45bb
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions distrho/extra/String.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ class String
*/
String& toLower() noexcept
{
static const char kCharDiff('a' - 'A');
static constexpr const char kCharDiff = 'a' - 'A';

for (std::size_t i=0; i < fBufferLen; ++i)
{
Expand All @@ -607,7 +607,7 @@ class String
*/
String& toUpper() noexcept
{
static const char kCharDiff('a' - 'A');
static constexpr const char kCharDiff = 'a' - 'A';

for (std::size_t i=0; i < fBufferLen; ++i)
{
Expand Down Expand Up @@ -874,6 +874,63 @@ class String
return *this;
}

/*
* Convert to a URL decoded string.
*/
String& urlDecode() noexcept
{
if (fBufferLen == 0)
return *this;

char* const newbuf = static_cast<char*>(std::malloc(fBufferLen + 1));
DISTRHO_SAFE_ASSERT_RETURN(newbuf != nullptr, *this);

char* newbufptr = newbuf;

for (std::size_t i=0; i < fBufferLen; ++i)
{
const char c = fBuffer[i];

if (c == '%')
{
DISTRHO_SAFE_ASSERT_CONTINUE(fBufferLen > i + 2);

char c1 = fBuffer[i + 1];
char c2 = fBuffer[i + 2];
i += 2;

/**/ if (c1 >= '0' && c1 <= '9')
c1 -= '0';
else if (c1 >= 'A' && c1 <= 'Z')
c1 -= 'A' - 10;
else if (c1 >= 'a' && c1 <= 'z')
c1 -= 'a' - 10;

/**/ if (c2 >= '0' && c2 <= '9')
c2 -= '0';
else if (c2 >= 'A' && c2 <= 'Z')
c2 -= 'A' - 10;
else if (c2 >= 'a' && c2 <= 'z')
c2 -= 'a' - 10;

*newbufptr++ = c1 << 4 | c2;
}
else
{
*newbufptr++ = c;
}
}

*newbufptr = '\0';

std::free(fBuffer);
fBuffer = newbuf;
fBufferLen = std::strlen(newbuf);
fBufferAlloc = true;

return *this;
}

// -------------------------------------------------------------------
// public operators

Expand Down

0 comments on commit dcf45bb

Please sign in to comment.