Permalink
Browse files

Split StringUtils into separated files.

git-svn-id: https://svn.opensource.yandex.net/xscript/trunk@717 b01ef89b-65f2-463d-9415-e8412542ae63
  • Loading branch information...
1 parent 5a8dfca commit 76a2a23b31c333536ecc78d7632cdfb3e3ae89d9 bacek committed Jan 28, 2009
@@ -20,6 +20,7 @@
#include <libxml/xinclude.h>
#include "file_block.h"
#include "file_extension.h"
+#include <xscript/string_utils.h>
#ifdef HAVE_DMALLOC_H
#include <dmalloc.h>
@@ -25,6 +25,7 @@
#include "xscript/request.h"
#include "xscript/state.h"
#include "xscript/util.h"
+#include "xscript/string_utils.h"
#include "xscript/writer.h"
#include "xscript/xml.h"
#include "xscript/xml_util.h"
@@ -13,6 +13,7 @@
#include "http_helper.h"
#include "xscript/policy.h"
#include "xscript/util.h"
+#include "xscript/string_utils.h"
#include "xscript/range.h"
#include "xscript/logger.h"
#include "xscript/request.h"
@@ -9,6 +9,7 @@
#include "xscript/functors.h"
#include "xscript/util.h"
+#include "xscript/string_utils.h"
#include "xscript/range.h"
namespace xscript {
@@ -11,6 +11,7 @@
#include <boost/checked_delete.hpp>
#include "xscript/util.h"
+#include "xscript/string_utils.h"
#include "xscript/resource_holder.h"
#include "internal/expect.h"
@@ -22,6 +22,7 @@
#include "xscript/range.h"
#include "xscript/cookie.h"
#include "xscript/request.h"
+#include "xscript/string_utils.h"
namespace xscript {
@@ -0,0 +1,46 @@
+#ifndef _XSCRIPT_STRING_UTILS_H_
+#define _XSCRIPT_STRING_UTILS_H_
+
+#include <string>
+#include <ostream>
+
+#include <xscript/range.h>
+
+/**
+ * Various string and url related utilities.
+ */
+
+namespace xscript {
+
+class Encoder;
+
+namespace StringUtils {
+ const std::string EMPTY_STRING;
+ typedef std::pair<std::string, std::string> NamedValue;
+
+ void report(const char *what, int error, std::ostream &stream);
+
+ std::string urlencode(const Range &val);
+ template<typename Cont> static std::string urlencode(const Cont &cont) {
+ return urlencode(createRange(cont));
+ };
+
+ std::string urldecode(const Range &val);
+ template<typename Cont> static std::string urldecode(const Cont &cont) {
+ return urldecode(createRange(cont));
+ }
+
+ void parse(const Range &range, std::vector<NamedValue> &v, Encoder *encoder = NULL);
+ template<typename Cont> static void parse(const Cont &cont, std::vector<NamedValue> &v, Encoder *encoder = NULL) {
+ parse(createRange(cont), v, encoder);
+ }
+
+ std::string tolower(const std::string& str);
+ std::string toupper(const std::string& str);
+ const char* nextUTF8(const char* data);
+};
+
+
+};
+
+#endif // _XSCRIPT_STRING_UTILS_H_
View
@@ -76,32 +76,6 @@ class SkipResultInvokeError : public InvokeError {
InvokeError(error, name, value) {}
};
-class Encoder;
-
-class StringUtils : private boost::noncopyable {
-public:
- static const std::string EMPTY_STRING;
- typedef std::pair<std::string, std::string> NamedValue;
-
- static void report(const char *what, int error, std::ostream &stream);
-
- static std::string urlencode(const Range &val);
- template<typename Cont> static std::string urlencode(const Cont &cont);
-
- static std::string urldecode(const Range &val);
- template<typename Cont> static std::string urldecode(const Cont &cont);
-
- static void parse(const Range &range, std::vector<NamedValue> &v, Encoder *encoder = NULL);
- template<typename Cont> static void parse(const Cont &cont, std::vector<NamedValue> &v, Encoder *encoder = NULL);
-
- static std::string tolower(const std::string& str);
- static std::string toupper(const std::string& str);
- static const char* nextUTF8(const char* data);
-
-private:
- StringUtils();
- virtual ~StringUtils();
-};
class HttpDateUtils : private boost::noncopyable {
public:
@@ -143,22 +117,6 @@ class FileUtils : private boost::noncopyable {
};
-
-template<typename Cont> inline std::string
-StringUtils::urlencode(const Cont &cont) {
- return urlencode(createRange(cont));
-}
-
-template<typename Cont> inline std::string
-StringUtils::urldecode(const Cont &cont) {
- return urldecode(createRange(cont));
-}
-
-template<typename Cont> inline void
-StringUtils::parse(const Cont &cont, std::vector<NamedValue> &v, Encoder *encoder) {
- parse(createRange(cont), v, encoder);
-}
-
class TimeoutCounter {
public:
TimeoutCounter();
View
@@ -34,7 +34,8 @@ libxscript_la_SOURCES = args.cpp authorizer.cpp block.cpp component.cpp \
dummy_cache_counter.cpp \
dummy_cache_usage_counter.cpp \
default_request_response.cpp \
- validator_factory.cpp validator.cpp validator_exception.cpp
+ validator_factory.cpp validator.cpp validator_exception.cpp \
+ string_utils.cpp
libxscript_la_LDFLAGS = @VERSION_INFO@
View
@@ -6,6 +6,7 @@
#include "xscript/context.h"
#include "xscript/response.h"
#include "xscript/authorizer.h"
+#include "xscript/string_utils.h"
#ifdef HAVE_DMALLOC_H
#include <dmalloc.h>
View
@@ -10,6 +10,7 @@
#include "xscript/util.h"
#include "xscript/range.h"
#include "xscript/encoder.h"
+#include "xscript/string_utils.h"
#include "internal/expect.h"
#ifdef HAVE_DMALLOC_H
@@ -3,6 +3,7 @@
#include "xscript/operation_mode.h"
#include "xscript/response.h"
#include "xscript/util.h"
+#include "xscript/string_utils.h"
#include <stdexcept>
View
@@ -7,6 +7,7 @@
#include "xscript/policy.h"
#include "xscript/request.h"
#include "xscript/util.h"
+#include "xscript/string_utils.h"
#include "xscript/vhost_data.h"
#ifdef HAVE_DMALLOC_H
View
@@ -0,0 +1,159 @@
+#include <libxml/tree.h>
+#include <stdexcept>
+#include <memory>
+#include "xscript/string_utils.h"
+#include "xscript/encoder.h"
+#include "xscript/algorithm.h"
+
+namespace xscript {
+
+static const int NEXT_UTF8[256] = {
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
+ 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
+};
+
+std::string
+StringUtils::urlencode(const Range &range) {
+
+ std::string result;
+ result.reserve(3 * range.size());
+
+ for (const char* i = range.begin(), *end = range.end(); i != end; ++i) {
+ char symbol = (*i);
+ if (isalnum(symbol)) {
+ result.append(1, symbol);
+ continue;
+ }
+ switch (symbol) {
+ case '-':
+ case '_':
+ case '.':
+ case '!':
+ case '~':
+ case '*':
+ case '(':
+ case ')':
+ case '\'':
+ result.append(1, symbol);
+ break;
+ default:
+ result.append(1, '%');
+ char bytes[3] = { 0, 0, 0 };
+ bytes[0] = (symbol & 0xF0) / 16 ;
+ bytes[0] += (bytes[0] > 9) ? 'A' - 10 : '0';
+ bytes[1] = symbol & 0x0F;
+ bytes[1] += (bytes[1] > 9) ? 'A' - 10 : '0';
+ result.append(bytes, sizeof(bytes) - 1);
+ break;
+ }
+ }
+ return result;
+}
+
+std::string
+StringUtils::urldecode(const Range &range) {
+
+ std::string result;
+ result.reserve(range.size());
+
+ for (const char *i = range.begin(), *end = range.end(); i != end; ++i) {
+ switch (*i) {
+ case '+':
+ result.push_back(' ');
+ break;
+ case '%':
+ if (std::distance(i, end) > 2) {
+ int digit;
+ char f = *(i + 1), s = *(i + 2);
+ digit = (f >= 'A' ? ((f & 0xDF) - 'A') + 10 : (f - '0')) * 16;
+ digit += (s >= 'A') ? ((s & 0xDF) - 'A') + 10 : (s - '0');
+ if (digit == 0) {
+ throw std::runtime_error("Null symbol in URL is not allowed");
+ }
+ result.push_back(static_cast<char>(digit));
+ i += 2;
+ }
+ else {
+ result.push_back('%');
+ }
+ break;
+ default:
+ result.push_back(*i);
+ break;
+ }
+ }
+ return result;
+}
+
+void
+StringUtils::parse(const Range &range, std::vector<NamedValue> &v, Encoder *encoder) {
+
+ Range part = range, key, value, head, tail;
+ std::auto_ptr<Encoder> enc(NULL);
+ if (NULL == encoder) {
+ enc = Encoder::createDefault("cp1251", "utf-8");
+ encoder = enc.get();
+ }
+ while (!part.empty()) {
+ splitFirstOf(part, "&;", head, tail);
+ split(head, '=', key, value);
+ if (!key.empty()) {
+ std::pair<std::string, std::string> p(urldecode(key), urldecode(value));
+ if (!xmlCheckUTF8((const xmlChar*) p.first.c_str())) {
+ encoder->encode(p.first).swap(p.first);
+ }
+ if (!xmlCheckUTF8((const xmlChar*) p.second.c_str())) {
+ encoder->encode(p.second).swap(p.second);
+ }
+ v.push_back(p);
+ }
+ part = tail;
+ }
+}
+
+const char*
+wrapStrerror(int, const char *value) {
+ return value;
+}
+
+const char*
+wrapStrerror(const char *value, const char *) {
+ return value;
+}
+
+void
+StringUtils::report(const char *what, int error, std::ostream &stream) {
+ char buffer[256];
+ stream << what << wrapStrerror(strerror_r(error, buffer, sizeof(buffer)), buffer);
+}
+
+std::string
+StringUtils::tolower(const std::string& str) {
+ std::string result;
+ result.reserve(str.size());
+ int (*func)(int) = &std::tolower;
+ std::transform(str.begin(), str.end(), std::back_inserter(result), func);
+ return result;
+}
+
+std::string
+StringUtils::toupper(const std::string& str) {
+ std::string result;
+ result.reserve(str.size());
+ int (*func)(int) = &std::toupper;
+ std::transform(str.begin(), str.end(), std::back_inserter(result), func);
+ return result;
+}
+
+const char*
+StringUtils::nextUTF8(const char* data) {
+ return data + NEXT_UTF8[static_cast<unsigned char>(*data)];
+}
+
+}
View
@@ -9,6 +9,7 @@
#include "xscript/param.h"
#include "xscript/tagged_block.h"
#include "xscript/util.h"
+#include "xscript/string_utils.h"
#ifdef HAVE_DMALLOC_H
#include <dmalloc.h>
Oops, something went wrong.

0 comments on commit 76a2a23

Please sign in to comment.