diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/io/packet_peer.cpp | 10 | ||||
-rw-r--r-- | core/io/packet_peer.h | 12 | ||||
-rw-r--r-- | core/os/os.cpp | 15 | ||||
-rw-r--r-- | core/os/os.h | 20 | ||||
-rw-r--r-- | core/ustring.cpp | 173 | ||||
-rw-r--r-- | core/ustring.h | 35 | ||||
-rw-r--r-- | core/variant_call.cpp | 4 |
7 files changed, 200 insertions, 69 deletions
diff --git a/core/io/packet_peer.cpp b/core/io/packet_peer.cpp index 16c73c26e7..c6b12f73ae 100644 --- a/core/io/packet_peer.cpp +++ b/core/io/packet_peer.cpp @@ -49,7 +49,7 @@ bool PacketPeer::is_object_decoding_allowed() const { return allow_object_decoding; } -Error PacketPeer::get_packet_buffer(PoolVector<uint8_t> &r_buffer) const { +Error PacketPeer::get_packet_buffer(PoolVector<uint8_t> &r_buffer) { const uint8_t *buffer; int buffer_size; @@ -78,7 +78,7 @@ Error PacketPeer::put_packet_buffer(const PoolVector<uint8_t> &p_buffer) { return put_packet(&r[0], len); } -Error PacketPeer::get_var(Variant &r_variant) const { +Error PacketPeer::get_var(Variant &r_variant) { const uint8_t *buffer; int buffer_size; @@ -107,7 +107,7 @@ Error PacketPeer::put_var(const Variant &p_packet) { return put_packet(buf, len); } -Variant PacketPeer::_bnd_get_var() const { +Variant PacketPeer::_bnd_get_var() { Variant var; get_var(var); @@ -117,7 +117,7 @@ Variant PacketPeer::_bnd_get_var() const { Error PacketPeer::_put_packet(const PoolVector<uint8_t> &p_buffer) { return put_packet_buffer(p_buffer); } -PoolVector<uint8_t> PacketPeer::_get_packet() const { +PoolVector<uint8_t> PacketPeer::_get_packet() { PoolVector<uint8_t> raw; last_get_error = get_packet_buffer(raw); @@ -202,7 +202,7 @@ int PacketPeerStream::get_available_packet_count() const { return count; } -Error PacketPeerStream::get_packet(const uint8_t **r_buffer, int &r_buffer_size) const { +Error PacketPeerStream::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { ERR_FAIL_COND_V(peer.is_null(), ERR_UNCONFIGURED); _poll_buffer(); diff --git a/core/io/packet_peer.h b/core/io/packet_peer.h index b08d44ad8a..a6d363ec12 100644 --- a/core/io/packet_peer.h +++ b/core/io/packet_peer.h @@ -37,13 +37,13 @@ class PacketPeer : public Reference { GDCLASS(PacketPeer, Reference); - Variant _bnd_get_var() const; + Variant _bnd_get_var(); void _bnd_put_var(const Variant &p_var); static void _bind_methods(); Error _put_packet(const PoolVector<uint8_t> &p_buffer); - PoolVector<uint8_t> _get_packet() const; + PoolVector<uint8_t> _get_packet(); Error _get_packet_error() const; mutable Error last_get_error; @@ -52,17 +52,17 @@ class PacketPeer : public Reference { public: virtual int get_available_packet_count() const = 0; - virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) const = 0; ///< buffer is GONE after next get_packet + virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) = 0; ///< buffer is GONE after next get_packet virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) = 0; virtual int get_max_packet_size() const = 0; /* helpers / binders */ - virtual Error get_packet_buffer(PoolVector<uint8_t> &r_buffer) const; + virtual Error get_packet_buffer(PoolVector<uint8_t> &r_buffer); virtual Error put_packet_buffer(const PoolVector<uint8_t> &p_buffer); - virtual Error get_var(Variant &r_variant) const; + virtual Error get_var(Variant &r_variant); virtual Error put_var(const Variant &p_packet); void set_allow_object_decoding(bool p_enable); @@ -91,7 +91,7 @@ protected: public: virtual int get_available_packet_count() const; - virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) const; + virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size); virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size); virtual int get_max_packet_size() const; diff --git a/core/os/os.cpp b/core/os/os.cpp index 8088a6fa74..7010d44930 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -536,12 +536,21 @@ String OS::get_joy_guid(int p_device) const { void OS::set_context(int p_context) { } + +OS::SwitchVSyncCallbackInThread OS::switch_vsync_function = NULL; + void OS::set_use_vsync(bool p_enable) { + _use_vsync = p_enable; + if (switch_vsync_function) { //if a function was set, use function + switch_vsync_function(p_enable); + } else { //otherwise just call here + _set_use_vsync(p_enable); + } } bool OS::is_vsync_enabled() const { - return true; + return _use_vsync; } OS::PowerState OS::get_power_state() { @@ -606,10 +615,6 @@ bool OS::has_feature(const String &p_feature) { return false; } -void *OS::get_stack_bottom() const { - return _stack_bottom; -} - OS::OS() { void *volatile stack_bottom; diff --git a/core/os/os.h b/core/os/os.h index 41500acd93..2faaa6ab8f 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -58,6 +58,7 @@ class OS { int _exit_code; int _orientation; bool _allow_hidpi; + bool _use_vsync; char *last_error; @@ -435,8 +436,16 @@ public: virtual void set_context(int p_context); - virtual void set_use_vsync(bool p_enable); - virtual bool is_vsync_enabled() const; + //amazing hack because OpenGL needs this to be set on a separate thread.. + //also core can't access servers, so a callback must be used + typedef void (*SwitchVSyncCallbackInThread)(bool); + + static SwitchVSyncCallbackInThread switch_vsync_function; + void set_use_vsync(bool p_enable); + bool is_vsync_enabled() const; + + //real, actual overridable function to switch vsync, which needs to be called from graphics thread if needed + virtual void _set_use_vsync(bool p_enable) {} virtual OS::PowerState get_power_state(); virtual int get_power_seconds_left(); @@ -445,13 +454,6 @@ public: virtual void force_process_input(){}; bool has_feature(const String &p_feature); - /** - * Returns the stack bottom of the main thread of the application. - * This may be of use when integrating languages with garbage collectors that - * need to check whether a pointer is on the stack. - */ - virtual void *get_stack_bottom() const; - bool is_hidpi_allowed() const { return _allow_hidpi; } OS(); virtual ~OS(); diff --git a/core/ustring.cpp b/core/ustring.cpp index 3a0708851e..ceafe209ea 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -393,7 +393,7 @@ bool String::operator<(const CharType *p_str) const { return false; //should never reach here anyway } -bool String::operator<=(String p_str) const { +bool String::operator<=(const String &p_str) const { return (*this < p_str) || (*this == p_str); } @@ -426,7 +426,7 @@ bool String::operator<(const char *p_str) const { return false; //should never reach here anyway } -bool String::operator<(String p_str) const { +bool String::operator<(const String &p_str) const { return operator<(p_str.c_str()); } @@ -734,7 +734,7 @@ Vector<String> String::split_spaces() const { return ret; } -Vector<String> String::split(const String &p_splitter, bool p_allow_empty) const { +Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const { Vector<String> ret; int from = 0; @@ -745,8 +745,21 @@ Vector<String> String::split(const String &p_splitter, bool p_allow_empty) const int end = find(p_splitter, from); if (end < 0) end = len; - if (p_allow_empty || (end > from)) - ret.push_back(substr(from, end - from)); + if (p_allow_empty || (end > from)) { + if (p_maxsplit <= 0) + ret.push_back(substr(from, end - from)); + else if (p_maxsplit > 0) { + + // Put rest of the string and leave cycle. + if (p_maxsplit == ret.size()) { + ret.push_back(substr(from, len)); + break; + } + + // Otherwise, push items until positive limit is reached. + ret.push_back(substr(from, end - from)); + } + } if (end == len) break; @@ -889,7 +902,10 @@ String String::to_upper() const { for (int i = 0; i < upper.size(); i++) { - upper[i] = _find_upper(upper[i]); + const char s = upper[i]; + const char t = _find_upper(s); + if (s != t) // avoid copy on write + upper[i] = t; } return upper; @@ -897,20 +913,17 @@ String String::to_upper() const { String String::to_lower() const { - String upper = *this; + String lower = *this; - for (int i = 0; i < upper.size(); i++) { + for (int i = 0; i < lower.size(); i++) { - upper[i] = _find_lower(upper[i]); + const char s = lower[i]; + const char t = _find_lower(s); + if (s != t) // avoid copy on write + lower[i] = t; } - return upper; -} - -int String::length() const { - - int s = size(); - return s ? (s - 1) : 0; // length does not include zero + return lower; } const CharType *String::c_str() const { @@ -2162,7 +2175,7 @@ Vector<uint8_t> String::sha256_buffer() const { return ret; } -String String::insert(int p_at_pos, String p_string) const { +String String::insert(int p_at_pos, const String &p_string) const { if (p_at_pos < 0) return *this; @@ -2190,10 +2203,15 @@ String String::substr(int p_from, int p_chars) const { p_chars = length() - p_from; } + if (p_from == 0 && p_chars >= length()) { + + return String(*this); + } + return String(&c_str()[p_from], p_chars); } -int String::find_last(String p_str) const { +int String::find_last(const String &p_str) const { int pos = -1; int findfrom = 0; @@ -2206,19 +2224,21 @@ int String::find_last(String p_str) const { return pos; } -int String::find(String p_str, int p_from) const { + +int String::find(const String &p_str, int p_from) const { if (p_from < 0) return -1; - int src_len = p_str.length(); + const int src_len = p_str.length(); - int len = length(); + const int len = length(); if (src_len == 0 || len == 0) return -1; //wont find anything! const CharType *src = c_str(); + const CharType *str = p_str.c_str(); for (int i = p_from; i <= (len - src_len); i++) { @@ -2233,7 +2253,7 @@ int String::find(String p_str, int p_from) const { return -1; }; - if (src[read_pos] != p_str[j]) { + if (src[read_pos] != str[j]) { found = false; break; } @@ -2246,6 +2266,62 @@ int String::find(String p_str, int p_from) const { return -1; } +int String::find(const char *p_str, int p_from) const { + + if (p_from < 0) + return -1; + + const int len = length(); + + if (len == 0) + return -1; //wont find anything! + + const CharType *src = c_str(); + + int src_len = 0; + while (p_str[src_len] != '\0') + src_len++; + + if (src_len == 1) { + + const char needle = p_str[0]; + + for (int i = p_from; i < len; i++) { + + if (src[i] == needle) { + return i; + } + } + + } else { + + for (int i = p_from; i <= (len - src_len); i++) { + + bool found = true; + for (int j = 0; j < src_len; j++) { + + int read_pos = i + j; + + if (read_pos >= len) { + + ERR_PRINT("read_pos>=len"); + return -1; + }; + + if (src[read_pos] != p_str[j]) { + found = false; + break; + } + } + + if (found) + return i; + } + } + + return -1; +} + int String::findmk(const Vector<String> &p_keys, int p_from, int *r_key) const { if (p_from < 0) @@ -2300,7 +2376,7 @@ int String::findmk(const Vector<String> &p_keys, int p_from, int *r_key) const { return -1; } -int String::findn(String p_str, int p_from) const { +int String::findn(const String &p_str, int p_from) const { if (p_from < 0) return -1; @@ -2341,7 +2417,7 @@ int String::findn(String p_str, int p_from) const { return -1; } -int String::rfind(String p_str, int p_from) const { +int String::rfind(const String &p_str, int p_from) const { // establish a limit int limit = length() - p_str.length(); @@ -2387,7 +2463,7 @@ int String::rfind(String p_str, int p_from) const { return -1; } -int String::rfindn(String p_str, int p_from) const { +int String::rfindn(const String &p_str, int p_from) const { // establish a limit int limit = length() - p_str.length(); @@ -2687,7 +2763,7 @@ String String::format(const Variant &values, String placeholder) const { return new_string; } -String String::replace(String p_key, String p_with) const { +String String::replace(const String &p_key, const String &p_with) const { String new_string; int search_from = 0; @@ -2700,12 +2776,43 @@ String String::replace(String p_key, String p_with) const { search_from = result + p_key.length(); } + if (search_from == 0) { + + return *this; + } + new_string += substr(search_from, length() - search_from); return new_string; } -String String::replace_first(String p_key, String p_with) const { +String String::replace(const char *p_key, const char *p_with) const { + + String new_string; + int search_from = 0; + int result = 0; + + while ((result = find(p_key, search_from)) >= 0) { + + new_string += substr(search_from, result - search_from); + new_string += p_with; + int k = 0; + while (p_key[k] != '\0') + k++; + search_from = result + k; + } + + if (search_from == 0) { + + return *this; + } + + new_string += substr(search_from, length() - search_from); + + return new_string; +} + +String String::replace_first(const String &p_key, const String &p_with) const { String new_string; int search_from = 0; @@ -2719,11 +2826,16 @@ String String::replace_first(String p_key, String p_with) const { break; } + if (search_from == 0) { + + return *this; + } + new_string += substr(search_from, length() - search_from); return new_string; } -String String::replacen(String p_key, String p_with) const { +String String::replacen(const String &p_key, const String &p_with) const { String new_string; int search_from = 0; @@ -2736,6 +2848,11 @@ String String::replacen(String p_key, String p_with) const { search_from = result + p_key.length(); } + if (search_from == 0) { + + return *this; + } + new_string += substr(search_from, length() - search_from); return new_string; } diff --git a/core/ustring.h b/core/ustring.h index 9c24133b55..73537bfd13 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -93,8 +93,8 @@ public: bool operator!=(const CharType *p_str) const; bool operator<(const CharType *p_str) const; bool operator<(const char *p_str) const; - bool operator<(String p_str) const; - bool operator<=(String p_str) const; + bool operator<(const String &p_str) const; + bool operator<=(const String &p_str) const; signed char casecmp_to(const String &p_str) const; signed char nocasecmp_to(const String &p_str) const; @@ -103,15 +103,19 @@ public: const CharType *c_str() const; /* standard size stuff */ - int length() const; + _FORCE_INLINE_ int length() const { + int s = size(); + return s ? (s - 1) : 0; // length does not include zero + } /* complex helpers */ String substr(int p_from, int p_chars) const; - int find(String p_str, int p_from = 0) const; ///< return <0 if failed - int find_last(String p_str) const; ///< return <0 if failed - int findn(String p_str, int p_from = 0) const; ///< return <0 if failed, case insensitive - int rfind(String p_str, int p_from = -1) const; ///< return <0 if failed - int rfindn(String p_str, int p_from = -1) const; ///< return <0 if failed, case insensitive + int find(const String &p_str, int p_from = 0) const; ///< return <0 if failed + int find(const char *p_str, int p_from) const; ///< return <0 if failed + int find_last(const String &p_str) const; ///< return <0 if failed + int findn(const String &p_str, int p_from = 0) const; ///< return <0 if failed, case insensitive + int rfind(const String &p_str, int p_from = -1) const; ///< return <0 if failed + int rfindn(const String &p_str, int p_from = -1) const; ///< return <0 if failed, case insensitive int findmk(const Vector<String> &p_keys, int p_from = 0, int *r_key = NULL) const; ///< return <0 if failed bool match(const String &p_wildcard) const; bool matchn(const String &p_wildcard) const; @@ -125,10 +129,11 @@ public: Vector<String> bigrams() const; float similarity(const String &p_string) const; String format(const Variant &values, String placeholder = "{_}") const; - String replace_first(String p_key, String p_with) const; - String replace(String p_key, String p_with) const; - String replacen(String p_key, String p_with) const; - String insert(int p_at_pos, String p_string) const; + String replace_first(const String &p_key, const String &p_with) const; + String replace(const String &p_key, const String &p_with) const; + String replace(const char *p_key, const char *p_with) const; + String replacen(const String &p_key, const String &p_with) const; + String insert(int p_at_pos, const String &p_string) const; String pad_decimals(int p_digits) const; String pad_zeros(int p_digits) const; String lpad(int min_length, const String &character = " ") const; @@ -162,7 +167,7 @@ public: String get_slice(String p_splitter, int p_slice) const; String get_slicec(CharType p_splitter, int p_slice) const; - Vector<String> split(const String &p_splitter, bool p_allow_empty = true) const; + Vector<String> split(const String &p_splitter, bool p_allow_empty = true, int p_maxsplit = 0) const; Vector<String> split_spaces() const; Vector<float> split_floats(const String &p_splitter, bool p_allow_empty = true) const; Vector<float> split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const; @@ -204,7 +209,7 @@ public: Vector<uint8_t> md5_buffer() const; Vector<uint8_t> sha256_buffer() const; - inline bool empty() const { return length() == 0; } + _FORCE_INLINE_ bool empty() const { return length() == 0; } // path functions bool is_abs_path() const; @@ -242,6 +247,8 @@ public: */ /* String(CharType p_char);*/ inline String() {} + inline String(const String &p_str) : + Vector(p_str) {} String(const char *p_str); String(const CharType *p_str, int p_clip_to_len = -1); String(const StrRange &p_range); diff --git a/core/variant_call.cpp b/core/variant_call.cpp index f66cce85c9..2b99a60ba5 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -254,7 +254,7 @@ struct _VariantCall { VCALL_LOCALMEM2R(String, replacen); VCALL_LOCALMEM2R(String, insert); VCALL_LOCALMEM0R(String, capitalize); - VCALL_LOCALMEM2R(String, split); + VCALL_LOCALMEM3R(String, split); VCALL_LOCALMEM2R(String, split_floats); VCALL_LOCALMEM0R(String, to_upper); VCALL_LOCALMEM0R(String, to_lower); @@ -1446,7 +1446,7 @@ void register_variant_methods() { ADDFUNC2R(STRING, STRING, String, replacen, STRING, "what", STRING, "forwhat", varray()); ADDFUNC2R(STRING, STRING, String, insert, INT, "position", STRING, "what", varray()); ADDFUNC0R(STRING, STRING, String, capitalize, varray()); - ADDFUNC2R(STRING, POOL_STRING_ARRAY, String, split, STRING, "divisor", BOOL, "allow_empty", varray(true)); + ADDFUNC3R(STRING, POOL_STRING_ARRAY, String, split, STRING, "divisor", BOOL, "allow_empty", INT, "maxsplit", varray(true, 0)); ADDFUNC2R(STRING, POOL_REAL_ARRAY, String, split_floats, STRING, "divisor", BOOL, "allow_empty", varray(true)); ADDFUNC0R(STRING, STRING, String, to_upper, varray()); |