textwolf  0.2
cstringiterator.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
10 
11 #ifndef __TEXTWOLF_CSTRING_ITERATOR_HPP__
12 #define __TEXTWOLF_CSTRING_ITERATOR_HPP__
13 #include <string>
14 #include <cstring>
15 #include <cstdlib>
16 
19 namespace textwolf {
20 
24 {
25 public:
28  :m_src(0)
29  ,m_size(0)
30  ,m_pos(0){}
31 
35  CStringIterator( const char* src, unsigned int size)
36  :m_src(src)
37  ,m_size(size)
38  ,m_pos(0){}
39 
42  CStringIterator( const char* src)
43  :m_src(src)
44  ,m_size(std::strlen(src))
45  ,m_pos(0){}
46 
49  CStringIterator( const std::string& src)
50  :m_src(src.c_str())
51  ,m_size(src.size())
52  ,m_pos(0){}
53 
57  :m_src(o.m_src)
58  ,m_size(o.m_size)
59  ,m_pos(o.m_pos){}
60 
63  inline char operator* ()
64  {
65  return (m_pos < m_size)?m_src[m_pos]:0;
66  }
67 
70  {
71  m_pos++;
72  return *this;
73  }
74 
76  inline unsigned int pos() const {return m_pos;}
77 
79  inline void pos( unsigned int i) {m_pos=(i<m_size)?i:m_size;}
80 
81  inline int operator - (const CStringIterator& o) const
82  {
83  if (m_src != o.m_src) return 0;
84  return (int)(m_pos - o.m_pos);
85  }
86 
87 private:
88  const char* m_src;
89  unsigned int m_size;
90  unsigned int m_pos;
91 };
92 
93 }//namespace
94 #endif
CStringIterator(const char *src, unsigned int size)
Constructor.
Definition: cstringiterator.hpp:35
CStringIterator()
Default constructor.
Definition: cstringiterator.hpp:27
void pos(unsigned int i)
Set current char position.
Definition: cstringiterator.hpp:79
CStringIterator(const char *src)
Constructor.
Definition: cstringiterator.hpp:42
int operator-(const CStringIterator &o) const
Definition: cstringiterator.hpp:81
CStringIterator(const std::string &src)
Constructor.
Definition: cstringiterator.hpp:49
Input iterator on a constant string returning null characters after EOF as required by textwolf scann...
Definition: cstringiterator.hpp:23
CStringIterator & operator++()
Preincrement.
Definition: cstringiterator.hpp:69
char operator*()
Element access.
Definition: cstringiterator.hpp:63
CStringIterator(const CStringIterator &o)
Copy constructor.
Definition: cstringiterator.hpp:56
unsigned int pos() const
Return current char position.
Definition: cstringiterator.hpp:76