#include <iostream>
#include <cstring>
using namespace std;
struct string_view
{
string_view() = default;
string_view(const string_view& other) = default;
string_view(const string& str)
: ptr(str.c_str()), size(str.length())
{}
string_view(const char* str)
: ptr(str), size(::strlen(str))
{}
string_view(const char* str, size_t len)
: ptr(str), size(len)
{}
// Protection for temporary objects
string_view(string&& str) = delete;
string_view& operator=(const string_view&) = default;
const char *ptr = nullptr;
size_t size = 0;
};
string get_host()
{
return "http://h...content-available-to-author-only...d.su";
}
string_view get_schema(string_view uri)
{
return uri;
}
string_view get_schema2(string url)
{
string_view v(url);
return v;
}
const char* get_schema3(string url)
{
return url.c_str();
}
int main()
{
{
// Compilation error:
//auto v1 = get_schema(get_host());
//string_view v2;
//v2 = get_host();
}
{
// WA for protection
auto v1 = get_schema2(get_host());
auto v2 = get_schema(get_host().c_str());
}
{
// Valid usage
string str = get_host();
auto v1 = get_schema(str);
string_view v2 = "hello";
auto v3 = get_schema("http://h...content-available-to-author-only...d.su");
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0cmluZz4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBzdHJpbmdfdmlldwp7CglzdHJpbmdfdmlldygpID0gZGVmYXVsdDsKCXN0cmluZ192aWV3KGNvbnN0IHN0cmluZ192aWV3JiBvdGhlcikgPSBkZWZhdWx0OwoJCglzdHJpbmdfdmlldyhjb25zdCBzdHJpbmcmIHN0cikKCQk6IHB0cihzdHIuY19zdHIoKSksIHNpemUoc3RyLmxlbmd0aCgpKQoJe30KCQoJc3RyaW5nX3ZpZXcoY29uc3QgY2hhciogc3RyKQoJCTogcHRyKHN0ciksIHNpemUoOjpzdHJsZW4oc3RyKSkKCXt9CgkKCXN0cmluZ192aWV3KGNvbnN0IGNoYXIqIHN0ciwgc2l6ZV90IGxlbikKCQk6IHB0cihzdHIpLCBzaXplKGxlbikKCXt9CgkKCS8vIFByb3RlY3Rpb24gZm9yIHRlbXBvcmFyeSBvYm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