sources/mystring.h

changeset 88
08ccaf26cffd
parent 87
53c2aecb9704
child 105
b4466472aecd
equal deleted inserted replaced
87:53c2aecb9704 88:08ccaf26cffd
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(); }
104 void replace (int pos, int n, const String &a) { m_string.replace (pos, n, a.chars()); } 108 void replace (int pos, int n, const String &a) { m_string.replace (pos, n, a.chars()); }
105 void shrink_to_fit() { m_string.shrink_to_fit(); } 109 void shrink_to_fit() { m_string.shrink_to_fit(); }
106 void __cdecl sprintf (const char* fmtstr, ...); 110 void __cdecl sprintf (const char* fmtstr, ...);
107 void vsprintf (const char* fmtstr, va_list args); 111 void vsprintf (const char* fmtstr, va_list args);
108 bool starts_with (const String &other); 112 bool starts_with (const String &other);
109 String strip (char unwanted) { return strip ({unwanted}); } 113 String strip (const List<char>& unwanted);
110 String strip (const List<char> &unwanted);
111 void trim (int n); 114 void trim (int n);
112 115
113 static String from_number (short int a); 116 static String from_number (short int a);
114 static String from_number (int a); 117 static String from_number (int a);
115 static String from_number (long int a); 118 static String from_number (long int a);
123 String operator+ (int num) const { return *this + String::from_number (num); } 126 String operator+ (int num) const { return *this + String::from_number (num); }
124 String& operator+= (const String& data) { append (data); return *this; } 127 String& operator+= (const String& data) { append (data); return *this; }
125 String& operator+= (const char* data) { append (data); return *this; } 128 String& operator+= (const char* data) { append (data); return *this; }
126 String& operator+= (int num) { return operator+= (String::from_number (num)); } 129 String& operator+= (int num) { return operator+= (String::from_number (num)); }
127 String& operator+= (char data) { append (data); return *this; } 130 String& operator+= (char data) { append (data); return *this; }
131 char& operator[] (int i) { return m_string[i]; }
132 char operator[] (int i) const { return m_string[i]; }
128 bool operator== (const String& other) const { return std_string() == other.std_string(); } 133 bool operator== (const String& other) const { return std_string() == other.std_string(); }
129 bool operator== (const char* other) const { return m_string == other; } 134 bool operator== (const char* other) const { return m_string == other; }
130 bool operator!= (const String& other) const { return std_string() != other.std_string(); } 135 bool operator!= (const String& other) const { return std_string() != other.std_string(); }
131 bool operator!= (const char* other) const { return m_string != other; } 136 bool operator!= (const char* other) const { return m_string != other; }
132 bool operator> (const String& other) const { return std_string() > other.std_string(); } 137 bool operator> (const String& other) const { return std_string() > other.std_string(); }
169 // 174 //
170 inline String operator+ (const char* a, const String& b) 175 inline String operator+ (const char* a, const String& b)
171 { 176 {
172 return String (a) + b; 177 return String (a) + b;
173 } 178 }
179
180 END_ZFC_NAMESPACE

mercurial