lib/SkipWhiteSpaceIterator.h

Code
Comments
Other
Rev Date Author Line
1549 24 Oct 12 peter 1 #ifndef theplu_svndigest_skip_white_space_iterator
1549 24 Oct 12 peter 2 #define theplu_svndigest_skip_white_space_iterator
1549 24 Oct 12 peter 3
1549 24 Oct 12 peter 4 // $Id$
1549 24 Oct 12 peter 5
1549 24 Oct 12 peter 6 /*
1549 24 Oct 12 peter 7   Copyright (C) 2012 Peter Johansson
1549 24 Oct 12 peter 8
1549 24 Oct 12 peter 9   This file is part of svndigest, http://dev.thep.lu.se/svndigest
1549 24 Oct 12 peter 10
1549 24 Oct 12 peter 11   svndigest is free software; you can redistribute it and/or modify it
1549 24 Oct 12 peter 12   under the terms of the GNU General Public License as published by
1549 24 Oct 12 peter 13   the Free Software Foundation; either version 3 of the License, or
1549 24 Oct 12 peter 14   (at your option) any later version.
1549 24 Oct 12 peter 15
1549 24 Oct 12 peter 16   svndigest is distributed in the hope that it will be useful, but
1549 24 Oct 12 peter 17   WITHOUT ANY WARRANTY; without even the implied warranty of
1549 24 Oct 12 peter 18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1549 24 Oct 12 peter 19   General Public License for more details.
1549 24 Oct 12 peter 20
1549 24 Oct 12 peter 21   You should have received a copy of the GNU General Public License
1549 24 Oct 12 peter 22   along with svndigest. If not, see <http://www.gnu.org/licenses/>.
1549 24 Oct 12 peter 23 */
1549 24 Oct 12 peter 24
1549 24 Oct 12 peter 25 #include <iterator>
1549 24 Oct 12 peter 26 #include <string>
1549 24 Oct 12 peter 27
1549 24 Oct 12 peter 28 namespace theplu {
1549 24 Oct 12 peter 29 namespace svndigest {
1549 24 Oct 12 peter 30
1549 24 Oct 12 peter 31   /**
1549 24 Oct 12 peter 32      \brief A wrapper around std::string::const_iterator.
1549 24 Oct 12 peter 33
1549 24 Oct 12 peter 34      Iterator behaves as its base std::string::const_iterator except
1549 24 Oct 12 peter 35      when incremented it skips white-spaces. Iterator is non-mutable
1549 24 Oct 12 peter 36      and only have forward iterator features, i.e., no random
1549 24 Oct 12 peter 37      access. Iterator could easily be extended to be a bidirectional
1549 24 Oct 12 peter 38      iterator by adding a 'operator--'.
1549 24 Oct 12 peter 39
1549 24 Oct 12 peter 40      Constructor takes an end-of-range iterator that typically is
1549 24 Oct 12 peter 41      'std::string::end()'. This is needed because when 'base_'
1549 24 Oct 12 peter 42      iterates out of range, 'base_' is not dereferencable and it is
1549 24 Oct 12 peter 43      thus impossible to check if iterator points to a
1549 24 Oct 12 peter 44      white-space. Instead we stop incrementing 'base_' as soon as it
1549 24 Oct 12 peter 45      reaches 'end_'.
1549 24 Oct 12 peter 46   */
1549 24 Oct 12 peter 47   class SkipWhiteSpaceIterator
1549 24 Oct 12 peter 48   {
1549 24 Oct 12 peter 49   public:
1549 24 Oct 12 peter 50     typedef std::forward_iterator_tag iterator_category;
1549 24 Oct 12 peter 51     typedef std::string::const_iterator::value_type value_type;
1549 24 Oct 12 peter 52     typedef std::string::const_iterator::difference_type difference_type;
1549 24 Oct 12 peter 53     typedef std::string::const_iterator::pointer pointer;
1549 24 Oct 12 peter 54     typedef std::string::const_iterator::reference reference;
1549 24 Oct 12 peter 55
1549 24 Oct 12 peter 56     /**
1549 24 Oct 12 peter 57        Create iterator pointing to same item as \a iter.
1549 24 Oct 12 peter 58
1549 24 Oct 12 peter 59        \a end defines end of range and the iterator will not iterate
1549 24 Oct 12 peter 60        passed this point.
1549 24 Oct 12 peter 61      */
1549 24 Oct 12 peter 62     SkipWhiteSpaceIterator(std::string::const_iterator iter,
1549 24 Oct 12 peter 63                            std::string::const_iterator end);
1549 24 Oct 12 peter 64
1549 24 Oct 12 peter 65     /**
1549 24 Oct 12 peter 66        return underlying string iterator
1549 24 Oct 12 peter 67      */
1549 24 Oct 12 peter 68     const std::string::const_iterator& base(void) const;
1549 24 Oct 12 peter 69
1549 24 Oct 12 peter 70     /**
1549 24 Oct 12 peter 71        return const reference to character
1549 24 Oct 12 peter 72      */
1549 24 Oct 12 peter 73     reference operator*(void) const;
1549 24 Oct 12 peter 74
1549 24 Oct 12 peter 75     /**
1549 24 Oct 12 peter 76        return pointer
1549 24 Oct 12 peter 77      */
1549 24 Oct 12 peter 78     pointer operator->(void) const;
1549 24 Oct 12 peter 79
1549 24 Oct 12 peter 80     /**
1549 24 Oct 12 peter 81        Increment iterator to next non-white-space character or end
1549 24 Oct 12 peter 82        position.
1549 24 Oct 12 peter 83      */
1549 24 Oct 12 peter 84     SkipWhiteSpaceIterator& operator++(void);
1549 24 Oct 12 peter 85
1549 24 Oct 12 peter 86   private:
1549 24 Oct 12 peter 87     std::string::const_iterator base_;
1549 24 Oct 12 peter 88     std::string::const_iterator end_;
1549 24 Oct 12 peter 89     // user compiler generated copy
1549 24 Oct 12 peter 90     //SkipWhiteSpaceIterator(const SkipWhiteSpaceIterator&);
1549 24 Oct 12 peter 91     //SkipWhiteSpaceIterator& operator=(const SkipWhiteSpaceIterator&);
1549 24 Oct 12 peter 92   };
1549 24 Oct 12 peter 93
1549 24 Oct 12 peter 94   bool operator==(const SkipWhiteSpaceIterator& lhs,
1549 24 Oct 12 peter 95                   const SkipWhiteSpaceIterator& rhs);
1549 24 Oct 12 peter 96
1549 24 Oct 12 peter 97   bool operator!=(const SkipWhiteSpaceIterator& lhs,
1549 24 Oct 12 peter 98                   const SkipWhiteSpaceIterator& rhs);
1549 24 Oct 12 peter 99
1549 24 Oct 12 peter 100
1549 24 Oct 12 peter 101 }} // end of namespace svndigest and namespace theplu
1549 24 Oct 12 peter 102 #endif