String library included and extended
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
#include "./string.h"
|
||||
|
||||
namespace c0ding {
|
||||
string::string() {
|
||||
data = std::string();
|
||||
}
|
||||
|
||||
string::string(string& str) {
|
||||
data = std::string(str.get());
|
||||
}
|
||||
|
||||
string::string(const std::string& str) {
|
||||
data = std::string(str);
|
||||
}
|
||||
|
||||
string::string(const c0ding::string& str, std::size_t pos, std::size_t len) {
|
||||
data = std::string(str.get(), pos, len);
|
||||
}
|
||||
|
||||
string::string(const std::string& str, std::size_t pos, std::size_t len) {
|
||||
data = std::string(str, pos, len);
|
||||
}
|
||||
|
||||
string::string(const char* s) {
|
||||
data = std::string(s);
|
||||
}
|
||||
|
||||
string::string(const char* s, std::size_t n) {
|
||||
data = std::string(s, n);
|
||||
}
|
||||
|
||||
string::string(std::size_t n, char c) {
|
||||
data = std::string(n, c);
|
||||
}
|
||||
|
||||
template<class InputIterator>
|
||||
string::string(InputIterator first, InputIterator last) {
|
||||
data = std::string(first, last);
|
||||
}
|
||||
|
||||
string::string(std::initializer_list<char> il) {
|
||||
data = std::string(il);
|
||||
}
|
||||
|
||||
string::string(std::string&& str) noexcept {
|
||||
data = std::string(str);
|
||||
}
|
||||
|
||||
string::operator std::string() {
|
||||
return data;
|
||||
}
|
||||
|
||||
string::operator std::string() const {
|
||||
return data;
|
||||
}
|
||||
|
||||
string& string::operator=(const string& str) {
|
||||
if (*this != str) {
|
||||
data = str.get();
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
string& string::operator=(const std::string& str) {
|
||||
if (*this != str) {
|
||||
data = str;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
string& string::operator=(const char* str) {
|
||||
if (*this != str) {
|
||||
data = str;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
string& string::operator+=(const string& str) {
|
||||
data += str.get();
|
||||
return *this;
|
||||
}
|
||||
|
||||
string& string::operator+=(const std::string& str) {
|
||||
data += str;
|
||||
return *this;
|
||||
}
|
||||
|
||||
string& string::operator+=(const char* str) {
|
||||
data += str;
|
||||
return *this;
|
||||
}
|
||||
|
||||
string& string::operator+(const string& str) {
|
||||
*this += str;
|
||||
return *this;
|
||||
}
|
||||
|
||||
string& string::operator+(const std::string& str) {
|
||||
*this += str;
|
||||
return *this;
|
||||
}
|
||||
|
||||
string& string::operator+(const char* str) {
|
||||
*this += str;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool string::operator==(const string& str) {
|
||||
return this->get() == str.get();
|
||||
}
|
||||
|
||||
bool string::operator==(const std::string& str) {
|
||||
return this->get() == str;
|
||||
}
|
||||
|
||||
bool string::operator==(const char* str) {
|
||||
return this->get() == str;
|
||||
}
|
||||
|
||||
bool string::operator!=(const string& str) {
|
||||
return this->get() != str.get();
|
||||
}
|
||||
|
||||
bool string::operator!=(const std::string& str) {
|
||||
return this->get() != str;
|
||||
}
|
||||
|
||||
bool string::operator!=(const char* str) {
|
||||
return this->get() != str;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const string& str) {
|
||||
os << str.get();
|
||||
return os;
|
||||
}
|
||||
|
||||
void string::erase_line(const std::string& sequence) {
|
||||
std::size_t pos;
|
||||
if ((pos = data.find(sequence)) != std::string::npos) {
|
||||
std::size_t start = data.rfind('\n', pos);
|
||||
std::size_t end = data.find('\n', pos);
|
||||
std::size_t size = (end != std::string::npos) ? end - start : data.length() - start;
|
||||
data.erase(start, size);
|
||||
}
|
||||
}
|
||||
|
||||
std::string string::get() {
|
||||
return data;
|
||||
}
|
||||
|
||||
std::string string::get() const {
|
||||
return data;
|
||||
}
|
||||
|
||||
std::string string::get_from(std::string delim) {
|
||||
std::string::size_type found;
|
||||
found = data.find(delim);
|
||||
if (found != std::string::npos) {
|
||||
data.replace(0, (found + delim.length()), "");
|
||||
return data;
|
||||
}
|
||||
return delim;
|
||||
}
|
||||
|
||||
std::string string::get_until(std::string delim) {
|
||||
std::string::size_type found;
|
||||
found = data.find(delim);
|
||||
if (found != std::string::npos) {
|
||||
data.replace(found, data.length(), "");
|
||||
return data;
|
||||
}
|
||||
return delim;
|
||||
}
|
||||
|
||||
void string::replace(const std::string& target, const std::string& replacement) {
|
||||
std::string::size_type found = 0;
|
||||
while ((found = data.find(target, found + replacement.length())) != std::string::npos) {
|
||||
data.replace(found, target.length(), replacement);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> string::split(const std::string& delim) {
|
||||
std::vector<std::string> parts;
|
||||
if (!data.length() || data.length() <= 0) { // string is not valid
|
||||
return parts;
|
||||
}
|
||||
std::string::size_type start = data.find_first_not_of(delim), end;
|
||||
if (start == std::string::npos) { // string contains no delim
|
||||
parts.push_back(data);
|
||||
return parts;
|
||||
}
|
||||
|
||||
while ((end = data.find(delim, start)) != std::string::npos) {
|
||||
std::string part = data.substr(start, end - start);
|
||||
if (part.length() > 0) {
|
||||
parts.push_back(part);
|
||||
}
|
||||
start = end + delim.length();
|
||||
}
|
||||
|
||||
std::string last = data.substr(start, end - start);
|
||||
if (last.length() > 0) {
|
||||
parts.push_back(last);
|
||||
}
|
||||
|
||||
return parts;
|
||||
}
|
||||
|
||||
void string::to_lower() {
|
||||
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
|
||||
}
|
||||
|
||||
void string::to_upper() {
|
||||
std::transform(data.begin(), data.end(), data.begin(), ::toupper);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,325 @@
|
||||
#ifndef C0DING_STRING_H
|
||||
#define C0DING_STRING_H
|
||||
|
||||
#include <string>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <streambuf>
|
||||
|
||||
namespace c0ding {
|
||||
class string {
|
||||
public:
|
||||
/**
|
||||
* Default constructor.
|
||||
*
|
||||
* @return The string object.
|
||||
*/
|
||||
string();
|
||||
|
||||
/**
|
||||
* Copy constructor for c0ding::string.
|
||||
*
|
||||
* @param str The c0ding::string.
|
||||
* @return The string object.
|
||||
*/
|
||||
string(string& str);
|
||||
|
||||
/**
|
||||
* Copy constructor for std::string.
|
||||
*
|
||||
* @param str The std::string.
|
||||
* @return The string object.
|
||||
*/
|
||||
explicit string(const std::string& str);
|
||||
|
||||
/**
|
||||
* Substring constructor for c0ding::string.
|
||||
*
|
||||
* @param str The c0ding::string.
|
||||
* @param pos The position of the first character to get the substring of.
|
||||
* @param len The length of the substring, if not given, takes the string until the end.
|
||||
* @return The string object.
|
||||
*/
|
||||
string(const c0ding::string& str, std::size_t pos, std::size_t len = std::string::npos);
|
||||
|
||||
/**
|
||||
* Substring constructor for std::string.
|
||||
*
|
||||
* @param str The std::string.
|
||||
* @param pos The position of the first character to get the substring of.
|
||||
* @param len The length of the substring, if not given, takes the string until the end.
|
||||
* @return The string object.
|
||||
*/
|
||||
string(const std::string& str, std::size_t pos, std::size_t len = std::string::npos);
|
||||
|
||||
/**
|
||||
* C-string constructor.
|
||||
*
|
||||
* @param s The c-string value.
|
||||
* @return The string object.
|
||||
*/
|
||||
explicit string(const char* s);
|
||||
|
||||
/**
|
||||
* Buffer constructor.
|
||||
*
|
||||
* @param s The c-string value.
|
||||
* @param n The length of the substring.
|
||||
* @return The string object.
|
||||
*/
|
||||
string(const char* s, std::size_t n);
|
||||
|
||||
/**
|
||||
* Fill constructor.
|
||||
*
|
||||
* @param n The character count.
|
||||
* @param c The character to fill into the string.
|
||||
* @return The string object.
|
||||
*/
|
||||
string(size_t n, char c);
|
||||
|
||||
/**
|
||||
* Range constructor.
|
||||
*
|
||||
* @tparam InputIterator The parameter class.
|
||||
* @param first The iterator of the start of the range to copy into the string (inclusive).
|
||||
* @param last The iterator of the end of the range to copy into the string (exclusive).
|
||||
*/
|
||||
template<class InputIterator>
|
||||
string(InputIterator first, InputIterator last);
|
||||
|
||||
/**
|
||||
* Initializer list constructor.
|
||||
*
|
||||
* @param il The initializer list of which each of the characters is copied into the string, maintaining the order.
|
||||
*/
|
||||
string(std::initializer_list<char> il);
|
||||
|
||||
/**
|
||||
* Move constructor.
|
||||
*
|
||||
* @param str The string of which the content is aquired and which is left in an unspecified but valid state.
|
||||
*/
|
||||
explicit string(std::string&& str) noexcept;
|
||||
|
||||
/**
|
||||
* Implicit conversion into std::string.
|
||||
*
|
||||
* @return The std::string.
|
||||
*/
|
||||
explicit operator std::string();
|
||||
|
||||
/**
|
||||
* Implicit conversion into std::string.
|
||||
*
|
||||
* @return The std::string.
|
||||
*/
|
||||
explicit operator std::string() const;
|
||||
|
||||
/**
|
||||
* Right hand side overloading for assignment operator for c0ding::string.
|
||||
*
|
||||
* @param str The std::string to assign to this.
|
||||
* @return The string object.
|
||||
*/
|
||||
string& operator=(const string& str);
|
||||
|
||||
/**
|
||||
* Overloading for assignment operator for std::string.
|
||||
*
|
||||
* @param str The std::string to assign to this.
|
||||
* @return The string object.
|
||||
*/
|
||||
string& operator=(const std::string& str);
|
||||
|
||||
/**
|
||||
* Overloading for assignment operator for c-string.
|
||||
*
|
||||
* @param str The char[] to assign to this.
|
||||
* @return The string object.
|
||||
*/
|
||||
string& operator=(const char* str);
|
||||
|
||||
/**
|
||||
* Overloading for assignment operator for c0ding::string.
|
||||
*
|
||||
* @param str The std::string to assign to this.
|
||||
* @return The string object.
|
||||
*/
|
||||
string& operator+=(const string& str);
|
||||
|
||||
/**
|
||||
* Overloading for assignment operator for std::string.
|
||||
*
|
||||
* @param str The std::string to assign to this.
|
||||
* @return The string object.
|
||||
*/
|
||||
string& operator+=(const std::string& str);
|
||||
|
||||
/**
|
||||
* Overloading for assignment operator for c-string.
|
||||
*
|
||||
* @param str The std::string to assign to this.
|
||||
* @return The string object.
|
||||
*/
|
||||
string& operator+=(const char* str);
|
||||
|
||||
/**
|
||||
* Plus operator overloading for c0ding::string.
|
||||
*
|
||||
* @param str The c0ding::string to add.
|
||||
* @return The string object.
|
||||
*/
|
||||
string& operator+(const string& str);
|
||||
|
||||
/**
|
||||
* Plus operator overloading for std::string.
|
||||
*
|
||||
* @param str The std::string to add.
|
||||
* @return The string object.
|
||||
*/
|
||||
string& operator+(const std::string& str);
|
||||
|
||||
/**
|
||||
* Plus operator overloading for c-string.
|
||||
*
|
||||
* @param str The std::string to add.
|
||||
* @return The string object.
|
||||
*/
|
||||
string& operator+(const char* str);
|
||||
|
||||
/**
|
||||
* Overloading for comparison operator (equality) for c0ding::string.
|
||||
*
|
||||
* @param str The c0ding::string to compare with.
|
||||
* @return Equality state.
|
||||
*/
|
||||
bool operator==(const string& str);
|
||||
|
||||
/**
|
||||
* Overloading for comparison operator (equality) for std::string.
|
||||
*
|
||||
* @param str The std::string to compare with.
|
||||
* @return Equality state.
|
||||
*/
|
||||
bool operator==(const std::string& str);
|
||||
|
||||
/**
|
||||
* Overloading for comparison operator (equality) for c-string.
|
||||
*
|
||||
* @param str The char[] to compare with.
|
||||
* @return Equality state.
|
||||
*/
|
||||
bool operator==(const char* str);
|
||||
|
||||
/**
|
||||
* Overloading for comparison operator (inequality) for c0ding::string.
|
||||
*
|
||||
* @param str The c0ding::string to compare with.
|
||||
* @return Inequality state.
|
||||
*/
|
||||
bool operator!=(const string& str);
|
||||
|
||||
/**
|
||||
* Overloading for comparison operator (inequality) for std::string.
|
||||
*
|
||||
* @param str The std::string to compare with.
|
||||
* @return Inequality state.
|
||||
*/
|
||||
bool operator!=(const std::string& str);
|
||||
|
||||
/**
|
||||
* Overloading for comparison operator (inequality) for c-string.
|
||||
*
|
||||
* @param str The char[] to compare with.
|
||||
* @return Inequality state.
|
||||
*/
|
||||
bool operator!=(const char* str);
|
||||
|
||||
/**
|
||||
* Shift operator overloading for ostreams.
|
||||
*
|
||||
* @param ostream The ostream to put the string into.
|
||||
* @param str The string to put into the ostream.
|
||||
* @return The ostream.
|
||||
*/
|
||||
friend std::ostream& operator<<(std::ostream& os, const string& str);
|
||||
|
||||
/**
|
||||
* Tries to erase the first line of the given string which contains the sequence.
|
||||
*
|
||||
* @param sequence The sequence.
|
||||
*/
|
||||
void erase_line(const std::string& sequence);
|
||||
|
||||
/**
|
||||
* The the std::string value of the string.
|
||||
*
|
||||
* @return The std::string value.
|
||||
*/
|
||||
std::string get();
|
||||
|
||||
/**
|
||||
* The the const std::string value of the string.
|
||||
*
|
||||
* @return The const std::string value.
|
||||
*/
|
||||
[[nodiscard]] std::string get() const;
|
||||
|
||||
/**
|
||||
* Get a part of a given string after a delimiter.
|
||||
*
|
||||
* @param string The given string.
|
||||
* @param delim The delimiter.
|
||||
* @return The partial string.
|
||||
*/
|
||||
std::string get_from(std::string delim);
|
||||
|
||||
/**
|
||||
* Get a part of a given string before a delimiter.
|
||||
*
|
||||
* @param string The given string.
|
||||
* @param delim The delimiter.
|
||||
* @return The partial string.
|
||||
*/
|
||||
std::string get_until(std::string delim);
|
||||
|
||||
/**
|
||||
* Replace all targets found in a given string by a replacement.
|
||||
*
|
||||
* @param string The given string.
|
||||
* @param target The target.
|
||||
* @param replacement The replacement.
|
||||
*/
|
||||
void replace(const std::string& target, const std::string& replacement);
|
||||
|
||||
/**
|
||||
* Split a given string at a delimiter.
|
||||
*
|
||||
* @param string The given string.
|
||||
* @param delim The delimiter.
|
||||
* @return The split parts.
|
||||
*/
|
||||
std::vector<std::string> split(const std::string& delim);
|
||||
|
||||
/**
|
||||
* Transform all characters in a given string to lowercase.
|
||||
*
|
||||
* @param string The given string.
|
||||
*/
|
||||
void to_lower();
|
||||
|
||||
/**
|
||||
* Transform all characters in a given string to uppercase.
|
||||
*
|
||||
* @param string The given string.
|
||||
*/
|
||||
void to_upper();
|
||||
|
||||
private:
|
||||
std::string data;
|
||||
};
|
||||
}
|
||||
|
||||
#endif //C0DING_STRING_H
|
||||
Reference in New Issue
Block a user