uni

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

commit 50eb197c50071d2815d50e774abf529c4f5c2990
parent 59d2fc43637a8a74ed0242673d351a62a9c4606a
Author: Christos Margiolis <christos@margiolis.net>
Date:   Wed,  6 May 2020 16:51:53 +0300

fixed xstring bug

Diffstat:
Massignment-2.5-spreadsheets/bin/spreadsheets | 0
Massignment-2.5-spreadsheets/obj/course.o | 0
Massignment-2.5-spreadsheets/obj/grades.o | 0
Massignment-2.5-spreadsheets/obj/xstring.o | 0
Massignment-2.5-spreadsheets/src/course.cpp | 10++++++++++
Massignment-2.5-spreadsheets/src/course.h | 3+++
Massignment-2.5-spreadsheets/src/xstring.cpp | 31+++++++++++++++++++++++++++++--
Massignment-2.5-spreadsheets/src/xstring.h | 4+---
8 files changed, 43 insertions(+), 5 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/course.o b/assignment-2.5-spreadsheets/obj/course.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/grades.o b/assignment-2.5-spreadsheets/obj/grades.o 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/course.cpp b/assignment-2.5-spreadsheets/src/course.cpp @@ -2,3 +2,13 @@ Course::Course(const lab::xstring& code, const lab::xstring& name) :code(code), name(name) {} + +const lab::xstring& Course::get_code() const +{ + return code; +} + +const lab::xstring& Course::get_name() const +{ + return name; +} diff --git a/assignment-2.5-spreadsheets/src/course.h b/assignment-2.5-spreadsheets/src/course.h @@ -11,6 +11,9 @@ class Course public: Course(const lab::xstring& code, const lab::xstring& name); + + const lab::xstring& get_code() const; + const lab::xstring& get_name() const; }; #endif /* COURSE_H */ diff --git a/assignment-2.5-spreadsheets/src/xstring.cpp b/assignment-2.5-spreadsheets/src/xstring.cpp @@ -183,7 +183,20 @@ xstring& xstring::append(const xstring& s) xstring& xstring::append(const xstring& s, std::size_t i) { - + if (!s.empty() && i < len) + { + char *tmp1 = new char[i + 1]; + char *tmp2 = new char[len - i + 1]; + std::copy(str, str + i + 1, tmp1); + std::copy(str + i, str + len + 1, tmp2); + resize(s.len); + 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(); + str[len] = '\0'; + } + return *this; } xstring& xstring::append(const char *s) @@ -194,7 +207,20 @@ xstring& xstring::append(const char *s) xstring& xstring::append(const char *s, std::size_t i) { - + if (!strempty(s) && i < len) + { + char *tmp1 = new char[i + 1]; + char *tmp2 = new char[len - i + 1]; + std::copy(str, str + i + 1, tmp1); + std::copy(str + i, str + len + 1, tmp2); + resize(strlen(s)); + 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(); + str[len] = '\0'; + } + return *this; } xstring& xstring::append(char c) @@ -211,6 +237,7 @@ char *xstring::cstr() const char& xstring::last() const { if (!this->empty()) return str[len-1]; + else return str[0]; } std::size_t xstring::size() diff --git a/assignment-2.5-spreadsheets/src/xstring.h b/assignment-2.5-spreadsheets/src/xstring.h @@ -6,7 +6,6 @@ #include <iostream> #include <string.h> -// add more '\0' checks namespace lab { class xstring { @@ -39,7 +38,7 @@ class xstring bool operator> (const char *s) const; bool operator>= (const xstring& s) const; bool operator>= (const char *s) const; - char& operator[] (std::size_t i) const; + char& operator[] (std::size_t i) const; friend std::ostream& operator<< (std::ostream& stream, const xstring& s); friend std::istream& operator>> (std::istream& stream, const xstring& s); @@ -64,7 +63,6 @@ class xstring }; std::istream& getline(std::istream& stream, xstring& s, char delim = '\n'); - template<typename T> xstring to_xstr(T val); template<typename T> xstring to_xstr(const char *fs, T val); template<typename T> const char *getformat();