1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-11 13:10:58 +00:00

Add ord() function to return Unicode code point of a string of length one

This commit is contained in:
Masoud Montazeri
2018-08-26 17:08:07 +04:30
committed by Rémi Verschelde
parent 4a6b445a93
commit 5565f5591f
2 changed files with 37 additions and 0 deletions

View File

@@ -106,6 +106,7 @@ const char *GDScriptFunctions::get_func_name(Function p_func) {
"typeof", "typeof",
"type_exists", "type_exists",
"char", "char",
"ord",
"str", "str",
"print", "print",
"printt", "printt",
@@ -665,6 +666,33 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_
CharType result[2] = { *p_args[0], 0 }; CharType result[2] = { *p_args[0], 0 };
r_ret = String(result); r_ret = String(result);
} break; } break;
case TEXT_ORD: {
VALIDATE_ARG_COUNT(1);
if (p_args[0]->get_type() != Variant::STRING) {
r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING;
r_ret = Variant();
return;
}
String str = p_args[0]->operator String();
if (str.length() != 1) {
r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING;
r_ret = RTR("Expected a string of length 1 (a character).");
return;
}
r_ret = str.get(0);
} break;
case TEXT_STR: { case TEXT_STR: {
if (p_arg_count < 1) { if (p_arg_count < 1) {
r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
@@ -1507,6 +1535,7 @@ bool GDScriptFunctions::is_deterministic(Function p_func) {
case TYPE_OF: case TYPE_OF:
case TYPE_EXISTS: case TYPE_EXISTS:
case TEXT_CHAR: case TEXT_CHAR:
case TEXT_ORD:
case TEXT_STR: case TEXT_STR:
case COLOR8: case COLOR8:
case LEN: case LEN:
@@ -1848,6 +1877,13 @@ MethodInfo GDScriptFunctions::get_info(Function p_func) {
mi.return_val.type = Variant::STRING; mi.return_val.type = Variant::STRING;
return mi; return mi;
} break;
case TEXT_ORD: {
MethodInfo mi("ord", PropertyInfo(Variant::STRING, "char"));
mi.return_val.type = Variant::INT;
return mi;
} break; } break;
case TEXT_STR: { case TEXT_STR: {

View File

@@ -97,6 +97,7 @@ public:
TYPE_OF, TYPE_OF,
TYPE_EXISTS, TYPE_EXISTS,
TEXT_CHAR, TEXT_CHAR,
TEXT_ORD,
TEXT_STR, TEXT_STR,
TEXT_PRINT, TEXT_PRINT,
TEXT_PRINT_TABBED, TEXT_PRINT_TABBED,