Skip to content

Commit

Permalink
Review native strings
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Oct 27, 2024
1 parent a8d696e commit e0a4ee7
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 225 deletions.
94 changes: 50 additions & 44 deletions src/xtd.core.native.linux/include/xtd/native/linux/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,24 @@ namespace xtd::native::linux {
class strings final {
public:
strings() = delete;


inline static const std::string empty_string;

static bool contains(const std::string& str, const std::string& value) noexcept {
return str.find(value) != str.npos;
}

static bool ends_with(const std::string& str, const std::string& value) noexcept {
return str.rfind(value) + value.size() == str.size();
}

template<typename separator_t, typename collection_t>
static std::string join(const separator_t& separator, const collection_t& values) noexcept {return join(separator, values, 0, values.size());}

template<typename separator_t, typename collection_t>
static std::string join(const separator_t& separator, const collection_t& values, size_t index, size_t count) noexcept {
size_t i = 0;
std::stringstream ss;
auto i = size_t {};
auto ss = std::stringstream {};
for (const auto& item : values) {
if (i >= index) {
if (i != index) ss << separator;
Expand All @@ -42,46 +44,46 @@ namespace xtd::native::linux {
}
return ss.str();
}

static size_t last_index_of(const std::string& str, char value) noexcept {
return last_index_of(str, value, 0, str.size());
}

static size_t last_index_of(const std::string& str, const std::string& value) noexcept {
return last_index_of(str, value, 0, str.size());
}

static size_t last_index_of(const std::string& str, char value, size_t start_index) noexcept {
return last_index_of(str, value, start_index, str.size() - start_index);
}

static size_t last_index_of(const std::string& str, const std::string& value, size_t start_index) noexcept {
return last_index_of(str, value, start_index, str.size() - start_index);
}

static size_t last_index_of(const std::string& str, char value, size_t start_index, size_t count) noexcept {
size_t result = str.rfind(value, start_index + count - 1);
auto result = str.rfind(value, start_index + count - 1);
return result < start_index ? str.npos : result;
}

static size_t last_index_of(const std::string& str, const std::string& value, size_t start_index, size_t count) noexcept {
size_t result = str.rfind(value, start_index + count - value.size());
auto result = str.rfind(value, start_index + count - value.size());
return result < start_index ? str.npos : result;
}

static std::string remove(const std::string& str, size_t start_index) noexcept {
return remove(str, start_index, str.size() - start_index);
}

static std::string remove(const std::string& str, size_t start_index, size_t count) noexcept {
if (start_index > str.size()) return str;
std::string result(str);
auto result = str;
return result.erase(start_index, count);
}

static std::string replace(const std::string& str, const std::string& old_string, const std::string& new_string) noexcept {
std::string result(str);
size_t index = 0;
auto result = str;
auto index = size_t {};
while (true) {
index = result.find(old_string, index);
if (index == std::string::npos) break;
Expand All @@ -91,50 +93,55 @@ namespace xtd::native::linux {
}
return result;
}

static std::vector<std::string> split(const std::string& str, const std::vector<char>& separators, size_t count = std::numeric_limits<size_t>::max(), bool remove_empty_entries = false) noexcept {
if (count == 0) return {};
if (count == 1) return {str};

std::vector<std::string> list;
std::string subString;
std::vector<char> split_char_separators = separators.size() == 0 ? std::vector<char> {9, 10, 11, 12, 13, 32} : separators;
for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) {
bool is_separator = std::find(split_char_separators.begin(), split_char_separators.end(), *it) != split_char_separators.end();
if (!is_separator) subString.append(std::string(1, *it));
if ((static_cast<size_t>(it - str.begin()) == str.length() - 1 || is_separator) && (subString.length() > 0 || (subString.length() == 0 && !remove_empty_entries))) {
auto list = std::vector<std::string> {};
auto sub_string = std::string {};
auto split_char_separators = separators.size() == 0 ? std::vector<char> {9, 10, 11, 12, 13, 32} : separators;
for (auto it = str.begin(); it != str.end(); ++it) {
auto is_separator = std::find(split_char_separators.begin(), split_char_separators.end(), *it) != split_char_separators.end();
if (!is_separator) sub_string.append(std::string(1, *it));
if ((static_cast<size_t>(it - str.begin()) == str.length() - 1 || is_separator) && (sub_string.length() > 0 || (sub_string.length() == 0 && !remove_empty_entries))) {
if (list.size() == count - 1) {
list.push_back(subString + std::string(str.c_str(), it - str.begin() + (is_separator ? 0 : 1), str.length() - (it - str.begin()) + (is_separator ? 0 : 1)));
list.push_back(sub_string + std::string(str.c_str(), it - str.begin() + (is_separator ? 0 : 1), str.length() - (it - str.begin()) + (is_separator ? 0 : 1)));
return list;
}
list.push_back(subString);
subString.clear();
list.push_back(sub_string);
sub_string.clear();
}
}

return list;
}


static std::string sub_string(const std::string& str, size_t start_index, size_t length) noexcept {
if (start_index >= str.size()) return "";
return str.substr(start_index, length);
}

static const std::string to_lower(const std::string& str) noexcept {
std::string result = str;
auto result = str;
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
return result;
}

static std::string substring(const std::string& str, size_t start_index, size_t length) noexcept {
if (start_index >= str.size()) return "";
return str.substr(start_index, length);

static const std::string to_upper(const std::string& str) noexcept {
auto result = str;
std::transform(result.begin(), result.end(), result.begin(), ::toupper);
return result;
}



static std::string trim_end(const std::string& str, const std::vector<char>& trim_chars) noexcept {
if (!str.size()) return str;
std::string result(str);
auto result = str;
while (std::find(trim_chars.begin(), trim_chars.end(), result[result.size() - 1]) != trim_chars.end())
result.erase(result.size() - 1, 1);
return result;
}

static bool try_parse(const std::string& str, int32_t& value) noexcept {
try {
value = std::atoi(str.c_str());
Expand All @@ -143,7 +150,6 @@ namespace xtd::native::linux {
return false;
}
}

};
}

44 changes: 21 additions & 23 deletions src/xtd.core.native.macos/include/xtd/native/macos/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ namespace xtd::native::macos {

template<typename separator_t, typename collection_t>
static std::string join(const separator_t& separator, const collection_t& values, size_t index, size_t count) noexcept {
size_t i = 0;
std::stringstream ss;
auto i = size_t {};
auto ss = std::stringstream {};
for (const auto& item : values) {
if (i >= index) {
if (i != index) ss << separator;
Expand Down Expand Up @@ -60,12 +60,12 @@ namespace xtd::native::macos {
}

static size_t last_index_of(const std::string& str, char value, size_t start_index, size_t count) noexcept {
size_t result = str.rfind(value, start_index + count - 1);
auto result = str.rfind(value, start_index + count - 1);
return result < start_index ? str.npos : result;
}

static size_t last_index_of(const std::string& str, const std::string& value, size_t start_index, size_t count) noexcept {
size_t result = str.rfind(value, start_index + count - value.size());
auto result = str.rfind(value, start_index + count - value.size());
return result < start_index ? str.npos : result;
}

Expand All @@ -75,13 +75,13 @@ namespace xtd::native::macos {

static std::string remove(const std::string& str, size_t start_index, size_t count) noexcept {
if (start_index > str.size()) return str;
std::string result(str);
auto result = str;
return result.erase(start_index, count);
}

static std::string replace(const std::string& str, const std::string& old_string, const std::string& new_string) noexcept {
std::string result(str);
size_t index = 0;
auto result = str;
auto index = size_t {};
while (true) {
index = result.find(old_string, index);
if (index == std::string::npos) break;
Expand All @@ -96,45 +96,45 @@ namespace xtd::native::macos {
if (count == 0) return {};
if (count == 1) return {str};

std::vector<std::string> list;
std::string subString;
std::vector<char> split_char_separators = separators.size() == 0 ? std::vector<char> {9, 10, 11, 12, 13, 32} : separators;
for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) {
bool is_separator = std::find(split_char_separators.begin(), split_char_separators.end(), *it) != split_char_separators.end();
if (!is_separator) subString.append(std::string(1, *it));
if ((static_cast<size_t>(it - str.begin()) == str.length() - 1 || is_separator) && (subString.length() > 0 || (subString.length() == 0 && !remove_empty_entries))) {
auto list = std::vector<std::string> {};
auto sub_string = std::string {};
auto split_char_separators = separators.size() == 0 ? std::vector<char> {9, 10, 11, 12, 13, 32} : separators;
for (auto it = str.begin(); it != str.end(); ++it) {
auto is_separator = std::find(split_char_separators.begin(), split_char_separators.end(), *it) != split_char_separators.end();
if (!is_separator) sub_string.append(std::string(1, *it));
if ((static_cast<size_t>(it - str.begin()) == str.length() - 1 || is_separator) && (sub_string.length() > 0 || (sub_string.length() == 0 && !remove_empty_entries))) {
if (list.size() == count - 1) {
list.push_back(subString + std::string(str.c_str(), it - str.begin() + (is_separator ? 0 : 1), str.length() - (it - str.begin()) + (is_separator ? 0 : 1)));
list.push_back(sub_string + std::string(str.c_str(), it - str.begin() + (is_separator ? 0 : 1), str.length() - (it - str.begin()) + (is_separator ? 0 : 1)));
return list;
}
list.push_back(subString);
subString.clear();
list.push_back(sub_string);
sub_string.clear();
}
}

return list;
}

static std::string substring(const std::string& str, size_t start_index, size_t length) noexcept {
static std::string sub_string(const std::string& str, size_t start_index, size_t length) noexcept {
if (start_index >= str.size()) return "";
return str.substr(start_index, length);
}

static const std::string to_lower(const std::string& str) noexcept {
std::string result = str;
auto result = str;
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
return result;
}

static const std::string to_upper(const std::string& str) noexcept {
std::string result = str;
auto result = str;
std::transform(result.begin(), result.end(), result.begin(), ::toupper);
return result;
}

static std::string trim_end(const std::string& str, const std::vector<char>& trim_chars) noexcept {
if (!str.size()) return str;
std::string result(str);
auto result = str;
while (std::find(trim_chars.begin(), trim_chars.end(), result[result.size() - 1]) != trim_chars.end())
result.erase(result.size() - 1, 1);
return result;
Expand All @@ -148,7 +148,5 @@ namespace xtd::native::macos {
return false;
}
}

};
}

Loading

0 comments on commit e0a4ee7

Please sign in to comment.