Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
string.h
1 /*
2  * $Id$
3  * Copyright (c) 2004-07, IRIT - UPS
4  *
5  * string module interface
6  */
7 #ifndef ELM_STRING_H
8 #define ELM_STRING_H
9 
10 #include <string.h>
11 #include <elm/string/Char.h>
12 #include <elm/string/CString.h>
13 #include <elm/string/String.h>
14 #include <elm/string/StringBuffer.h>
15 #include <elm/string/Split.h>
16 
17 namespace elm {
18 
19 // Back C Strings
20 inline CString CString::substring(int off) const { return CString(buf + off); };
21 inline String CString::substring(int off, int len) const { return String(buf + off, len); };
22 inline String CString::concat(const CString str) const { return String::concat(chars(), length(), str.chars(), str.length()); };
23 inline String CString::concat(const String& str) const { return String::concat(chars(), length(), str.chars(), str.length()); };
24 inline bool CString::startsWith(const char *str) const { return startsWith(CString(str)); };
25 inline bool CString::startsWith(const CString str) const { return !strncmp(buf, str.chars(), str.length()); };
26 inline bool CString::startsWith(const String& str) const { return !strncmp(buf, str.chars(), str.length()); };
27 inline bool CString::endsWith(const char *str) const
28  { return endsWith(CString(str)); };
29 inline bool CString::endsWith(const CString str) const
30  { int l1 = length(), l2 = str.length(); return l1 >= l2 && !memcmp(buf + l1 - l2, str.chars(), l2); };
31 inline bool CString::endsWith(const String& str) const
32  { int l1 = length(), l2 = str.length(); return l1 >= l2 && !memcmp(buf + l1 - l2, str.chars(), l2); };
33 
34 // Comparison inlines
35 inline bool operator==(const CString& s1, const CString& s2) { return s1.compare(s2) == 0; };
36 inline bool operator!=(const CString& s1, const CString& s2) { return s1.compare(s2) != 0; };
37 inline bool operator<(const CString s1, const CString s2) { return s1.compare(s2) < 0; };
38 inline bool operator<=(const CString s1, const CString s2) { return s1.compare(s2) <= 0; };
39 inline bool operator>(const CString s1, const CString s2) { return s1.compare(s2) > 0; };
40 inline bool operator>=(const CString s1, const CString s2) { return s1.compare(s2) >= 0; };
41 
42 inline bool operator==(const String& s1, const CString s2) { return s1.compare(s2) == 0; };
43 inline bool operator!=(const String& s1, const CString s2) { return s1.compare(s2) != 0; };
44 inline bool operator<(const String& s1, const CString s2) { return s1.compare(s2) < 0; };
45 inline bool operator<=(const String& s1, const CString s2) { return s1.compare(s2) <= 0; };
46 inline bool operator>(const String& s1, const CString s2) { return s1.compare(s2) > 0; };
47 inline bool operator>=(const String& s1, const CString s2) { return s1.compare(s2) >= 0; };
48 
49 inline bool operator==(const String& s1, const String& s2) { return s1.compare(s2) == 0; };
50 inline bool operator!=(const String& s1, const String& s2) { return s1.compare(s2) != 0; };
51 inline bool operator<(const String& s1, const String& s2) { return s1.compare(s2) < 0; };
52 inline bool operator<=(const String& s1, const String& s2) { return s1.compare(s2) <= 0; };
53 inline bool operator>(const String& s1, const String& s2) { return s1.compare(s2) > 0; };
54 inline bool operator>=(const String& s1, const String& s2) { return s1.compare(s2) >= 0; };
55 
56 inline bool operator==(const CString s1, const String& s2) { return s2.compare(s1) == 0; };
57 inline bool operator!=(const CString s1, const String& s2) { return s2.compare(s1) != 0; };
58 inline bool operator<(const CString s1, const String& s2) { return s2.compare(s1) >= 0; };
59 inline bool operator<=(const CString s1, const String& s2) { return s2.compare(s1) > 0; };
60 inline bool operator>(const CString s1, const String& s2) { return s2.compare(s1) <= 0; };
61 inline bool operator>=(const CString s1, const String& s2) { return s2.compare(s1) < 0; };
62 
63 inline bool operator==(const String& s1, const char *s2) { return s1.compare(CString(s2)) == 0; };
64 inline bool operator!=(const String& s1, const char *s2) { return s1.compare(CString(s2)) != 0; };
65 inline bool operator<(const String& s1, const char *s2) { return s1.compare(CString(s2)) < 0; };
66 inline bool operator<=(const String& s1, const char *s2) { return s1.compare(CString(s2)) <= 0; };
67 inline bool operator>(const String& s1, const char *s2) { return s1.compare(CString(s2)) > 0; };
68 inline bool operator>=(const String& s1, const char *s2) { return s1.compare(CString(s2)) >= 0; };
69 
70 inline bool operator==(const CString s1, const char *s2) { return s1.compare(CString(s2)) == 0; };
71 inline bool operator!=(const CString s1, const char *s2) { return s1.compare(CString(s2)) != 0; };
72 inline bool operator<(const CString s1, const char *s2) { return s1.compare(CString(s2)) < 0; };
73 inline bool operator<=(const CString s1, const char *s2) { return s1.compare(CString(s2)) <= 0; };
74 inline bool operator>(const CString s1, const char *s2) { return s1.compare(CString(s2)) > 0; };
75 inline bool operator>=(const CString s1, const char *s2) { return s1.compare(CString(s2)) >= 0; };
76 
77 inline bool operator==(const char *s1, const CString s2) { return s2.compare(CString(s1)) == 0; };
78 inline bool operator!=(const char *s1, const CString s2) { return s2.compare(CString(s1)) != 0; };
79 inline bool operator<(const char *s1, const CString s2) { return s2.compare(CString(s1)) >= 0; };
80 inline bool operator<=(const char *s1, const CString s2) { return s2.compare(CString(s1)) > 0; };
81 inline bool operator>(const char *s1, const CString s2) { return s2.compare(CString(s1)) <= 0; };
82 inline bool operator>=(const char *s1, const CString s2) { return s2.compare(CString(s1)) < 0; };
83 
84 inline bool operator==(const char *s1, const String& s2) { return s2.compare(CString(s1)) == 0; };
85 inline bool operator!=(const char *s1, const String& s2) { return s2.compare(CString(s1)) != 0; };
86 inline bool operator<(const char *s1, const String& s2) { return s2.compare(CString(s1)) >= 0; };
87 inline bool operator<=(const char *s1, const String& s2) { return s2.compare(CString(s1)) > 0; };
88 inline bool operator>(const char *s1, const String& s2) { return s2.compare(CString(s1)) <= 0; };
89 inline bool operator>=(const char *s1, const String& s2) { return s2.compare(CString(s1)) < 0; };
90 
91 
92 // Concatenation
93 inline String operator+(const CString& s1, const CString& s2) { return s1.concat(s2); }
94 inline String operator+(const CString& s1, const char *s2) { return s1.concat(CString(s2)); };
95 inline String operator+(const CString s1, const String& s2) { return s1.concat(s2); };
96 inline String operator+(const cstring& s, char c) { char t[2] = { c, '\0' }; return s.concat(cstring(t)); }
97 inline String operator+(const String& s1, const CString s2) { return s1.concat(s2); };
98 inline String operator+(const String& s1, const char *s2) { return s1.concat(CString(s2)); };
99 inline String operator+(const String& s1, const String& s2) { return s1.concat(s2); };
100 inline String operator+(const string& s, char c) { char t[2] = { c, '\0' }; return s.concat(cstring(t)); }
101 inline String operator+(const char *s1, const CString s2) { return CString(s1).concat(s2); };
102 inline String operator+(const char *s1, const String& s2) { return CString(s1).concat(s2); };
103 inline String operator+(char c, const cstring s) { char t[2] = { c, '\0' }; return cstring(t).concat(s); };
104 inline String operator+(char c, const String& s) { char t[2] = { c, '\0' }; return cstring(t).concat(s); };
105 
106 template <class C> String inline String::join(const C& coll) {
107  StringBuffer buf;
108  bool f = true;
109  for(const auto& x: coll) {
110  if(f)
111  f = false;
112  else
113  buf << *this;
114  buf << x;
115  }
116  return buf.toString();
117 }
118 
119 };
120 
121 #include <elm/string/AutoString.h>
122 
123 #endif // ELM_STRING_H
elm::operator==
bool operator==(const CString &s1, const CString &s2)
Definition: string.h:35
elm::String::chars
const char * chars(void) const
Definition: String.h:76
elm::operator<=
bool operator<=(const T &v, const FragTable< T, E, A > &t)
Definition: FragTable.h:158
elm::operator+
String operator+(const CString &s1, const CString &s2)
Definition: string.h:93
elm::StringBuffer
Definition: StringBuffer.h:18
elm::CString::substring
CString substring(int pos) const
Definition: string.h:20
elm::CString::compare
int compare(const CString &str) const
Definition: CString.h:28
elm::CString::buf
const char * buf
Definition: CString.h:19
elm::cstring
CString cstring
Definition: CString.h:62
elm::String::join
String join(const C &coll)
Definition: string.h:106
elm::operator!=
bool operator!=(const CString &s1, const CString &s2)
Definition: string.h:36
elm::CString
Definition: CString.h:17
elm::CString::CString
CString(void)
Definition: CString.h:21
elm::String::length
int length(void) const
Definition: String.h:75
elm::CString::concat
String concat(const CString str) const
Definition: string.h:22
elm::CString::startsWith
bool startsWith(const char *str) const
Definition: string.h:24
elm
Definition: adapter.h:26
elm::CString::chars
const char * chars(void) const
Definition: CString.h:27
elm::operator>
bool operator>(const CString s1, const CString s2)
Definition: string.h:39
elm::String::compare
int compare(const String &str) const
Definition: String.h:77
elm::CString::length
int length(void) const
Definition: CString.h:26
elm::StringBuffer::toString
String toString()
Definition: StringBuffer.h:25
elm::operator<
bool operator<(const CString s1, const CString s2)
Definition: string.h:37
elm::operator>=
bool operator>=(const CString s1, const CString s2)
Definition: string.h:40
elm::String
Definition: String.h:30
elm::str
string str(const char *s)
Definition: String.h:150
elm::CString::endsWith
bool endsWith(const char *str) const
Definition: string.h:27