aboutsummaryrefslogtreecommitdiffstats
path: root/utility.h
diff options
context:
space:
mode:
authoripknHama <ipknhama@gmail.com>2014-04-03 01:38:08 +0900
committeripknHama <ipknhama@gmail.com>2014-04-03 08:29:22 +0900
commit5e5d69688435ffe8fc148af73fe33b029d9f6110 (patch)
tree1a37f2927e6825482404307346661a7b1c34300d /utility.h
parent4fdff79719af8d024f176bbdc03a57516e932602 (diff)
downloadcrow-5e5d69688435ffe8fc148af73fe33b029d9f6110.tar.gz
crow-5e5d69688435ffe8fc148af73fe33b029d9f6110.zip
preparing compile time url parsing
Diffstat (limited to 'utility.h')
-rw-r--r--utility.h52
1 files changed, 52 insertions, 0 deletions
diff --git a/utility.h b/utility.h
new file mode 100644
index 0000000..c8575d3
--- /dev/null
+++ b/utility.h
@@ -0,0 +1,52 @@
+#pragma once
+
+namespace flask
+{
+ namespace black_magic
+ {
+ struct OutOfRange
+ {
+ OutOfRange(unsigned pos, unsigned length) {}
+ };
+ constexpr unsigned requires_in_range( unsigned i, unsigned len )
+ {
+ return i >= len ? throw OutOfRange(i, len) : i;
+ }
+
+ // from http://akrzemi1.wordpress.com/2011/05/11/parsing-strings-at-compile-time-part-i/
+ class StrWrap
+ {
+ const char * const begin_;
+ unsigned size_;
+
+ public:
+ template< unsigned N >
+ constexpr StrWrap( const char(&arr)[N] ) : begin_(arr), size_(N - 1) {
+ static_assert( N >= 1, "not a string literal");
+ }
+
+ constexpr char operator[]( unsigned i ) {
+ return requires_in_range(i, size_), begin_[i];
+ }
+
+ constexpr operator const char *() {
+ return begin_;
+ }
+
+ constexpr unsigned size() {
+ return size_;
+ }
+ };
+
+
+ constexpr int find_closing_tag(StrWrap s, std::size_t p)
+ {
+ return s[p] == '>' ? p : find_closing_tag(s, p+1);
+ }
+
+ constexpr int count(StrWrap s, int i=0)
+ {
+ return i == s.size() ? 0 : s[i] == '<' ? 1+count(s,i+1) : count(s,i+1);
+ }
+ }
+}