From e14b17c0bdea7445ecf3492fc21a2ce8d678b583 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 11 Feb 2015 16:41:09 +1100 Subject: [PATCH] uri: use begin and end methods, not variables --- uri.cpp.rl | 36 ++++++++++++++++++------------------ view.cpp | 46 +++++++++++++++++++++++++++++++++++----------- view.hpp | 15 +++++++++++---- 3 files changed, 64 insertions(+), 33 deletions(-) diff --git a/uri.cpp.rl b/uri.cpp.rl index b03287a6..f5753459 100644 --- a/uri.cpp.rl +++ b/uri.cpp.rl @@ -32,20 +32,20 @@ action success {__success = true; } action failure {__success = false; } - action scheme_begin { m_views[SCHEME].begin = p; } - action scheme_end { m_views[SCHEME].end = p; } + action scheme_begin { m_views[SCHEME] = { p, nullptr }; } + action scheme_end { m_views[SCHEME] = { m_views[SCHEME].begin (), p }; } - action authority_begin { m_views[AUTHORITY].begin = p; } - action authority_end { m_views[AUTHORITY].end = p; } + action authority_begin { m_views[AUTHORITY] = { p, nullptr}; } + action authority_end { m_views[AUTHORITY] = { m_views[AUTHORITY].begin (), p }; } - action path_begin { m_views[PATH].begin = p; } - action path_end { m_views[PATH].end = p; } + action path_begin { m_views[PATH] = { p, nullptr}; } + action path_end { m_views[PATH] = { m_views[PATH].begin (), p }; } - action query_begin { m_views[QUERY].begin = p; } - action query_end { m_views[QUERY].end = p; } + action query_begin { m_views[QUERY] = { p, nullptr}; } + action query_end { m_views[QUERY] = { m_views[QUERY].begin (), p }; } - action fragment_begin { m_views[FRAGMENT].begin = p; } - action fragment_end { m_views[FRAGMENT].end = p; } + action fragment_begin { m_views[FRAGMENT] = { p, nullptr}; } + action fragment_end { m_views[FRAGMENT] = { m_views[FRAGMENT].begin (), p }; } ## Characters 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 { - auto tail = std::find (s.size () < 3 ? s.begin - : s.end - 2, - s.end, + auto tail = std::find (s.size () < 3 ? s.begin () + : s.end () - 2, + s.end (), '%'); - if (tail != s.end) + if (tail != s.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. auto out_cursor = out.begin (); - for (auto i = s.begin; i < s.end; ++i) { - auto cursor = std::find (i, s.end, '%'); + for (auto i = s.begin (); i < s.end (); ++i) { + auto cursor = std::find (i, s.end (), '%'); - if (cursor == s.end) { - out_cursor = std::copy (i, s.end, out_cursor); + if (cursor == s.end ()) { + out_cursor = std::copy (i, s.end (), out_cursor); break; } diff --git a/view.cpp b/view.cpp index 49e97676..fe1c94c6 100644 --- a/view.cpp +++ b/view.cpp @@ -27,25 +27,49 @@ //----------------------------------------------------------------------------- util::view::view (): - begin (nullptr), - end (nullptr) + m_begin (nullptr), + m_end (nullptr) { ; } //----------------------------------------------------------------------------- util::view::view (const char *str): - begin (str), - end (str + strlen (str)) + m_begin (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 util::view::view::empty (void) const { - return begin == nullptr || - end == nullptr || - begin == end; + return m_begin == nullptr || + m_end == nullptr || + m_begin == m_end; } @@ -53,7 +77,7 @@ util::view::view::empty (void) const size_t util::view::size (void) const { - return end - begin; + return m_end - m_begin; } @@ -61,8 +85,8 @@ util::view::size (void) const const char& util::view::operator[] (size_t idx) const { - CHECK_LT (begin + idx, end); - return begin[idx]; + CHECK_LT (m_begin + idx, m_end); + return m_begin[idx]; } @@ -70,7 +94,7 @@ util::view::operator[] (size_t idx) const std::ostream& util::operator<< (std::ostream &os, util::view s) { - std::copy (s.begin, s.end, std::ostream_iterator (os)); + std::copy (s.begin (), s.end (), std::ostream_iterator (os)); return os; } diff --git a/view.hpp b/view.hpp index 9133c0d0..431b1cc0 100644 --- a/view.hpp +++ b/view.hpp @@ -25,18 +25,25 @@ #include namespace util { - struct view { + class view { + public: view (); - view (const char*); + view (const char *str); + view (const char *first, const char *last); - const char *begin; - const char *end; + const char *begin (void) const; + const char *end (void) const; bool empty () const; size_t size (void) const; const char& operator[] (size_t) const; + + private: + const char *m_begin; + const char *m_end; }; + std::ostream& operator<< (std::ostream&, view); } #endif