Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
String.h
1 /*
2  * $Id$
3  * type_info class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2004-08, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef ELM_STRING_STRING_H
23 #define ELM_STRING_STRING_H
24 
25 #include <assert.h>
26 #include <elm/string/CString.h>
27 
28 namespace elm {
29 
30 // String class
31 class String {
32  friend class CString;
33  friend class StringBuffer;
34 
35  // Data structure
36  typedef struct buffer_t {
37  unsigned short use;
38  char buf[1];
39  } buffer_t;
40  static buffer_t empty_buf;
41  static const int zero_off = sizeof(unsigned short);
42  mutable const char *buf;
43  mutable unsigned short off, len;
44 
45  // Internals
46  void copy(const char *str, int _len);
47  void lock(void) const { ((buffer_t *)buf)->use++; }
48  void toc(void) const;
49  void unlock(void) const {
50  ((buffer_t *)buf)->use--;
51  if(!((buffer_t *)buf)->use && buf != (char *)&empty_buf)
52  delete [] buf;
53  }
54  inline String(const char *_buf, int _off, int _len): buf(_buf), off(_off), len(_len) { lock(); };
55  static String concat(const char *s1, int l1, const char *s2, int l2);
56  inline String(buffer_t *buffer, int offset, int length)
57  : buf((char *)buffer), off(offset), len(length) { lock(); };
58 
59 public:
60  inline String(void): buf((char *)&empty_buf), off(zero_off), len(0) { lock(); };
61  inline String(const char *str, int _len) { copy(str, _len); };
62  inline String(const char *str) { if(!str) str = ""; copy(str, strlen(str)); };
63  inline String(cstring str) { copy(str.chars(), str.length()); };
64  inline String(const String& str): buf(str.buf), off(str.off), len(str.len) { lock(); };
65  inline ~String(void) { unlock(); };
66  inline String& operator=(const String& str)
67  { str.lock(); unlock(); buf = str.buf; off = str.off; len = str.len; return *this; };
68  inline String& operator=(const CString str)
69  { unlock(); copy(str.chars(), str.length()); return *this; };
70  inline String& operator=(const char *str)
71  { if(!str) str = ""; unlock(); copy(str, strlen(str)); return *this; };
72 
73  inline int length(void) const { return len; };
74  inline const char *chars(void) const { return buf + off; };
75  inline int compare(const String& str) const {
76  int res = memcmp(chars(), str.chars(), len > str.len ? str.len : len);
77  return res ? res : len - str.len;
78  };
79  inline int compare(const CString str) const {
80  int slen = str.length();
81  int res = memcmp(chars(), str.chars(), len > slen ? slen : len);
82  return res ? res : len - slen;
83  };
84 
85  inline bool isEmpty(void) const { return !len; };
86  inline operator bool(void) const { return !isEmpty(); };
87 
88  inline CString toCString(void) const { if(buf[off + len] != '\0') toc(); return chars(); };
89  inline const char *operator&(void) const { return toCString().chars(); };
90 
91  inline char charAt(int index) const { return buf[index + off]; };
92  inline char operator[](int index) const { return charAt(index); };
93  inline String substring(int _off) const { return String(buf, off + _off, len - _off); };
94  inline String substring(int _off, int _len) const { return String(buf, off + _off, _len); };
95 
96  inline String concat(const CString str) const { return concat(chars(), len, str.chars(), str.length()); };
97  inline String concat(const String& str) const { return concat(chars(), len, str.chars(), str.length()); };
98 
99  inline int indexOf(char chr) const { return indexOf(chr, 0); };
100  inline int indexOf(char chr, int pos) const
101  { for(const char *p = chars() + pos; p < chars() + len; p++) if(*p == chr) return p - chars(); return -1; };
102  int indexOf(const String& str, int pos = 0);
103  inline int lastIndexOf(char chr) const { return lastIndexOf(chr, length()); };
104  inline int lastIndexOf(char chr, int pos) const
105  { for(const char *p = chars() + pos - 1; p >= chars(); p--) if(*p == chr) return p - chars(); return -1; };
106  inline int lastIndexOf(const String& str) { return lastIndexOf(str, length()); }
107  int lastIndexOf(const String& str, int pos);
108 
109  inline bool startsWith(const char *str) const;
110  inline bool startsWith(const CString str) const;
111  inline bool startsWith(const String& str) const;
112  inline bool endsWith(const char *str) const;
113  inline bool endsWith(const CString str) const;
114  inline bool endsWith(const String& str) const;
115 
116  String trim(void) const;
117  String ltrim(void) const;
118  String rtrim(void) const;
119 };
120 
121 // Type shortcut
122 #ifndef ELM_NO_STRING_SHORTCUT
123  typedef String string;
124 #endif
125 
126 // Inlines
127 inline bool String::startsWith(const char *str) const {
128  return startsWith(CString(str));
129 }
130 inline bool String::startsWith(const CString str) const {
131  int l = str.length();
132  return len >= l && !memcmp(chars(), str.chars(), l);
133 }
134 inline bool String::startsWith(const String& str) const {
135  return len >= str.len && !memcmp(chars(), str.chars(), str.length());
136 }
137 inline bool String::endsWith(const char *str) const {
138  return endsWith(CString(str));
139 }
140 inline bool String::endsWith(const CString str) const {
141  int l = str.length();
142  return len >= l && !memcmp(chars() + len - l, str.chars(), l);
143 }
144 inline bool String::endsWith(const String& str) const {
145  return len>= str.len
146  && !memcmp(chars() + len - str.len, str.chars(), str.len);
147 }
148 
149 } // elm
150 
151 #endif // ELM_STRING_STRING_H
Definition: CString.h:17
String ltrim(void) const
Definition: string_String.cpp:509
String substring(int _off) const
Definition: String.h:93
String & operator=(const char *str)
Definition: String.h:70
String(const char *str, int _len)
Definition: String.h:61
String concat(const String &str) const
Definition: String.h:97
String & operator=(const String &str)
Definition: String.h:66
int compare(const String &str) const
Definition: String.h:75
String(const String &str)
Definition: String.h:64
String & operator=(const CString str)
Definition: String.h:68
String trim(void) const
Definition: string_String.cpp:500
String(cstring str)
Definition: String.h:63
int lastIndexOf(char chr, int pos) const
Definition: String.h:104
~String(void)
Definition: String.h:65
String(void)
Definition: String.h:60
String concat(const CString str) const
Definition: String.h:96
String string
Definition: String.h:123
uint32 offset
Definition: int.h:42
String(const char *str)
Definition: String.h:62
const char * chars(void) const
Definition: String.h:74
int compare(const CString str) const
Definition: String.h:79
bool startsWith(const char *str) const
Definition: String.h:127
int indexOf(char chr, int pos) const
Definition: String.h:100
const char * operator&(void) const
Definition: String.h:89
bool endsWith(const char *str) const
Definition: String.h:137
bool isEmpty(void) const
Definition: String.h:85
char charAt(int index) const
Definition: String.h:91
int indexOf(char chr) const
Definition: String.h:99
Definition: StringBuffer.h:18
int lastIndexOf(const String &str)
Definition: String.h:106
int length(void) const
Definition: String.h:73
char operator[](int index) const
Definition: String.h:92
String rtrim(void) const
Definition: string_String.cpp:521
Definition: String.h:31
friend class CString
Definition: String.h:32
String substring(int _off, int _len) const
Definition: String.h:94
CString toCString(void) const
Definition: String.h:88
int length(void) const
Definition: CString.h:26
const char * chars(void) const
Definition: CString.h:27
int lastIndexOf(char chr) const
Definition: String.h:103