/
thomas-king
/
Literals
Обзор
Документация
Войти
/
thomas-king
/
Literals
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
include/lib/String.h
130 строк
2 KB
AceRodstin
Update libraries.
30 дек 2022, 16:14
30 дек 2022, 16:14
4e4ae27
Код
Авторство
О чём код?
// // String.h // Libraries // // Created by Ace Rodstin on 15 Nov 2022. // #ifndef STRING_HEADER_FILE #define STRING_HEADER_FILE #include <string> #include <vector> #include <sstream> #include <optional> #include "Range.h" #include "utils.h" using namespace std; class String : public string { public: using string::string; using SizeType = string::size_type; String(string wrapped) : string(wrapped) {}; String operator[](Range<SizeType> range) const { return substr(range.start, range.end - range.start); } optional<char> first() { if (empty()) { return {}; } else { return *begin(); } } optional<char> last() { if (empty()) { return {}; } else { return *rbegin(); } } void drop_first(size_type count = 1) { auto iterator = begin(); for (size_type i = 0; i < count; ++i) { erase(iterator); ++iterator; } } void drop_last(size_type count = 1) { auto iterator = end() - 1; for (size_type i = 0; i < count; ++i) { erase(iterator); --iterator; } } template<class Predicate> String trimmed(Predicate predicate) { auto temp = *this; temp.trim(predicate); return temp; } template<class Predicate> void trim(Predicate predicate) { auto leading = begin(); while (predicate(*leading)) { leading++; } erase(begin(), leading); auto trailing = end() - 1; while (predicate(*trailing)) { trailing--; } erase(trailing + 1, end()); } template<class Predicate> vector<string> split(Predicate predicate) { stringstream input_stream { *this }; stringstream component_stream; vector<string> result; while (input_stream.good()) { char character = input_stream.get(); if (predicate(character)) { string component = component_stream.str(); if (!component.empty()) { result.push_back(component); component_stream.str(""); } continue; } else { component_stream << character; } input_stream.peek(); if (input_stream.eof()) { string component = component_stream.str(); if (!component.empty()) { result.push_back(component); } } } return result; } }; #endif