Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Split.h
1 /*
2  * Split class
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2004-08, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_STRING_SPLIT_H_
22 #define ELM_STRING_SPLIT_H_
23 
24 #include <elm/string/String.h>
25 
26 namespace elm {
27 
28 class StringSplit {
29 public:
30  inline StringSplit(void): l(-1), p(-1) { }
31  inline StringSplit(const String& str, char chr): s(str), ss(String::make(chr)), l(-1), p(-1) { find(); }
32  inline StringSplit(const String& str, String sub): s(str), ss(sub), l(-1), p(-1) { find(); }
33 
34  inline bool ended(void) const { return l >= s.length(); }
35  inline String item(void) const { return s.substring(l + 1, p - l - 1); }
36  inline void next(void) { if(p >= s.length()) l = s.length(); else find(); }
37  inline bool equals(const StringSplit& sp) const { return l == sp.l && p == sp.p; }
38 
39  inline operator bool() const { return !ended(); }
40  inline operator String() const { return item(); }
41  inline String operator*() const { return item(); }
42  inline StringSplit& operator++() { next(); return *this; }
43  inline StringSplit operator++(int) { StringSplit o = *this; next(); return o; }
44  inline bool operator==(const StringSplit& sp) const { return equals(sp); }
45  inline bool operator!=(const StringSplit& sp) const { return !equals(sp); }
46 
47 private:
48  inline void find(void)
49  { l = p; p = s.indexOf(ss, l + 1); if(p < 0) p = s.length(); }
50  String s;
51  String ss;
52  int l, p;
53 };
54 
55 } // elm
56 
57 #endif /* ELM_STRING_SPLIT_H_ */
elm::StringSplit::next
void next(void)
Definition: Split.h:36
elm::String::indexOf
int indexOf(char chr) const
Definition: String.h:102
elm::StringSplit
Definition: Split.h:28
elm::StringSplit::operator*
String operator*() const
Definition: Split.h:41
elm::StringSplit::StringSplit
StringSplit(const String &str, char chr)
Definition: Split.h:31
elm::StringSplit::StringSplit
StringSplit(void)
Definition: Split.h:30
elm::StringSplit::ended
bool ended(void) const
Definition: Split.h:34
elm::StringSplit::operator!=
bool operator!=(const StringSplit &sp) const
Definition: Split.h:45
elm::StringSplit::operator==
bool operator==(const StringSplit &sp) const
Definition: Split.h:44
bool
elm::String::length
int length(void) const
Definition: String.h:75
elm
Definition: adapter.h:26
elm::StringSplit::item
String item(void) const
Definition: Split.h:35
elm::StringSplit::StringSplit
StringSplit(const String &str, String sub)
Definition: Split.h:32
elm::String::substring
String substring(int _off) const
Definition: String.h:96
elm::StringSplit::equals
bool equals(const StringSplit &sp) const
Definition: Split.h:37
elm::String
Definition: String.h:30
elm::str
string str(const char *s)
Definition: String.h:150
elm::StringSplit::operator++
StringSplit & operator++()
Definition: Split.h:42
elm::StringSplit::operator++
StringSplit operator++(int)
Definition: Split.h:43