sources/mystring.h

branch
protocol5
changeset 106
7b156b764d11
parent 103
b78c0ca832a9
parent 105
b4466472aecd
child 108
5900be70c619
equal deleted inserted replaced
104:a76af67a3a4b 106:7b156b764d11
32 #include <deque> 32 #include <deque>
33 #include <string> 33 #include <string>
34 #include <stdarg.h> 34 #include <stdarg.h>
35 #include "basics.h" 35 #include "basics.h"
36 #include "list.h" 36 #include "list.h"
37 BEGIN_ZFC_NAMESPACE
37 38
38 class String; 39 class String;
39 class StringList; 40 class StringList;
40 41
41 // ------------------------------------------------------------------------------------------------- 42 // -------------------------------------------------------------------------------------------------
43 class String 44 class String
44 { 45 {
45 public: 46 public:
46 String() {} 47 String() {}
47 48
48 explicit String (char a) : 49 String (char a)
49 m_string ({ a, '\0' }) {} 50 {
51 char buffer[2] = { a, '0' };
52 m_string = buffer;
53 }
50 54
51 String (const char* data) : 55 String (const char* data) :
52 m_string (data) {} 56 m_string (data) {}
53 57
54 String (const std::string& data) : 58 String (const std::string& data) :
55 m_string (data) {} 59 m_string (data) {}
56 60
57 String (const Vector<char>& data) : 61 String (const Vector<char>& data) :
58 m_string (data.data(), data.size()) {} 62 m_string (data.data(), data.size()) {}
59 63
60 using Iterator = std::string::iterator; 64 typedef std::string::iterator Iterator;
61 using ConstIterator = std::string::const_iterator; 65 typedef std::string::const_iterator ConstIterator;
62 66
63 ConstIterator begin() const { return m_string.cbegin(); } 67 ConstIterator begin() const { return m_string.cbegin(); }
64 int compare (const String &other) const; 68 int compare (const String &other) const;
65 int count (char needle) const; 69 int count (char needle) const;
66 const char* chars() const { return m_string.c_str(); } 70 const char* chars() const { return m_string.c_str(); }
71 int find_last (const char*c, int a) const; 75 int find_last (const char*c, int a) const;
72 int length() const { return m_string.length(); } 76 int length() const { return m_string.length(); }
73 bool mask_against (const String &pattern) const; 77 bool mask_against (const String &pattern) const;
74 String md5() const; 78 String md5() const;
75 String mid (long a, long b) const; 79 String mid (long a, long b) const;
80 String right (int length) const;
76 StringList split (const String &del) const; 81 StringList split (const String &del) const;
77 StringList split (char del) const; 82 StringList split (char del) const;
78 const std::string& std_string() const { return m_string; } 83 const std::string& std_string() const { return m_string; }
79 double to_double (bool* ok = nullptr) const; 84 double to_double (bool* ok = nullptr) const;
80 float to_float (bool* ok = nullptr) const; 85 float to_float (bool* ok = nullptr) const;
92 bool ends_with (const String &other); 97 bool ends_with (const String &other);
93 int index_difference (int a, int b) { modify_index (a); modify_index (b); return b - a; } 98 int index_difference (int a, int b) { modify_index (a); modify_index (b); return b - a; }
94 void insert (int pos, char c) { m_string.insert (m_string.begin() + pos, c); } 99 void insert (int pos, char c) { m_string.insert (m_string.begin() + pos, c); }
95 void insert (int pos, const char*c) { m_string.insert (pos, c); } 100 void insert (int pos, const char*c) { m_string.insert (pos, c); }
96 void modify_index (int &a) { if (a < 0) { a = length() - a; } } 101 void modify_index (int &a) { if (a < 0) { a = length() - a; } }
97 void normalize (int (*filter)(int) = &std::isspace); 102 void normalize (int (*filter)(int) = &isspace);
98 String normalized (int (*filter)(int) = &std::isspace) const; 103 String normalized (int (*filter)(int) = &isspace) const;
99 void prepend (String a) { m_string = (a + m_string).std_string(); } 104 void prepend (String a) { m_string = (a + m_string).std_string(); }
100 void remove (int pos, int len) { m_string.replace (pos, len, ""); } 105 void remove (int pos, int len) { m_string.replace (pos, len, ""); }
101 void remove_at (int pos) { m_string.erase (m_string.begin() + pos); } 106 void remove_at (int pos) { m_string.erase (m_string.begin() + pos); }
102 void remove_from_end (int len) { remove (length() - len, len); } 107 void remove_from_end (int len) { remove (length() - len, len); }
103 void remove_from_start (int len) { remove (0, len); } 108 void remove_from_start (int len) { remove (0, len); }
124 String operator+ (int num) const { return *this + String::from_number (num); } 129 String operator+ (int num) const { return *this + String::from_number (num); }
125 String& operator+= (const String& data) { append (data); return *this; } 130 String& operator+= (const String& data) { append (data); return *this; }
126 String& operator+= (const char* data) { append (data); return *this; } 131 String& operator+= (const char* data) { append (data); return *this; }
127 String& operator+= (int num) { return operator+= (String::from_number (num)); } 132 String& operator+= (int num) { return operator+= (String::from_number (num)); }
128 String& operator+= (char data) { append (data); return *this; } 133 String& operator+= (char data) { append (data); return *this; }
134 char& operator[] (int i) { return m_string[i]; }
135 char operator[] (int i) const { return m_string[i]; }
129 bool operator== (const String& other) const { return std_string() == other.std_string(); } 136 bool operator== (const String& other) const { return std_string() == other.std_string(); }
130 bool operator== (const char* other) const { return m_string == other; } 137 bool operator== (const char* other) const { return m_string == other; }
131 bool operator!= (const String& other) const { return std_string() != other.std_string(); } 138 bool operator!= (const String& other) const { return std_string() != other.std_string(); }
132 bool operator!= (const char* other) const { return m_string != other; } 139 bool operator!= (const char* other) const { return m_string != other; }
133 bool operator> (const String& other) const { return std_string() > other.std_string(); } 140 bool operator> (const String& other) const { return std_string() > other.std_string(); }
144 // ------------------------------------------------------------------------------------------------- 151 // -------------------------------------------------------------------------------------------------
145 // 152 //
146 class StringList : public List<String> 153 class StringList : public List<String>
147 { 154 {
148 public: 155 public:
149 using Super = List<String>; 156 typedef List<String> Super;
150 using Super::Super;
151 157
152 StringList() {} 158 StringList() {}
159
160 StringList (int numvalues) :
161 Super (numvalues) {}
153 162
154 StringList (const Super& other) : 163 StringList (const Super& other) :
155 Super (other) {} 164 Super (other) {}
156 165
157 String join (const String& delim); 166 String join (const String& delim);
168 // 177 //
169 inline String operator+ (const char* a, const String& b) 178 inline String operator+ (const char* a, const String& b)
170 { 179 {
171 return String (a) + b; 180 return String (a) + b;
172 } 181 }
182
183 END_ZFC_NAMESPACE

mercurial