godot-cpp/src/core/String.cpp

483 lines
12 KiB
C++
Raw Normal View History

2017-03-06 07:49:24 +00:00
#include "String.hpp"
2017-10-03 10:57:33 +00:00
#include "Array.hpp"
#include "NodePath.hpp"
2017-10-03 10:57:33 +00:00
#include "PoolArrays.hpp"
#include "Variant.hpp"
2017-10-20 23:42:10 +00:00
#include "GodotGlobal.hpp"
2017-10-03 10:37:34 +00:00
#include <gdnative/string.h>
#include <string.h>
2017-04-17 22:38:09 +00:00
namespace godot {
godot::CharString::~CharString() {
godot::api->godot_char_string_destroy(&_char_string);
}
int godot::CharString::length() const {
return godot::api->godot_char_string_length(&_char_string);
}
const char *godot::CharString::get_data() const {
return godot::api->godot_char_string_get_data(&_char_string);
}
2017-10-03 10:57:33 +00:00
godot::String::String() {
2017-10-20 23:42:10 +00:00
godot::api->godot_string_new(&_godot_string);
}
2017-10-03 10:57:33 +00:00
String::String(const char *contents) {
godot::api->godot_string_new(&_godot_string);
godot::api->godot_string_parse_utf8(&_godot_string, contents);
}
2017-10-03 10:57:33 +00:00
String::String(const wchar_t *contents) {
godot::api->godot_string_new_with_wide_string(&_godot_string, contents, wcslen(contents));
}
2017-10-03 10:57:33 +00:00
String::String(const wchar_t c) {
godot::api->godot_string_new_with_wide_string(&_godot_string, &c, 1);
}
2017-10-03 10:57:33 +00:00
String::String(const String &other) {
2017-10-20 23:42:10 +00:00
godot::api->godot_string_new_copy(&_godot_string, &other._godot_string);
}
2017-10-03 10:57:33 +00:00
String::~String() {
2017-10-20 23:42:10 +00:00
godot::api->godot_string_destroy(&_godot_string);
}
2017-10-03 10:57:33 +00:00
wchar_t &String::operator[](const int idx) {
2017-10-20 23:42:10 +00:00
return *godot::api->godot_string_operator_index(&_godot_string, idx);
}
2017-10-03 10:57:33 +00:00
wchar_t String::operator[](const int idx) const {
2017-10-20 23:42:10 +00:00
return *godot::api->godot_string_operator_index((godot_string *)&_godot_string, idx);
}
2017-10-03 10:57:33 +00:00
int String::length() const {
return godot::api->godot_string_length(&_godot_string);
}
2017-10-03 10:57:33 +00:00
void String::operator=(const String &s) {
2017-10-20 23:42:10 +00:00
godot::api->godot_string_destroy(&_godot_string);
godot::api->godot_string_new_copy(&_godot_string, &s._godot_string);
2017-03-18 14:23:28 +00:00
}
2017-10-03 10:57:33 +00:00
bool String::operator==(const String &s) {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_operator_equal(&_godot_string, &s._godot_string);
}
2017-10-03 10:57:33 +00:00
bool String::operator!=(const String &s) {
return !(*this == s);
}
2017-10-03 10:57:33 +00:00
String String::operator+(const String &s) {
2017-06-18 11:21:18 +00:00
String new_string = *this;
new_string._godot_string = godot::api->godot_string_operator_plus(&new_string._godot_string, &s._godot_string);
return new_string;
}
2017-10-03 10:57:33 +00:00
void String::operator+=(const String &s) {
2017-10-20 23:42:10 +00:00
_godot_string = godot::api->godot_string_operator_plus(&_godot_string, &s._godot_string);
}
2017-10-03 10:57:33 +00:00
void String::operator+=(const wchar_t c) {
// @Todo
}
2017-10-03 10:57:33 +00:00
bool String::operator<(const String &s) {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_operator_less(&_godot_string, &s._godot_string);
}
2017-10-03 10:57:33 +00:00
bool String::operator<=(const String &s) {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_operator_less(&_godot_string, &s._godot_string) ||
2017-10-03 10:57:33 +00:00
(*this == s);
}
2017-10-03 10:57:33 +00:00
bool String::operator>(const String &s) {
return !(*this <= s);
}
2017-10-03 10:57:33 +00:00
bool String::operator>=(const String &s) {
return !(*this < s);
}
2017-10-03 10:57:33 +00:00
String::operator NodePath() const {
return NodePath(*this);
}
const wchar_t *String::unicode_str() const {
return godot::api->godot_string_wide_str(&_godot_string);
}
2017-11-24 22:54:35 +00:00
char *String::alloc_c_string() const {
godot_char_string contents = godot::api->godot_string_utf8(&_godot_string);
2017-11-24 22:54:35 +00:00
int length = godot::api->godot_char_string_length(&contents);
char *result = (char *) godot::api->godot_alloc(length + 1);
if (result) {
memcpy(result, godot::api->godot_char_string_get_data(&contents), length + 1);
2017-11-24 22:54:35 +00:00
}
godot::api->godot_char_string_destroy(&contents);
2017-11-24 22:54:35 +00:00
return result;
}
CharString String::utf8() const {
CharString ret;
ret._char_string = godot::api->godot_string_utf8(&_godot_string);
return ret;
}
CharString String::ascii(bool p_extended) const {
CharString ret;
if (p_extended)
ret._char_string = godot::api->godot_string_ascii_extended(&_godot_string);
else
ret._char_string = godot::api->godot_string_ascii(&_godot_string);
return ret;
}
2017-10-03 10:57:33 +00:00
String operator+(const char *a, const String &b) {
2017-03-15 22:19:58 +00:00
return String(a) + b;
}
String operator+(const wchar_t *a, const String &b) {
return String(a) + b;
}
2017-10-03 10:57:33 +00:00
bool String::begins_with(String &p_string) const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_begins_with(&_godot_string, &p_string._godot_string);
2017-10-03 10:57:33 +00:00
}
bool String::begins_with_char_array(const char *p_char_array) const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_begins_with_char_array(&_godot_string, p_char_array);
2017-10-03 10:57:33 +00:00
}
PoolStringArray String::bigrams() const {
2017-10-20 23:42:10 +00:00
godot_array arr = godot::api->godot_string_bigrams(&_godot_string);
2017-10-03 10:57:33 +00:00
return *(PoolStringArray *)&arr;
}
String String::c_escape() const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_c_escape(&_godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
String String::c_unescape() const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_c_unescape(&_godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
String String::capitalize() const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_capitalize(&_godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
bool String::empty() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_empty(&_godot_string);
2017-10-03 10:57:33 +00:00
}
bool String::ends_with(String &p_string) const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_ends_with(&_godot_string, &p_string._godot_string);
2017-10-03 10:57:33 +00:00
}
2017-10-03 15:10:36 +00:00
void String::erase(int position, int chars) {
2017-10-20 23:42:10 +00:00
godot::api->godot_string_erase(&_godot_string, position, chars);
2017-10-03 10:57:33 +00:00
}
int String::find(String p_what, int p_from) const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_find(&_godot_string, p_what._godot_string);
2017-10-03 10:57:33 +00:00
}
int String::find_last(String what) const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_find_last(&_godot_string, what._godot_string);
2017-10-03 10:57:33 +00:00
}
int String::findn(String what, int from) const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_findn(&_godot_string, what._godot_string);
2017-10-03 10:57:33 +00:00
}
String String::format(Variant values, String placeholder) const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_format(&_godot_string, (godot_variant *)&values);
2017-10-03 10:57:33 +00:00
return new_string;
}
String String::get_base_dir() const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_get_base_dir(&_godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
String String::get_basename() const {
2017-10-20 23:42:10 +00:00
godot_string new_string = godot::api->godot_string_get_basename(&_godot_string);
2017-10-03 10:57:33 +00:00
return *(String *)&new_string;
}
String String::get_extension() const {
2017-10-20 23:42:10 +00:00
godot_string new_string = godot::api->godot_string_get_extension(&_godot_string);
2017-10-03 10:57:33 +00:00
return *(String *)&new_string;
}
String String::get_file() const {
2017-10-20 23:42:10 +00:00
godot_string new_string = godot::api->godot_string_get_file(&_godot_string);
2017-10-03 10:57:33 +00:00
return *(String *)&new_string;
}
int String::hash() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_hash(&_godot_string);
2017-10-03 10:57:33 +00:00
}
int String::hex_to_int() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_hex_to_int(&_godot_string);
2017-10-03 10:57:33 +00:00
}
String String::insert(int position, String what) const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_insert(&_godot_string, position, what._godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
bool String::is_abs_path() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_is_abs_path(&_godot_string);
2017-10-03 10:57:33 +00:00
}
bool String::is_rel_path() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_is_rel_path(&_godot_string);
2017-10-03 10:57:33 +00:00
}
bool String::is_subsequence_of(String text) const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_is_subsequence_of(&_godot_string, &text._godot_string);
2017-10-03 10:57:33 +00:00
}
bool String::is_subsequence_ofi(String text) const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_is_subsequence_ofi(&_godot_string, &text._godot_string);
2017-10-03 10:57:33 +00:00
}
bool String::is_valid_float() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_is_valid_float(&_godot_string);
2017-10-03 10:57:33 +00:00
}
bool String::is_valid_html_color() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_is_valid_html_color(&_godot_string);
2017-10-03 10:57:33 +00:00
}
bool String::is_valid_identifier() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_is_valid_identifier(&_godot_string);
2017-10-03 10:57:33 +00:00
}
bool String::is_valid_integer() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_is_numeric(&_godot_string);
2017-10-03 10:57:33 +00:00
}
bool String::is_valid_ip_address() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_is_valid_ip_address(&_godot_string);
2017-10-03 10:57:33 +00:00
}
String String::json_escape() const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_json_escape(&_godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
String String::left(int position) const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_left(&_godot_string, position);
2017-10-03 10:57:33 +00:00
return new_string;
}
bool String::match(String expr) const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_match(&_godot_string, &expr._godot_string);
2017-10-03 10:57:33 +00:00
}
bool String::matchn(String expr) const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_match(&_godot_string, &expr._godot_string);
2017-10-03 10:57:33 +00:00
}
PoolByteArray String::md5_buffer() const {
2017-10-20 23:42:10 +00:00
godot_pool_byte_array arr = godot::api->godot_string_md5_buffer(&_godot_string);
2017-10-03 10:57:33 +00:00
return *(PoolByteArray *)&arr;
}
String String::md5_text() const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_md5_text(&_godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
int String::ord_at(int at) const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_ord_at(&_godot_string, at);
2017-10-03 10:57:33 +00:00
}
String String::pad_decimals(int digits) const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_pad_decimals(&_godot_string, digits);
2017-10-03 10:57:33 +00:00
return new_string;
}
String String::pad_zeros(int digits) const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_pad_zeros(&_godot_string, digits);
2017-10-03 10:57:33 +00:00
return new_string;
}
String String::percent_decode() const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_percent_decode(&_godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
String String::percent_encode() const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_percent_encode(&_godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
String String::plus_file(String file) const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_plus_file(&_godot_string, &file._godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
String String::replace(String p_key, String p_with) const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_replace(&_godot_string, p_key._godot_string, p_with._godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
String String::replacen(String what, String forwhat) const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_replacen(&_godot_string, what._godot_string, forwhat._godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
int String::rfind(String what, int from) const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_rfind(&_godot_string, what._godot_string);
2017-10-03 10:57:33 +00:00
}
int String::rfindn(String what, int from) const {
// From -1
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_rfindn(&_godot_string, what._godot_string);
2017-10-03 10:57:33 +00:00
}
String String::right(int position) const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_right(&_godot_string, position);
2017-10-03 10:57:33 +00:00
return new_string;
}
PoolByteArray String::sha256_buffer() const {
2017-10-20 23:42:10 +00:00
godot_pool_byte_array arr = godot::api->godot_string_sha256_buffer(&_godot_string);
2017-10-03 10:57:33 +00:00
return *(PoolByteArray *)&arr;
}
String String::sha256_text() const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_sha256_text(&_godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
float String::similarity(String text) const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_similarity(&_godot_string, &text._godot_string);
2017-10-03 10:57:33 +00:00
}
2017-10-03 10:57:33 +00:00
PoolStringArray String::split(String divisor, bool allow_empty) const {
2017-10-20 23:42:10 +00:00
godot_array arr = godot::api->godot_string_split(&_godot_string, &divisor._godot_string);
2017-10-03 10:57:33 +00:00
return *(PoolStringArray *)&arr;
}
PoolRealArray String::split_floats(String divisor, bool allow_empty) const {
2017-10-20 23:42:10 +00:00
godot_array arr = godot::api->godot_string_split_floats(&_godot_string, &divisor._godot_string);
2017-10-03 10:57:33 +00:00
return *(PoolRealArray *)&arr;
}
String String::strip_edges(bool left, bool right) const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_strip_edges(&_godot_string, left, right);
2017-10-03 10:57:33 +00:00
return new_string;
}
String String::substr(int from, int len) const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_substr(&_godot_string, from, len);
2017-10-03 10:57:33 +00:00
return new_string;
}
float String::to_float() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_to_float(&_godot_string);
2017-10-03 10:57:33 +00:00
}
int64_t String::to_int() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_string_to_int(&_godot_string);
2017-10-03 10:57:33 +00:00
}
String String::to_lower() const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_to_lower(&_godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
String String::to_upper() const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_to_upper(&_godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
String String::xml_escape() const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_xml_escape(&_godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
String String::xml_unescape() const {
String new_string;
2017-10-20 23:42:10 +00:00
new_string._godot_string = godot::api->godot_string_xml_unescape(&_godot_string);
2017-10-03 10:57:33 +00:00
return new_string;
}
}