uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

commit cc930be2d7d4f8c9a0cb2b9fac17155f1de620cc
parent 50eb197c50071d2815d50e774abf529c4f5c2990
Author: Christos Margiolis <christos@margiolis.net>
Date:   Thu,  7 May 2020 07:42:25 +0300

added new methods to xstring

Diffstat:
Massignment-2.5-spreadsheets/bin/spreadsheets | 0
Massignment-2.5-spreadsheets/obj/xstring.o | 0
Massignment-2.5-spreadsheets/src/xstring.cpp | 111++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Massignment-2.5-spreadsheets/src/xstring.h | 24++++++++++++++++++++++--
4 files changed, 89 insertions(+), 46 deletions(-)

diff --git a/assignment-2.5-spreadsheets/bin/spreadsheets b/assignment-2.5-spreadsheets/bin/spreadsheets Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/xstring.o b/assignment-2.5-spreadsheets/obj/xstring.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/src/xstring.cpp b/assignment-2.5-spreadsheets/src/xstring.cpp @@ -47,7 +47,7 @@ xstring xstring::operator= (const char *s) { if (!empty()) delete[] str; str = conv(s); - len = size(); + len = length(); return *this; } @@ -63,38 +63,19 @@ xstring xstring::operator+ (const char *s) xstring& xstring::operator+= (const xstring& s) { - if (!this->empty()) - { - resize(s.str); - strcat(str, s.str); - } - else str = conv(s.str); - len = size(); + append(s.str); return *this; } xstring& xstring::operator+= (const char *s) { - if (!this->empty()) - { - resize(s); - strcat(str, s); - } - else str = conv(s); - len = size(); + append(s); return *this; } xstring& xstring::operator+= (char c) { - if (!this->empty()) - { - resize(1); - str[len] = c; - str[len+1] = '\0'; - } - else str = conv(c); - len = size(); + push_back(c); return *this; } @@ -177,7 +158,13 @@ std::istream& operator>> (std::istream& stream, const xstring& s) xstring& xstring::append(const xstring& s) { - if (!s.empty()) *this += s; + if (!s.empty()) + { + resize(s.str); + strcat(str, s.str); + } + else str = conv(s.str); + len = length(); return *this; } @@ -193,7 +180,7 @@ xstring& xstring::append(const xstring& s, std::size_t i) std::copy(tmp1, tmp1 + i + 1, str); std::copy(s.str, s.str + s.len + 1, str + i); std::copy(tmp2, tmp2 + len - i + 1, str + s.len + i); - len = size(); + len = length(); str[len] = '\0'; } return *this; @@ -201,7 +188,13 @@ xstring& xstring::append(const xstring& s, std::size_t i) xstring& xstring::append(const char *s) { - if (!strempty(s)) *this += s; + if (!strempty(s)) + { + resize(s); + strcat(str, s); + } + else str = conv(s); + len = length(); return *this; } @@ -217,7 +210,7 @@ xstring& xstring::append(const char *s, std::size_t i) std::copy(tmp1, tmp1 + i + 1, str); std::copy(s, s + strlen(s) + 1, str + i); std::copy(tmp2, tmp2 + len - i + 1, str + strlen(s) + i); - len = size(); + len = length(); str[len] = '\0'; } return *this; @@ -225,22 +218,64 @@ xstring& xstring::append(const char *s, std::size_t i) xstring& xstring::append(char c) { - *this += c; + push_back(c); return *this; } +void xstring::push_back(char c) +{ + if (!this->empty()) + { + resize(1); + str[len] = c; + str[len+1] = '\0'; + } + else str = conv(c); + len = length(); +} + +void xstring::pop_back() +{ + if (len - 1 > 0) + { + char *tmp = new char[len]; + std::copy(str, str + len, tmp); + delete[] str; + tmp[len - 1] = '\0'; + str = tmp; + len--; + } + else if (len - 1 == 0) + { + delete[] str; + str = new char[1]; + str[0] = '\0'; + } + else return; +} + +void xstring::replace(std::size_t i, char c) +{ + if (i < len) str[i] = c; +} + char *xstring::cstr() const { return str; } -char& xstring::last() const +char& xstring::front() const +{ + return str[0]; +} + +char& xstring::back() const { if (!this->empty()) return str[len-1]; else return str[0]; } -std::size_t xstring::size() +std::size_t xstring::length() { len = strlen(str); return len; @@ -289,7 +324,7 @@ void xstring::resize(const char *s) std::copy(str, str + len+1, tmp); delete[] str; str = tmp; - len = size(); + len = length(); } } @@ -302,7 +337,7 @@ void xstring::resize(std::size_t n) std::copy(str, str + len + 1, tmp); delete[] str; str = tmp; - len = size(); + len = length(); } } @@ -317,16 +352,4 @@ std::istream& getline(std::istream& stream, xstring& s, char delim) } return stream; } - -template<> const char *getformat<short>() {return "%hi";} -template<> const char *getformat<int>() {return "%d";} -template<> const char *getformat<long>() {return "%ld";} -template<> const char *getformat<long long>() {return "%lld";} -template<> const char *getformat<unsigned short>() {return "%hu";} -template<> const char *getformat<unsigned int>() {return "%u";} -template<> const char *getformat<unsigned long>() {return "%lu";} -template<> const char *getformat<unsigned long long>() {return "%llu";} -template<> const char *getformat<float>() {return "%f";} -template<> const char *getformat<double>() {return "%f";} -template<> const char *getformat<long double>() {return "%Lf";} } diff --git a/assignment-2.5-spreadsheets/src/xstring.h b/assignment-2.5-spreadsheets/src/xstring.h @@ -48,9 +48,13 @@ class xstring xstring& append(const char *s, std::size_t i); xstring& append(char c); + void push_back(char c); + void pop_back(); + void replace(std::size_t i, char c); char *cstr() const; - char& last() const; - std::size_t size(); + char& front() const; + char& back() const; + std::size_t length(); bool empty() const; void clear(); @@ -82,6 +86,22 @@ xstring to_xstr(const char *fs, T val) snprintf(buf, sizeof(buf), fs, val); return xstring(buf); } + +template<typename T> +const char *getformat() +{ + if (std::is_same<T, short>::value) return "%hi"; + if (std::is_same<T, int>::value) return "%d"; + if (std::is_same<T, long>::value) return "%ld"; + if (std::is_same<T, long long>::value) return "%lld"; + if (std::is_same<T, unsigned short>::value) return "%hu"; + if (std::is_same<T, unsigned int>::value) return "%u"; + if (std::is_same<T, unsigned long>::value) return "%lu"; + if (std::is_same<T, unsigned long>::value) return "%llu"; + if (std::is_same<T, float>::value) return "%f"; + if (std::is_same<T, double>::value) return "%f"; + if (std::is_same<T, long double>::value) return "%Lf"; +} } #endif /* XSTRING_H */