diff options
author | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2014-06-20 21:31:41 -0700 |
---|---|---|
committer | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2014-06-20 21:31:41 -0700 |
commit | df9b8f8ae7a540988c20fdffaaff2442c349873a (patch) | |
tree | ff9be69934b6896c8d7e91412b63dd9cd9b37341 /src | |
parent | fcc66ce9f5d6db995536beab5539fd47e490bf14 (diff) |
Moving tokenize, percentEncode and percentDecode around. Fixing a few warnings. Workarounding c-ares and pthreads compilation issue with unicode.
Diffstat (limited to 'src')
-rw-r--r-- | src/BString.cc | 22 | ||||
-rw-r--r-- | src/CurlTask.cc | 81 | ||||
-rw-r--r-- | src/Http.cc | 68 | ||||
-rw-r--r-- | src/SimpleMustache.cc | 6 |
4 files changed, 93 insertions, 84 deletions
diff --git a/src/BString.cc b/src/BString.cc index 4ba3601..5f4b868 100644 --- a/src/BString.cc +++ b/src/BString.cc @@ -154,3 +154,25 @@ Balau::String::List Balau::String::split(char c) { free(d); return r; } + +std::vector<Balau::String> Balau::String::tokenize(const String & delimiters, bool trimEmpty) { + std::vector<String> tokens; + size_t pos, lastPos = 0; + for (;;) { + pos = find_first_of(delimiters, lastPos); + if (pos == String::npos) { + pos = strlen(); + + if ((pos != lastPos) || !trimEmpty) + tokens.push_back(extract(lastPos, pos)); + + return tokens; + } + else { + if ((pos != lastPos) || !trimEmpty) + tokens.push_back(extract(lastPos, pos)); + } + + lastPos = pos + 1; + } +} diff --git a/src/CurlTask.cc b/src/CurlTask.cc index 2a792f9..2c37514 100644 --- a/src/CurlTask.cc +++ b/src/CurlTask.cc @@ -38,87 +38,6 @@ int Balau::CurlTask::debugFunctionStatic(CURL * easy, curl_infotype info, char * return curlTask->debugFunction(info, str, str_len); } -Balau::String Balau::CurlTask::percentEncode(const String & src) { - const size_t size = src.strlen(); - String ret; - ret.reserve(size); - - static char toHex[] = "0123456789ABCDEF"; - - for (size_t i = 0; i < size; i++) { - char c = src[i]; - if (((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || (c == '-') || (c == '.') || (c == '_') || (c == '~')) { - ret += c; - } else { - ret += '%'; - ret += toHex[c >> 4]; - ret += toHex[c & 15]; - } - } - - return ret; -} - -Balau::String Balau::CurlTask::percentDecode(const String & src) { - const size_t size = src.strlen(); - String ret; - ret.reserve(size); - - for (size_t i = 0; i < size; i++) { - char c = src[i]; - if (((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || (c == '-') || (c == '.') || (c == '_') || (c == '~')) { - ret += c; - } else if ((c == '%') && ((i + 2) < size)) { - char h1 = src[i + 1]; - char h2 = src[i + 2]; - if ((h1 >= '0') && (h1 <= '9')) { - c = h1 - '0'; - } else if ((h1 >= 'A') && (h1 <= 'F')) { - c = h1 - 'A' + 10; - } else { - // invalid - return ret; - } - c <<= 4; - if ((h2 >= '0') && (h2 <= '9')) { - c |= h2 - '0'; - } else if ((h2 >= 'A') && (h2 <= 'F')) { - c |= h2 - 'A' + 10; - } else { - // invalid - return ret; - } - i += 2; - } else { - // invalid - return ret; - } - } - - return ret; -} - -std::vector<Balau::String> Balau::CurlTask::tokenize(const String & str, const String & delimiters, bool trimEmpty) { - std::vector<String> tokens; - size_t pos, lastPos = 0; - for (;;) { - pos = str.find_first_of(delimiters, lastPos); - if (pos == String::npos) { - pos = str.strlen(); - - if ((pos != lastPos) || !trimEmpty) - tokens.push_back(str.extract(lastPos, pos)); - - return tokens; - } else { - if ((pos != lastPos) || !trimEmpty) - tokens.push_back(str.extract(lastPos, pos)); - } - - lastPos = pos + 1; - } -} - Balau::DownloadTask::DownloadTask(const Balau::String & url) { curl_easy_setopt(m_curlHandle, CURLOPT_URL, url.to_charp()); m_name.set("DownloadTask(%s)", url.to_charp()); diff --git a/src/Http.cc b/src/Http.cc index 10c3430..c645836 100644 --- a/src/Http.cc +++ b/src/Http.cc @@ -72,3 +72,71 @@ static const std::map<const Balau::String, const char *> s_mimeMap { const char * Balau::Http::getContentType(const String & extension) { return getMap<const Balau::String>(s_mimeMap, extension, "application/octet-stream"); } + + +Balau::String Balau::Http::percentEncode(const String & src) { + const size_t size = src.strlen(); + String ret; + ret.reserve(size); + + static char toHex[] = "0123456789ABCDEF"; + + for (size_t i = 0; i < size; i++) { + char c = src[i]; + if (((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || (c == '-') || (c == '.') || (c == '_') || (c == '~')) { + ret += c; + } + else { + ret += '%'; + ret += toHex[c >> 4]; + ret += toHex[c & 15]; + } + } + + return ret; +} + +Balau::String Balau::Http::percentDecode(const String & src) { + const size_t size = src.strlen(); + String ret; + ret.reserve(size); + + for (size_t i = 0; i < size; i++) { + char c = src[i]; + if (((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || (c == '-') || (c == '.') || (c == '_') || (c == '~')) { + ret += c; + } + else if ((c == '%') && ((i + 2) < size)) { + char h1 = src[i + 1]; + char h2 = src[i + 2]; + if ((h1 >= '0') && (h1 <= '9')) { + c = h1 - '0'; + } + else if ((h1 >= 'A') && (h1 <= 'F')) { + c = h1 - 'A' + 10; + } + else { + // invalid + return ret; + } + c <<= 4; + if ((h2 >= '0') && (h2 <= '9')) { + c |= h2 - '0'; + } + else if ((h2 >= 'A') && (h2 <= 'F')) { + c |= h2 - 'A' + 10; + } + else { + // invalid + return ret; + } + i += 2; + } + else { + // invalid + return ret; + } + } + + return ret; +} diff --git a/src/SimpleMustache.cc b/src/SimpleMustache.cc index 557a183..bad2ab0 100644 --- a/src/SimpleMustache.cc +++ b/src/SimpleMustache.cc @@ -65,7 +65,7 @@ Balau::SimpleMustache::Context & Balau::SimpleMustache::Context::Proxy::operator m_idx = ctxLst.size() + m_idx + 1; if (m_idx <= 0) m_idx = 1; - if (ctxLst.size() < m_idx) + if (ctxLst.size() < static_cast<size_t>(m_idx)) ctxLst.resize(m_idx); SubContext & subCtx = ctxLst[m_idx - 1]; SubContext::iterator s = subCtx.find(key); @@ -447,7 +447,7 @@ Balau::SimpleMustache::Fragments::const_iterator Balau::SimpleMustache::render_r Balau::String Balau::SimpleMustache::escape(const String & s) { int size = 0; - for (int i = 0; i < s.strlen(); i++) { + for (unsigned i = 0; i < s.strlen(); i++) { switch (s[i]) { case '&': size += 5; @@ -476,7 +476,7 @@ Balau::String Balau::SimpleMustache::escape(const String & s) { char * t = (char *) malloc(size + 1); char * p = t; - for (int i = 0; i < s.strlen(); i++) { + for (unsigned i = 0; i < s.strlen(); i++) { switch (s[i]) { case '&': *p++ = '&'; |