upate util

This commit is contained in:
Patrick Brosi 2019-09-06 01:24:24 +02:00
parent feacae92ef
commit 06aefa538a
2 changed files with 52 additions and 17 deletions

View file

@ -7,6 +7,7 @@
#include <algorithm> #include <algorithm>
#include <cstring> #include <cstring>
#include <iomanip>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
@ -36,18 +37,43 @@ inline std::string urlDecode(const std::string& encoded) {
} }
// _____________________________________________________________________________ // _____________________________________________________________________________
inline std::string jsonStringEscape(const std::string& unescaped) { inline std::string jsonStringEscape(const std::string& unesc) {
std::string escaped; // modified code from
for (size_t i = 0; i < unescaped.size(); ++i) { // http://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c
if (unescaped[i] == '"' || unescaped[i] == '\\') { std::ostringstream o;
escaped += "\\"; for (auto c = unesc.cbegin(); c != unesc.cend(); c++) {
switch (*c) {
case '"':
o << "\\\"";
break;
case '\\':
o << "\\\\";
break;
case '\b':
o << "\\b";
break;
case '\f':
o << "\\f";
break;
case '\n':
o << "\\n";
break;
case '\r':
o << "\\r";
break;
case '\t':
o << "\\t";
break;
default:
if ('\x00' <= *c && *c <= '\x1f') {
o << "\\u" << std::hex << std::setw(4) << std::setfill('0')
<< static_cast<int>(*c);
} else {
o << *c;
}
} }
if (iscntrl(unescaped[i])) {
escaped += " ";
}
escaped += unescaped[i];
} }
return escaped; return o.str();
} }
// _____________________________________________________________________________ // _____________________________________________________________________________
@ -152,19 +178,22 @@ inline size_t prefixEditDist(const std::string& prefix, const std::string& s,
d[0] = 0; d[0] = 0;
for (size_t i = 1; i <= len1; ++i) d[i * (len2 + 1)] = i; for (size_t i = 1; i <= len1; ++i) d[i * (len2 + 1)] = i;
for (size_t i = 1; i <= len2; ++i) d[ i] = i; for (size_t i = 1; i <= len2; ++i) d[i] = i;
for (size_t i = 1; i <= len1; i++) { for (size_t i = 1; i <= len1; i++) {
for (size_t j = 1; j <= len2; j++) { for (size_t j = 1; j <= len2; j++) {
d[i * (len2 + 1) + j] = std::min(std::min(d[(i - 1) * (len2 + 1) + j] + 1, d[i * (len2 + 1) + j - 1] + 1), d[i * (len2 + 1) + j] = std::min(std::min(d[(i - 1) * (len2 + 1) + j] + 1,
d[(i - 1) * (len2 + 1) + j - 1] + (prefix[i - 1] == s[j - 1] ? 0 : 1)); d[i * (len2 + 1) + j - 1] + 1),
d[(i - 1) * (len2 + 1) + j - 1] +
(prefix[i - 1] == s[j - 1] ? 0 : 1));
} }
} }
// take min of last row // take min of last row
size_t deltaMin = std::max(std::max(deltaMax + 1, prefix.size()), s.size()); size_t deltaMin = std::max(std::max(deltaMax + 1, prefix.size()), s.size());
for (size_t i = 0; i <= len2; i++) { for (size_t i = 0; i <= len2; i++) {
if (d[len1 * (len2 + 1) + i] < deltaMin) deltaMin = d[len1 * (len2 + 1) + i]; if (d[len1 * (len2 + 1) + i] < deltaMin)
deltaMin = d[len1 * (len2 + 1) + i];
} }
return deltaMin; return deltaMin;
@ -177,13 +206,13 @@ inline size_t prefixEditDist(const std::string& prefix, const std::string& s) {
// _____________________________________________________________________________ // _____________________________________________________________________________
inline std::string toUpper(std::string str) { inline std::string toUpper(std::string str) {
std::transform(str.begin(), str.end(),str.begin(), toupper); std::transform(str.begin(), str.end(), str.begin(), toupper);
return str; return str;
} }
// _____________________________________________________________________________ // _____________________________________________________________________________
inline std::string toLower(std::string str) { inline std::string toLower(std::string str) {
std::transform(str.begin(), str.end(),str.begin(), tolower); std::transform(str.begin(), str.end(), str.begin(), tolower);
return str; return str;
} }

View file

@ -22,6 +22,7 @@
#include <vector> #include <vector>
#include "Server.h" #include "Server.h"
#include "util/String.h" #include "util/String.h"
#include "util/log/Log.h"
using util::http::Socket; using util::http::Socket;
using util::http::Queue; using util::http::Queue;
@ -112,7 +113,12 @@ void HttpServer::handle() {
answ = Answer("500 Internal Server Error", "500 Internal Server Error"); answ = Answer("500 Internal Server Error", "500 Internal Server Error");
} }
send(connection, &answ); try {
send(connection, &answ);
} catch (const std::runtime_error& err) {
LOG(WARN) << err.what();
}
close(connection); close(connection);
} }
} }