Skip to content

Commit

Permalink
add a bunch of string functions to common
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitalita committed Dec 26, 2024
1 parent 1f7ae79 commit 5121c8e
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 2 deletions.
1 change: 0 additions & 1 deletion exporters/translation_exporter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "translation_exporter.h"

#include "../../../../../../../../Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__config"
#include "compat/optimized_translation_extractor.h"
#include "compat/resource_loader_compat.h"
#include "exporters/export_report.h"
Expand Down
105 changes: 104 additions & 1 deletion utility/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class GodotFileInterface : public tga::FileInterface {
GodotFileInterface(const String &p_path, FileAccess::ModeFlags p_mode) {
m_file = FileAccess::open(p_path, p_mode);
}

// Returns true if we can read/write bytes from/into the file
virtual bool ok() const override {
return m_file.is_valid();
Expand Down Expand Up @@ -486,4 +487,106 @@ bool gdre::dir_is_empty(const String &dir) {
f = da->get_next();
}
return true;
}
}

//void get_chars_in_set(const String &s, const HashSet<char32_t> &chars, HashSet<char32_t> &ret);

void gdre::get_chars_in_set(const String &s, const HashSet<char32_t> &chars, HashSet<char32_t> &ret) {
for (int i = 0; i < s.length(); i++) {
if (chars.has(s[i])) {
ret.insert(s[i]);
}
}
}

bool gdre::has_chars_in_set(const String &s, const HashSet<char32_t> &chars) {
for (int i = 0; i < s.length(); i++) {
if (chars.has(s[i])) {
return true;
}
}
return false;
}

String gdre::remove_chars(const String &s, const HashSet<char32_t> &chars) {
String ret;
for (int i = 0; i < s.length(); i++) {
if (!chars.has(s[i])) {
ret += s[i];
}
}
return ret;
}

Vector<String> gdre::split_multichar(const String &s, const HashSet<char32_t> &splitters, bool allow_empty, int maxsplit) {
Vector<String> ret;
String current;
int i;
for (i = 0; i < s.length(); i++) {
if (splitters.has(s[i])) {
if (current.length() > 0 || allow_empty) {
ret.push_back(current);
current = "";
if (maxsplit > 0 && ret.size() >= maxsplit - 1) {
i++;
break;
}
}
} else {
current += s[i];
}
}
if (i < s.length()) {
current += s.substr(i, s.length());
}
if (current.length() > 0 || allow_empty) {
ret.push_back(current);
}
return ret;
}

Vector<String> gdre::rsplit_multichar(const String &s, const HashSet<char32_t> &splitters, bool allow_empty, int maxsplit) {
Vector<String> ret;
String current;
int i;
for (i = s.length() - 1; i >= 0; i--) {
if (splitters.has(s[i])) {
if (current.length() > 0 || allow_empty) {
ret.push_back(current);
current = "";
if (maxsplit > 0 && ret.size() >= maxsplit - 1) {
i--;
break;
}
}
} else {
current = s[i] + current;
}
}
if (i >= 0) {
current = s.substr(0, i + 1) + current;
}
if (current.length() > 0 || allow_empty) {
ret.push_back(current);
}
ret.reverse();
return ret;
}

bool gdre::string_has_whitespace(const String &s) {
for (int i = 0; i < s.length(); i++) {
if (s[i] == ' ' || s[i] == '\t' || s[i] == '\n') {
return true;
}
}
return false;
};

bool gdre::string_is_ascii(const String &s) {
for (int i = 0; i < s.length(); i++) {
if (s[i] > 127) {
return false;
}
}
return true;
};
27 changes: 27 additions & 0 deletions utility/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ bool hashset_intersects_vector(const HashSet<T> &a, const Vector<T> &b) {
return false;
}

template <class K, class V>
Vector<K> get_keys(const HashMap<K, V> &map) {
Vector<K> ret;
for (const auto &E : map) {
ret.push_back(E.key);
}
return ret;
}

template <class K, class V>
HashSet<K> get_set_of_keys(const HashMap<K, V> &map) {
HashSet<K> ret;
for (const auto &E : map) {
ret.insert(E.key);
}
return ret;
}

template <class T>
Vector<T> get_vector_intersection(const Vector<T> &a, const Vector<T> &b) {
Vector<T> ret;
Expand Down Expand Up @@ -87,6 +105,15 @@ void shuffle_vector(Vector<T> &vec) {
}
}

bool string_is_ascii(const String &s);
bool string_has_whitespace(const String &s);
void get_chars_in_set(const String &s, const HashSet<char32_t> &chars, HashSet<char32_t> &ret);
bool has_chars_in_set(const String &s, const HashSet<char32_t> &chars);
String remove_chars(const String &s, const HashSet<char32_t> &chars);
Vector<String> split_multichar(const String &s, const HashSet<char32_t> &splitters, bool allow_empty = true,
int maxsplit = 0);
Vector<String> rsplit_multichar(const String &s, const HashSet<char32_t> &splitters, bool allow_empty = true,
int maxsplit = 0);
} // namespace gdre

#define GDRE_ERR_DECOMPRESS_OR_FAIL(img) \
Expand Down

0 comments on commit 5121c8e

Please sign in to comment.