uri: use begin and end methods, not variables

This commit is contained in:
Danny Robson 2015-02-11 16:41:09 +11:00
parent 49e60e576f
commit e14b17c0bd
3 changed files with 64 additions and 33 deletions

View File

@ -32,20 +32,20 @@
action success {__success = true; } action success {__success = true; }
action failure {__success = false; } action failure {__success = false; }
action scheme_begin { m_views[SCHEME].begin = p; } action scheme_begin { m_views[SCHEME] = { p, nullptr }; }
action scheme_end { m_views[SCHEME].end = p; } action scheme_end { m_views[SCHEME] = { m_views[SCHEME].begin (), p }; }
action authority_begin { m_views[AUTHORITY].begin = p; } action authority_begin { m_views[AUTHORITY] = { p, nullptr}; }
action authority_end { m_views[AUTHORITY].end = p; } action authority_end { m_views[AUTHORITY] = { m_views[AUTHORITY].begin (), p }; }
action path_begin { m_views[PATH].begin = p; } action path_begin { m_views[PATH] = { p, nullptr}; }
action path_end { m_views[PATH].end = p; } action path_end { m_views[PATH] = { m_views[PATH].begin (), p }; }
action query_begin { m_views[QUERY].begin = p; } action query_begin { m_views[QUERY] = { p, nullptr}; }
action query_end { m_views[QUERY].end = p; } action query_end { m_views[QUERY] = { m_views[QUERY].begin (), p }; }
action fragment_begin { m_views[FRAGMENT].begin = p; } action fragment_begin { m_views[FRAGMENT] = { p, nullptr}; }
action fragment_end { m_views[FRAGMENT].end = p; } action fragment_end { m_views[FRAGMENT] = { m_views[FRAGMENT].begin (), p }; }
## Characters ## Characters
unreserved = alpha | digit | "-" | "." | "_" | "~"; unreserved = alpha | digit | "-" | "." | "_" | "~";
@ -224,11 +224,11 @@ util::uri::percent_decode (view s)
// Early check for late percent-encoding so we can simplify the decode loop // Early check for late percent-encoding so we can simplify the decode loop
{ {
auto tail = std::find (s.size () < 3 ? s.begin auto tail = std::find (s.size () < 3 ? s.begin ()
: s.end - 2, : s.end () - 2,
s.end, s.end (),
'%'); '%');
if (tail != s.end) if (tail != s.end ())
throw parse_error ("triple overlaps end"); throw parse_error ("triple overlaps end");
} }
@ -240,11 +240,11 @@ util::uri::percent_decode (view s)
// Find the percent, copy until that, decode, advance, repeat. // Find the percent, copy until that, decode, advance, repeat.
auto out_cursor = out.begin (); auto out_cursor = out.begin ();
for (auto i = s.begin; i < s.end; ++i) { for (auto i = s.begin (); i < s.end (); ++i) {
auto cursor = std::find (i, s.end, '%'); auto cursor = std::find (i, s.end (), '%');
if (cursor == s.end) { if (cursor == s.end ()) {
out_cursor = std::copy (i, s.end, out_cursor); out_cursor = std::copy (i, s.end (), out_cursor);
break; break;
} }

View File

@ -27,25 +27,49 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
util::view::view (): util::view::view ():
begin (nullptr), m_begin (nullptr),
end (nullptr) m_end (nullptr)
{ ; } { ; }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
util::view::view (const char *str): util::view::view (const char *str):
begin (str), m_begin (str),
end (str + strlen (str)) m_end (str + strlen (str))
{ ; } { ; }
//-----------------------------------------------------------------------------
util::view::view (const char *_begin,
const char *_end):
m_begin (_begin),
m_end (_end)
{ ; }
//-----------------------------------------------------------------------------
const char*
util::view::begin (void) const
{
return m_begin;
}
//-----------------------------------------------------------------------------
const char*
util::view::end (void) const
{
return m_end;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool bool
util::view::view::empty (void) const util::view::view::empty (void) const
{ {
return begin == nullptr || return m_begin == nullptr ||
end == nullptr || m_end == nullptr ||
begin == end; m_begin == m_end;
} }
@ -53,7 +77,7 @@ util::view::view::empty (void) const
size_t size_t
util::view::size (void) const util::view::size (void) const
{ {
return end - begin; return m_end - m_begin;
} }
@ -61,8 +85,8 @@ util::view::size (void) const
const char& const char&
util::view::operator[] (size_t idx) const util::view::operator[] (size_t idx) const
{ {
CHECK_LT (begin + idx, end); CHECK_LT (m_begin + idx, m_end);
return begin[idx]; return m_begin[idx];
} }
@ -70,7 +94,7 @@ util::view::operator[] (size_t idx) const
std::ostream& std::ostream&
util::operator<< (std::ostream &os, util::view s) util::operator<< (std::ostream &os, util::view s)
{ {
std::copy (s.begin, s.end, std::ostream_iterator<char> (os)); std::copy (s.begin (), s.end (), std::ostream_iterator<char> (os));
return os; return os;
} }

View File

@ -25,18 +25,25 @@
#include <iostream> #include <iostream>
namespace util { namespace util {
struct view { class view {
public:
view (); view ();
view (const char*); view (const char *str);
view (const char *first, const char *last);
const char *begin; const char *begin (void) const;
const char *end; const char *end (void) const;
bool empty () const; bool empty () const;
size_t size (void) const; size_t size (void) const;
const char& operator[] (size_t) const; const char& operator[] (size_t) const;
private:
const char *m_begin;
const char *m_end;
}; };
std::ostream& operator<< (std::ostream&, view); std::ostream& operator<< (std::ostream&, view);
} }
#endif #endif