aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/text/utf8/make_url.cpp
blob: 631738fb8a1176c136d04f49bfe79956f09a1513 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/text/utf8.h>

void printCodepoint(unsigned long codepoint)
{
    vespalib::string data;
    vespalib::Utf8Writer w(data);
    w.putChar(codepoint);
    printf("URL encoding of codepoint U+%04lX entity &#%lu; string value '%s' is:\n",
           codepoint, codepoint, data.c_str());

    for (size_t i = 0; i < data.size(); ++i) {
        unsigned char byte = data[i];
        printf("%%%02X", byte);
    }
    printf("\n");
}

int main(int argc, char **argv)
{
    if (argc == 2) {
        unsigned long codepoint = 0;
        if (sscanf(argv[1], "U+%lx", &codepoint) == 1) {
            printCodepoint(codepoint);
            return 0;
        } else if (sscanf(argv[1], "\\u%lx", &codepoint) == 1) {
            printCodepoint(codepoint);
            return 0;
        }
    }
    fprintf(stderr, "Usage: %s U+XXXX\n", argv[0]);
    return 1;
}