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(); } |