Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
String.h
1 /*
2  * type_info class interface
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_STRING_H
22 #define ELM_STRING_STRING_H
23 
24 #include <elm/PreIterator.h>
25 #include <elm/string/CString.h>
26 
27 namespace elm {
28 
29 // String class
30 class String {
31  friend class CString;
32  friend class StringBuffer;
33 
34  // Data structure
35  typedef struct buffer_t {
36  unsigned short use;
37  char buf[1];
38  } buffer_t;
39  static buffer_t empty_buf;
40  static const int zero_off = sizeof(unsigned short);
41  mutable const char *buf;
42  mutable unsigned short off, len;
43 
44  // Internals
45  void copy(const char *str, int _len);
46  void lock(void) const { ((buffer_t *)buf)->use++; }
47  void toc(void) const;
48  void unlock(void) const {
49  ((buffer_t *)buf)->use--;
50  if(!((buffer_t *)buf)->use && buf != (char *)&empty_buf)
51  delete [] buf;
52  }
53  inline String(const char *_buf, int _off, int _len): buf(_buf), off(_off), len(_len) { lock(); };
54  static String concat(const char *s1, int l1, const char *s2, int l2);
55  inline String(buffer_t *buffer, int offset, int length)
56  : buf((char *)buffer), off(offset), len(length) { lock(); };
57 
58 public:
59  static String make(char chr);
60  static String make(String chr, int n);
61 
62  inline String(void): buf((char *)&empty_buf), off(zero_off), len(0) { lock(); };
63  inline String(const char *str, int _len) { copy(str, _len); };
64  inline String(const char *str) { if(!str) str = ""; copy(str, strlen(str)); };
65  inline String(cstring str) { copy(str.chars(), str.length()); };
66  inline String(const String& str): buf(str.buf), off(str.off), len(str.len) { lock(); };
67  inline ~String(void) { unlock(); };
68  inline String& operator=(const String& str)
69  { str.lock(); unlock(); buf = str.buf; off = str.off; len = str.len; return *this; };
70  inline String& operator=(const CString str)
71  { unlock(); copy(str.chars(), str.length()); return *this; };
72  inline String& operator=(const char *str)
73  { if(!str) str = ""; unlock(); copy(str, strlen(str)); return *this; };
74 
75  inline int length(void) const { return len; };
76  inline const char *chars(void) const { return buf + off; };
77  inline int compare(const String& str) const {
78  int res = memcmp(chars(), str.chars(), len > str.len ? str.len : len);
79  return res ? res : len - str.len;
80  };
81  inline int compare(const CString str) const {
82  int slen = str.length();
83  int res = memcmp(chars(), str.chars(), len > slen ? slen : len);
84  return res ? res : len - slen;
85  };
86 
87  inline bool isEmpty(void) const { return !len; };
88  inline operator bool(void) const { return !isEmpty(); };
89 
90  inline CString toCString(void) const { if(buf[off + len] != '\0') toc(); return chars(); };
91  inline const char *asNullTerminated() const { return toCString().chars(); };
92  inline const char *asSysString() const { return asNullTerminated(); }
93 
94  inline char charAt(int index) const { return buf[index + off]; };
95  inline char operator[](int index) const { return charAt(index); };
96  inline String substring(int _off) const { return String(buf, off + _off, len - _off); };
97  inline String substring(int _off, int _len) const { return String(buf, off + _off, _len); };
98 
99  inline String concat(const CString str) const { return concat(chars(), len, str.chars(), str.length()); };
100  inline String concat(const String& str) const { return concat(chars(), len, str.chars(), str.length()); };
101 
102  inline int indexOf(char chr) const { return indexOf(chr, 0); };
103  inline int indexOf(char chr, int pos) const
104  { for(const char *p = chars() + pos; p < chars() + len; p++) if(*p == chr) return p - chars(); return -1; };
105  int indexOf(const String& str, int pos = 0);
106  inline int lastIndexOf(char chr) const { return lastIndexOf(chr, length()); };
107  inline int lastIndexOf(char chr, int pos) const
108  { for(const char *p = chars() + pos - 1; p >= chars(); p--) if(*p == chr) return p - chars(); return -1; };
109  inline int lastIndexOf(const String& str) { return lastIndexOf(str, length()); }
110  int lastIndexOf(const String& str, int pos);
111 
112  inline bool startsWith(const char *str) const
113  { return startsWith(CString(str)); }
114  inline bool startsWith(const CString str) const
115  { int l = str.length(); return len >= l && !memcmp(chars(), str.chars(), l); }
116  inline bool startsWith(const String& str) const
117  { return len >= str.len && !memcmp(chars(), str.chars(), str.length()); }
118  inline bool endsWith(const char *str) const
119  { return endsWith(CString(str)); }
120  inline bool endsWith(const CString str) const
121  { int l = str.length(); return len >= l && !memcmp(chars() + len - l, str.chars(), l); }
122  inline bool endsWith(const String& str) const
123  { return len>= str.len && !memcmp(chars() + len - str.len, str.chars(), str.len); }
124 
125  String trim(void) const;
126  String ltrim(void) const;
127  String rtrim(void) const;
128  String replace(String pat, String sub);
129 
130  class Iter: public PreIterator<Iter, char> {
131  public:
132  inline Iter(const String& s, int i = 0): _s(s), _i(i) { }
133  inline bool atEnd() const { return _i >= _s.length(); }
134  inline char item() const { return _s[_i]; }
135  inline void next() { _i++; }
136  inline bool equals(const Iter& i) const { return _i == i._i; }
137  private:
138  const String& _s;
139  int _i;
140  };
141  inline Iter begin() const { return Iter(*this); }
142  inline Iter end() const { return Iter(*this, length()); }
143 
144  template <class C> inline String join(const C& coll);
145 };
146 
147 // Type shortcut
148 #ifndef ELM_NO_STRING_SHORTCUT
149  typedef String string;
150  inline string str(const char *s) { return string(s); }
151 #endif
152 
153 } // elm
154 
155 #endif // ELM_STRING_STRING_H
elm::String::lastIndexOf
int lastIndexOf(char chr) const
Definition: String.h:106
elm::String::make
static String make(char chr)
Definition: string_String.cpp:171
elm::String::endsWith
bool endsWith(const char *str) const
Definition: String.h:118
elm::String::operator[]
char operator[](int index) const
Definition: String.h:95
elm::String::chars
const char * chars(void) const
Definition: String.h:76
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::String::Iter::Iter
Iter(const String &s, int i=0)
Definition: String.h:132
elm::String::end
Iter end() const
Definition: String.h:142
elm::StringBuffer
Definition: StringBuffer.h:18
elm::String::String
String(const char *str)
Definition: String.h:64
elm::String::startsWith
bool startsWith(const CString str) const
Definition: String.h:114
elm::String::charAt
char charAt(int index) const
Definition: String.h:94
elm::String::begin
Iter begin() const
Definition: String.h:141
elm::String::indexOf
int indexOf(char chr, int pos) const
Definition: String.h:103
elm::String::indexOf
int indexOf(char chr) const
Definition: String.h:102
elm::String::Iter::atEnd
bool atEnd() const
Definition: String.h:133
elm::String::endsWith
bool endsWith(const CString str) const
Definition: String.h:120
elm::String::String
String(const String &str)
Definition: String.h:66
elm::String::replace
String replace(String pat, String sub)
Definition: string_String.cpp:565
elm::String::endsWith
bool endsWith(const String &str) const
Definition: String.h:122
elm::String::join
String join(const C &coll)
Definition: string.h:106
elm::string
String string
Definition: String.h:149
bool
elm::CString
Definition: CString.h:17
elm::String::String
String(void)
Definition: String.h:62
elm::String::lastIndexOf
int lastIndexOf(char chr, int pos) const
Definition: String.h:107
elm::String::Iter::next
void next()
Definition: String.h:135
elm::String::concat
String concat(const String &str) const
Definition: String.h:100
elm::String::length
int length(void) const
Definition: String.h:75
elm
Definition: adapter.h:26
elm::CString::chars
const char * chars(void) const
Definition: CString.h:27
elm::String::String
String(cstring str)
Definition: String.h:65
elm::String::trim
String trim(void) const
Definition: string_String.cpp:533
elm::String::substring
String substring(int _off) const
Definition: String.h:96
elm::String::compare
int compare(const String &str) const
Definition: String.h:77
elm::String::rtrim
String rtrim(void) const
Definition: string_String.cpp:554
elm::String::lastIndexOf
int lastIndexOf(const String &str)
Definition: String.h:109
elm::String::Iter::equals
bool equals(const Iter &i) const
Definition: String.h:136
elm::String::asSysString
const char * asSysString() const
Definition: String.h:92
elm::String::substring
String substring(int _off, int _len) const
Definition: String.h:97
elm::String::startsWith
bool startsWith(const String &str) const
Definition: String.h:116
elm::String
Definition: String.h:30
elm::String::Iter::item
char item() const
Definition: String.h:134
elm::String::operator=
String & operator=(const String &str)
Definition: String.h:68
elm::String::ltrim
String ltrim(void) const
Definition: string_String.cpp:542
elm::String::asNullTerminated
const char * asNullTerminated() const
Definition: String.h:91
elm::String::startsWith
bool startsWith(const char *str) const
Definition: String.h:112
elm::str
string str(const char *s)
Definition: String.h:150
elm::String::Iter
Definition: String.h:130
elm::String::toCString
CString toCString(void) const
Definition: String.h:90
elm::String::isEmpty
bool isEmpty(void) const
Definition: String.h:87
elm::PreIterator
Definition: iter.h:28
elm::String::String
String(const char *str, int _len)
Definition: String.h:63
elm::String::operator=
String & operator=(const CString str)
Definition: String.h:70
elm::String::operator=
String & operator=(const char *str)
Definition: String.h:72
elm::String::~String
~String(void)
Definition: String.h:67
elm::t::offset
uint64 offset
Definition: arch.h:36
elm::String::CString
friend class CString
Definition: String.h:31
elm::String::concat
String concat(const CString str) const
Definition: String.h:99
elm::String::compare
int compare(const CString str) const
Definition: String.h:81