-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
740078e
commit 905df5f
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include "string.h" | ||
|
||
String::String(): | ||
mString(nullptr), | ||
mSize(0), | ||
mCapacity(0) | ||
{}; | ||
String::String(const char* chars,std::size_t capacity): | ||
mString(new char[capacity>strlen(chars)?capacity:strlen(chars)]), | ||
mSize(strlen(chars)), | ||
mCapacity(capacity>strlen(chars)?capacity:strlen(chars)) | ||
{ | ||
std::copy_n(chars,mSize,mString.get()); | ||
}; | ||
String String::operator=(String str) | ||
{ | ||
std::swap(mString,str.mString); | ||
mSize =str.size(); | ||
mCapacity =str.capacity(); | ||
return *this; | ||
} | ||
String& String::operator+(const String& str) | ||
{ | ||
String str2(c_str(),size()+str.size()); | ||
for(std::size_t i=0;i<str.mSize; i++) | ||
{ | ||
str2.push_back(str.mString[i]); | ||
} | ||
*this=str2; | ||
return *this; | ||
} | ||
void String::push_back(const char c) | ||
{ | ||
if (mSize==mCapacity) | ||
{add_capacity();} | ||
mString[mSize] = c; | ||
mSize+=1; | ||
} | ||
void String::add_capacity() | ||
{ | ||
std::unique_ptr<char[]> newString(new char[mCapacity*2 + 1]); | ||
std::copy_n(mString.get(),mSize,newString.get()); | ||
mString=std::move(newString); | ||
} | ||
|
||
std::size_t strlen(const char* chars) | ||
{ | ||
std::size_t counter(0); | ||
while(chars[counter]){++counter;} | ||
return counter; | ||
} | ||
|
||
bool operator==(const std::string& str1,const String& str2) | ||
{ | ||
return str1==std::string(str2); | ||
} | ||
|
||
bool operator==(const char* chars,const String& str) | ||
{ | ||
return String(chars)==str; | ||
}; | ||
|
||
String operator+(const char* chars, const String& str) | ||
{ | ||
return String(chars)+str; | ||
} | ||
|
||
std::ostream& operator<<(std::ostream& os, const String& string) | ||
{ | ||
return os << string.c_str(); | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#pragma once | ||
#include <iostream> | ||
#include <memory> | ||
#include <algorithm> | ||
|
||
std::size_t strlen(const char* chars); | ||
|
||
class String | ||
{ | ||
public: | ||
String(); | ||
|
||
String(const char* chars,std::size_t capacity); | ||
|
||
String(const char* chars):String(chars,strlen(chars)){}; | ||
|
||
String(const String& str):String(str.c_str()){}; | ||
|
||
String operator=(String str); | ||
|
||
const bool empty()const {return mSize==0;} | ||
|
||
char& operator[](int idx)const { return mString[idx];} | ||
|
||
bool operator==(const String& str)const{return std::equal(c_str() , c_str() + mSize , str.c_str());}; | ||
|
||
String& operator+(const String& str); | ||
|
||
operator std::string()const{return c_str();} | ||
|
||
void push_back(const char c); | ||
void add_capacity(); | ||
|
||
std::size_t size()const{return mSize;}; | ||
std::size_t capacity()const{return mCapacity;}; | ||
|
||
const char* c_str()const {return mString.get();} | ||
private: | ||
std::unique_ptr<char[]> mString; | ||
std::size_t mSize; | ||
std::size_t mCapacity; | ||
}; | ||
|
||
|
||
bool operator==(const std::string& str1,const String& str2); | ||
|
||
bool operator==(const char* chars,const String& str); | ||
|
||
String operator+(const char* , const String& str); | ||
|
||
std::ostream& operator<<(std::ostream&, const String&); |