From 49e60e576f20f43c91dc61bc028ba66c750411a2 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 11 Feb 2015 16:18:43 +1100 Subject: [PATCH] view: extract span class into standalone class --- Makefile.am | 2 ++ uri.cpp.rl | 79 +++++++++-------------------------------------------- uri.hpp | 22 ++++----------- view.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ view.hpp | 42 ++++++++++++++++++++++++++++ 5 files changed, 138 insertions(+), 83 deletions(-) create mode 100644 view.cpp create mode 100644 view.hpp diff --git a/Makefile.am b/Makefile.am index 023cd56f..bcbd1596 100644 --- a/Makefile.am +++ b/Makefile.am @@ -168,6 +168,8 @@ UTIL_FILES = \ vector.ipp \ version.cpp \ version.hpp \ + view.cpp \ + view.hpp \ zlib.cpp \ zlib.hpp diff --git a/uri.cpp.rl b/uri.cpp.rl index 179a5ee1..b03287a6 100644 --- a/uri.cpp.rl +++ b/uri.cpp.rl @@ -22,9 +22,7 @@ #include "debug.hpp" #include -#include #include -#include %%{ @@ -34,20 +32,20 @@ action success {__success = true; } action failure {__success = false; } - action scheme_begin { m_spans[SCHEME].begin = p; } - action scheme_end { m_spans[SCHEME].end = p; } + action scheme_begin { m_views[SCHEME].begin = p; } + action scheme_end { m_views[SCHEME].end = p; } - action authority_begin { m_spans[AUTHORITY].begin = p; } - action authority_end { m_spans[AUTHORITY].end = p; } + action authority_begin { m_views[AUTHORITY].begin = p; } + action authority_end { m_views[AUTHORITY].end = p; } - action path_begin { m_spans[PATH].begin = p; } - action path_end { m_spans[PATH].end = p; } + action path_begin { m_views[PATH].begin = p; } + action path_end { m_views[PATH].end = p; } - action query_begin { m_spans[QUERY].begin = p; } - action query_end { m_spans[QUERY].end = p; } + action query_begin { m_views[QUERY].begin = p; } + action query_end { m_views[QUERY].end = p; } - action fragment_begin { m_spans[FRAGMENT].begin = p; } - action fragment_end { m_spans[FRAGMENT].end = p; } + action fragment_begin { m_views[FRAGMENT].begin = p; } + action fragment_end { m_views[FRAGMENT].end = p; } ## Characters unreserved = alpha | digit | "-" | "." | "_" | "~"; @@ -157,49 +155,6 @@ }%% -/////////////////////////////////////////////////////////////////////////////// -// Span - -util::uri::span::span (): - begin (nullptr), - end (nullptr) -{ ; } - - -//----------------------------------------------------------------------------- -util::uri::span::span (const char *str): - begin (str), - end (str + strlen (str)) -{ ; } - - -//----------------------------------------------------------------------------- -bool -util::uri::span::span::empty (void) const -{ - return begin == nullptr || - end == nullptr || - begin == end; -} - - -//----------------------------------------------------------------------------- -size_t -util::uri::span::size (void) const -{ - return end - begin; -} - - -//----------------------------------------------------------------------------- -const char& -util::uri::span::operator[] (size_t idx) const -{ - CHECK_LT (begin + idx, end); - return begin[idx]; -} - - /////////////////////////////////////////////////////////////////////////////// // URI @@ -235,11 +190,11 @@ util::uri::uri (std::string &&_value): //----------------------------------------------------------------------------- -util::uri::span +util::view util::uri::get (util::uri::component c) { CHECK_NEQ (c, NUM_COMPONENTS); - return m_spans[c]; + return m_views[c]; } @@ -262,7 +217,7 @@ hex_to_uint (char c) //----------------------------------------------------------------------------- std::string -util::uri::percent_decode (span s) +util::uri::percent_decode (view s) { if (s.size () == 0) return std::string (); @@ -304,14 +259,6 @@ util::uri::percent_decode (span s) } -//----------------------------------------------------------------------------- -std::ostream& -util::operator<< (std::ostream &os, util::uri::span s) -{ - std::copy (s.begin, s.end, std::ostream_iterator (os)); - return os; -} - //----------------------------------------------------------------------------- std::ostream& util::operator<< (std::ostream &os, util::uri::component c) diff --git a/uri.hpp b/uri.hpp index c9df8c47..17bfc786 100644 --- a/uri.hpp +++ b/uri.hpp @@ -21,8 +21,9 @@ #ifndef __UTIL_URI_HPP #define __UTIL_URI_HPP +#include "view.hpp" + #include -#include #include @@ -46,29 +47,16 @@ namespace util { NUM_COMPONENTS }; - struct span { - span (); - span (const char*); + view get (component); - const char *begin; - const char *end; - - bool empty () const; - size_t size (void) const; - const char& operator[] (size_t) const; - }; - - span get (component); - - static std::string percent_decode (span); + static std::string percent_decode (view); private: - span m_spans[NUM_COMPONENTS]; + view m_views[NUM_COMPONENTS]; std::string m_value; }; std::ostream& operator<< (std::ostream&, uri::component); - std::ostream& operator<< (std::ostream&, uri::span); } diff --git a/view.cpp b/view.cpp new file mode 100644 index 00000000..49e97676 --- /dev/null +++ b/view.cpp @@ -0,0 +1,76 @@ +/* + * This file is part of libgim. + * + * libgim is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * libgim is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with libgim. If not, see . + * + * Copyright 2015 Danny Robson + */ + + +#include "view.hpp" + +#include "debug.hpp" + +#include +#include + +//----------------------------------------------------------------------------- +util::view::view (): + begin (nullptr), + end (nullptr) +{ ; } + + +//----------------------------------------------------------------------------- +util::view::view (const char *str): + begin (str), + end (str + strlen (str)) +{ ; } + + +//----------------------------------------------------------------------------- +bool +util::view::view::empty (void) const +{ + return begin == nullptr || + end == nullptr || + begin == end; +} + + +//----------------------------------------------------------------------------- +size_t +util::view::size (void) const +{ + return end - begin; +} + + +//----------------------------------------------------------------------------- +const char& +util::view::operator[] (size_t idx) const +{ + CHECK_LT (begin + idx, end); + return begin[idx]; +} + + +//----------------------------------------------------------------------------- +std::ostream& +util::operator<< (std::ostream &os, util::view s) +{ + std::copy (s.begin, s.end, std::ostream_iterator (os)); + return os; +} + diff --git a/view.hpp b/view.hpp new file mode 100644 index 00000000..9133c0d0 --- /dev/null +++ b/view.hpp @@ -0,0 +1,42 @@ +/* + * This file is part of libgim. + * + * libgim is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * libgim is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with libgim. If not, see . + * + * Copyright 2015 Danny Robson + */ + + +#ifndef __UTIL_VIEW_HPP +#define __UTIL_VIEW_HPP + +#include +#include + +namespace util { + struct view { + view (); + view (const char*); + + const char *begin; + const char *end; + + bool empty () const; + size_t size (void) const; + const char& operator[] (size_t) const; + }; + + std::ostream& operator<< (std::ostream&, view); +} +#endif