Skip to content

Commit

Permalink
Remove 0 at end of strings. (#213)
Browse files Browse the repository at this point in the history
* remove 0 at end of strings.
  • Loading branch information
CedNaru authored May 4, 2021
1 parent a139fc2 commit 6fa0f95
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class StringTest : Node() {
@RegisterFunction
fun identity(str: String) = str

@RegisterFunction
fun getLength(str: String) = str.length

@RegisterFunction
fun fillTheBuffer(str1: String, str2: String, str3: String, str4: String, str5: String): Boolean {
return true
Expand Down
9 changes: 9 additions & 0 deletions harness/tests/test/unit/test_string.gd
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@ func test_flood_string() -> void:
var script = godot_tests_coretypes_StringTest.new()
assert_eq(script.fill_the_buffer(longest_short_str, longest_short_str, longest_short_str, longest_short_str, longest_short_str), true, "Should return true when buffer is filled")
script.free()

func test_string_length() -> void:
var script = godot_tests_coretypes_StringTest.new()
assert_eq(script.get_length(""), 0, "Should return the right size of the string")
assert_eq(script.get_length("A"), 1, "Should return the right size of the string")
assert_eq(script.get_length("abcde"), 5, "Should return the right size of the string")
assert_eq("", script.identity(""), "String on JVM side should be same as gdscript one.")
assert_eq("A", script.identity("A"), "String on JVM side should be same as gdscript one.")
script.free()
19 changes: 14 additions & 5 deletions kt/godot-runtime/src/main/kotlin/godot/core/Variant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,21 @@ enum class VariantType(
if(isLong){
val str = LongStringQueue.pollString()
str
}
else{
} else {
/**
* A CString is read from the buffer, they all end with a 0 character except if the String is empty
* "" has a size of 0, but "a" has a size of 2.
* We only read the buffer if the size is superior to 0.
* When it's the case, we create a string without the last 0 character.
*/
val stringSize = buffer.int
val charArray = ByteArray(stringSize)
buffer.get(charArray, 0, stringSize)
val str = String(charArray, Charsets.UTF_8)
val str = if (stringSize == 0) {
String()
} else {
val charArray = ByteArray(stringSize)
buffer.get(charArray, 0, stringSize)
String(charArray, 0, stringSize - 1, Charsets.UTF_8)
}
str
}
},
Expand Down
7 changes: 5 additions & 2 deletions src/kt_variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ namespace ktvariant {
String str{src};
const CharString& char_string{str.utf8()};
set_variant_type(des, Variant::Type::STRING);
if (unlikely(char_string.size() > LongStringQueue::max_string_size)) {
int size = char_string.size();
if (unlikely(size > LongStringQueue::max_string_size)) {
des->increment_position(encode_uint32(true, des->get_cursor()));
LongStringQueue::get_instance().send_string_to_jvm(str);
} else {
des->increment_position(encode_uint32(false, des->get_cursor()));
des->increment_position(encode_uint32(char_string.size(), des->get_cursor()));
des->increment_position(encode_cstring(char_string, des->get_cursor()));
if (likely(size > 0)) {
des->increment_position(encode_cstring(char_string, des->get_cursor()));
}
}
}

Expand Down

0 comments on commit 6fa0f95

Please sign in to comment.