view: extract span class into standalone class

This commit is contained in:
Danny Robson 2015-02-11 16:18:43 +11:00
parent 2a72943ad7
commit 49e60e576f
5 changed files with 138 additions and 83 deletions

View File

@ -168,6 +168,8 @@ UTIL_FILES = \
vector.ipp \
version.cpp \
version.hpp \
view.cpp \
view.hpp \
zlib.cpp \
zlib.hpp

View File

@ -22,9 +22,7 @@
#include "debug.hpp"
#include <algorithm>
#include <cstring>
#include <iostream>
#include <iterator>
%%{
@ -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<char> (os));
return os;
}
//-----------------------------------------------------------------------------
std::ostream&
util::operator<< (std::ostream &os, util::uri::component c)

22
uri.hpp
View File

@ -21,8 +21,9 @@
#ifndef __UTIL_URI_HPP
#define __UTIL_URI_HPP
#include "view.hpp"
#include <string>
#include <cstdlib>
#include <stdexcept>
@ -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);
}

76
view.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#include "view.hpp"
#include "debug.hpp"
#include <cstring>
#include <iterator>
//-----------------------------------------------------------------------------
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<char> (os));
return os;
}

42
view.hpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_VIEW_HPP
#define __UTIL_VIEW_HPP
#include <cstdlib>
#include <iostream>
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