अगर मैं सही ढंग से खोज पढ़ सकते हैं और आप कर्ल मुझे लगता है कि मैं एक समाधान है का उपयोग किए बिना अपने आप को ऐसा करने के लिए, चाहते हैं (UTF-8 sssuming) और मुझे लगता है कि इस URL एन्कोडिंग क्वेरी स्ट्रिंग के अनुरूप और पोर्टेबल तरीका है :
#include <boost/function_output_iterator.hpp>
#include <boost/bind.hpp>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iterator>
#include <iomanip>
namespace {
std::string encimpl(std::string::value_type v) {
if (isalnum(v))
return std::string()+v;
std::ostringstream enc;
enc << '%' << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << int(static_cast<unsigned char>(v));
return enc.str();
}
}
std::string urlencode(const std::string& url) {
// Find the start of the query string
const std::string::const_iterator start = std::find(url.begin(), url.end(), '?');
// If there isn't one there's nothing to do!
if (start == url.end())
return url;
// store the modified query string
std::string qstr;
std::transform(start+1, url.end(),
// Append the transform result to qstr
boost::make_function_output_iterator(boost::bind(static_cast<std::string& (std::string::*)(const std::string&)>(&std::string::append),&qstr,_1)),
encimpl);
return std::string(url.begin(), start+1) + qstr;
}
यह कोई अमानक बढ़ावा अलावा अन्य निर्भरता है और अगर आप को बढ़ावा देने पसंद नहीं है यह है कि दूर करने के लिए मुश्किल नहीं है निर्भरता।
int main() {
const char *testurls[] = {"http://foo.com/bar?abc<>de??90 210fg!\"$%",
"http://google.com",
"http://www.unicode.com/example?großpösna"};
std::copy(testurls, &testurls[sizeof(testurls)/sizeof(*testurls)],
std::ostream_iterator<std::string>(std::cout,"\n"));
std::cout << "encode as: " << std::endl;
std::transform(testurls, &testurls[sizeof(testurls)/sizeof(*testurls)],
std::ostream_iterator<std::string>(std::cout,"\n"),
std::ptr_fun(urlencode));
}
सब कौन सा लग रहा था काम करने के लिए:
http://foo.com/bar?abc<>de??90 210fg!"$%
http://google.com
http://www.unicode.com/example?großpösna
बन जाता है:
http://foo.com/bar?abc%3C%3Ede%3F%3F90%20%20%20210fg%21%22%24%25
http://google.com
http://www.unicode.com/example?gro%C3%9Fp%C3%B6sna
इन examples
साथ कौन सा वर्ग UTF8 नहीं है
मैं का उपयोग कर इसे परीक्षण किया यूनिकोड? – maxschlepzig
यूटीएफ -8 यूनिकोड डेटा स्थानांतरित करने के लिए तार प्रोटोकॉल में से एक है। एएससीआईआई एन्कोडिंग के साथ पिछड़े संगत होने का इसका एक अतिरिक्त लाभ है। जीजे के सुझाव के लिए +1। – ivymike
@maxschlepzig: मैंने भी ऐसा सोचा। लेकिन आज कुछ नया सीखा ... –