File: UrlEncode.cpp

package info (click to toggle)
audacity 3.7.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 134,800 kB
  • sloc: cpp: 366,277; ansic: 198,323; lisp: 7,761; sh: 3,414; python: 1,501; xml: 1,385; perl: 854; makefile: 125
file content (45 lines) | stat: -rw-r--r-- 999 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*!********************************************************************

 Audacity: A Digital Audio Editor

 @file UrlEncode.cpp
 @brief Define a function to perform URL encoding of a string.

 Dmitry Vedenko
 **********************************************************************/

#include "UrlEncode.h"
#include <string_view>

namespace audacity
{

std::string UrlEncode (const std::string_view url)
{
    std::string escaped;

    for (char c : url)
    {
        if (('0' <= c && c <= '9') ||
            ('A' <= c && c <= 'Z') ||
            ('a' <= c && c <= 'z') ||
            (c == '~' || c == '-' || c == '_' || c == '.')
            )
        {
            escaped.push_back (c);
        }
        else
        {
            static const char symbolLookup[] = "0123456789ABCDEF";

            escaped.push_back ('%');

            escaped.push_back (symbolLookup[(c & 0xF0) >> 4]);
            escaped.push_back (symbolLookup[(c & 0x0F) >> 0]);
        }
    }

    return escaped;
}

}