std::string encode_field(str_ref v, bool add_br) { std::string r; r.reserve(v.size() << 1); while (v) { if (boost::istarts_with(v, "ftp://") || boost::istarts_with(v, "http://") || boost::istarts_with(v, "https://") || boost::istarts_with(v, "mailto:")) { size_t p = 0; while (p < v.size() && !isspace(v[p] & 0xff) && v[p] != '\"' && v[p] != '<' && v[p] != '>' && v[p] != '[' && v[p] != ']') p++; if (v[p - 1] == '!' || v[p - 1] == ',' || v[p - 1] == '.' || v[p - 1] == '?') p--; if (v[p - 1] == ')') p--; str_ref url = v.substr(0, p); if (boost::istarts_with(v, "ftp.")) r += web_link(url, "ftp://" + url.s()); else if (boost::istarts_with(v, "www.")) r += web_link(url, "http://" + url.s()); else r += web_link(boost::istarts_with(v, "mailto:") ? url.substr(7) : url, url); while (p--) v.pop_front(); } else { switch (v.front()) { case '\n': r += add_br ? "<br>" : " "; break; case '\r': break; case '&': r += "&"; break; case '<': r += "<"; break; default: r += v.front(); } v.pop_front(); } } return r; }
static std::string web_encode(str_ref v) { std::string d; d.reserve(v.size() << 1); while (v) { switch (v.front()) { case '"': d += """; break; case '&': d += "&"; break; case '<': d += "<"; break; default: d += v.front(); } v.pop_front(); } return d; }