Aws::String PercentDecode()

in src/aws-cpp-sdk-core/source/endpoint/DefaultEndpointProvider.cpp [35:104]


Aws::String PercentDecode(Aws::String inputString)
{
    if (inputString.find_first_of("%") == Aws::String::npos)
    {
        return inputString;
    }
    Aws::String result;
    result.reserve(inputString.size());

    bool percentFound = false;
    char firstOctet = 0;
    char secondOctet = 0;
    for(size_t i = 0; i < inputString.size(); ++i)
    {
        const char currentChar = inputString[i];
        if ('%' == currentChar)
        {
            if (percentFound)
            {
                // not percent-encoded string
                result += currentChar;
            }
            percentFound = true;
            continue;
        }

        if (percentFound)
        {
            if ((currentChar >= '0' && currentChar <= '9') ||
                (currentChar >= 'A' && currentChar <= 'F') ||
                (currentChar >= 'a' && currentChar <= 'f'))
            {
                if(!firstOctet)
                {
                    firstOctet = currentChar;
                    continue;
                }
                if(!secondOctet)
                {
                    secondOctet = currentChar;
                    char encodedChar = CharToDec(firstOctet) * 16 + CharToDec(secondOctet);
                    result += encodedChar;

                    percentFound = false;
                    firstOctet = 0;
                    secondOctet = 0;
                    continue;
                }
            } else {
                // Non-percent encoded sequence
                result += '%';
                if(!firstOctet)
                    result += firstOctet;
                result += currentChar;
                percentFound = false;
                firstOctet = 0;
                secondOctet = 0;
                continue;
            }
        }

        if ('+' == currentChar)
        {
            result += ' ';
            continue;
        }
        result += currentChar;
    }
    return result;
}