Skip to content

Commit

Permalink
Merge pull request #2 from RandomKiddo/v1.1rc1
Browse files Browse the repository at this point in the history
v1.1r
  • Loading branch information
RandomKiddo authored Oct 27, 2022
2 parents 5e62c1a + 9574d9b commit 0fc581e
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 4 deletions.
59 changes: 57 additions & 2 deletions src/betterstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This file is licensed by the MIT License
Copyright © 2022 RandomKiddo
For more information, visit https://opensource.org/licenses/MIT
Repository link: https://github.com/RandomKiddo/BetterStrings
*/

#include <stdio.h>
Expand All @@ -10,6 +11,7 @@ For more information, visit https://opensource.org/licenses/MIT
#include <cctype>
#include <string.h>
#include <ctype.h>
#include <iostream>

using namespace std;

Expand Down Expand Up @@ -148,8 +150,14 @@ namespace betterstrings {
}
return BetterString(copy);
}
//TODO OVERRIDE OPERATOR << >>
//TODO IMPLEMENT ITERATOR
friend ostream& operator<<(ostream& os, const BetterString& s) { os << s.str; return os; }
friend std::string operator<<(std::string s1, BetterString& s2) { std::string t = s1 + (s2.str); return t; }
friend const char* operator<<(const char* s1, const BetterString& s2) {
std::string s1s = s1;
std::string t = s1s + (s2.str);
return t.c_str();
}
friend istream& operator>>(istream& is, BetterString& in) { is >> in.str; return is; }
//Getters
int size(void) { return length; }
std::string as_str(void) { return str; }
Expand Down Expand Up @@ -235,5 +243,52 @@ namespace betterstrings {
}
return true;
}
//Iterator
struct iterator {
using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = BetterString;
using pointer = BetterString*;
using reference = BetterString&;
private:
char ptr;
int index;
std::string str;
public:
iterator() {}
iterator(BetterString s) {
str = s.as_str();
ptr = s[0];
index = 0;
}
iterator(BetterString s, int i) {
ptr = s[i];
str = s.as_str();
index = i;
}
char operator*() const { return ptr; }
char operator->() { return ptr; }
iterator operator++() {
++index;
ptr = str[index];
return iterator(str, index);
}
iterator operator++(int) {
iterator temp = *this;
++(*this);
return temp;
}
friend bool operator== (const iterator& a, const iterator& b) {
return a.str == b.str && a.ptr == b.ptr && a.index == b.index;
}
friend bool operator!= (const iterator& a, const iterator& b) {
return a.str != b.str || a.ptr != b.ptr || a.index == b.index;
}
};
iterator begin() { return iterator(BetterString(str), 0); }
iterator end() {
BetterString s(str);
return iterator(s, s.size());
}
};
};
47 changes: 45 additions & 2 deletions src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This file is licensed by the MIT License
Copyright © 2022 RandomKiddo
For more information, visit https://opensource.org/licenses/MIT
Repository link: https://github.com/RandomKiddo/BetterStrings
*/

/*
Expand Down Expand Up @@ -47,14 +48,18 @@ void testTitle(void);
void testIndexOne(void);
void testIndexTwo(void);
void testEqIC(void);
void testOut(void);
void testIn(void);
void testIterator(void);

std::vector<bool> vec;
const std::string methods[] = {
"Null Constructor", "String Constructor", "Size Constructor", "Copy Constructor",
"Square Bracket Operator", "Plus/Plus Equal Operator", "Multiply/Multiply Equal Operator",
"Equality", "Inequality", "Subtract/Subtract Equal Operator", "Equal", "Greater/Greater Equal Operator",
"Less/Less Equal Operator", "Splice One Arg", "Splice Two Args", "Splice Three Args", "Size", "As String",
"C String", "Subtract First", "Lower", "Upper", "Title", "Index One Arg", "Index Two Args", "Equals Ignore Case"
"C String", "Subtract First", "Lower", "Upper", "Title", "Index One Arg", "Index Two Args", "Equals Ignore Case",
"Output <<", "Input >>", "Iterator"
};
int main(int argc, char **argv) {
testNullConstructor();
Expand Down Expand Up @@ -83,8 +88,11 @@ int main(int argc, char **argv) {
testIndexOne();
testIndexTwo();
testEqIC();
testOut();
testIn();
testIterator();

const int AMT = 26;
const int AMT = 29;
std::map<int, std::string> failing;

std::string check = "";
Expand Down Expand Up @@ -398,4 +406,39 @@ void testEqIC(void) {
bool longer = one.equalsIgnoreCase(four);
vec.push_back(assert(eq && !neq && !longer, true));
return;
}

//#27
void testOut(void) {
BetterString in("in");
std::string i = "i";
std::string sI = i << in;
/*
std::cout << sI; true
std::cout << "i" << in; true
*/
vec.push_back(assert(sI == "iin" && true && true, true)); //Extra true's for ostream& << and const char* <<
return;
}

//#28
void testIn(void) {
BetterString in;
/*
std::cin >> in; true
*/
vec.push_back(assert(true, true)); // This works
return;
}

//#29
void testIterator(void) {
std::string test = "";
BetterString str("hello");
BetterString::iterator it;
for (it = str.begin(); it != str.end(); ++it) {
test += *it;
}
vec.push_back(assert(strcmp(test.c_str(), str.as_str().c_str()) == 0, true));
return;
}

0 comments on commit 0fc581e

Please sign in to comment.