Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Only remove trailing blankets #22

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions addons/godot_xml/xml.gd
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,23 @@ static func _get_attributes(parser: XMLParser) -> Dictionary:
static func _cleanup_double_blankets(xml: PackedByteArray) -> PackedByteArray:
# XMLParser is again "incorrect" and duplicates nodes due to double blank escapes
# https://github.com/godotengine/godot/issues/81896#issuecomment-1731320027
var cleaned := PackedByteArray()

for byte in xml:
if byte not in [9, 10, 13]: # [\t, \n, \r]
cleaned.append(byte)
var rm_count := 0 # How much elements (blankets) to remove from the source
var idx := xml.size() - 1

return cleaned
# Iterate in reverse order. This matters for perf because otherwise we
# would need to do a double .reverse() and remove elements from the start
# of the array, both of which are quite expensive
while idx >= 0:
if xml[idx] in [9, 10, 13]: # [\t, \n, \r]
rm_count += 1
idx -= 1
else:
break

# Remove blankets
while rm_count > 0:
xml.remove_at(xml.size() - 1)
rm_count -= 1

return xml