Godot: String.rstrip removes to much characters in some cases

Created on 13 Nov 2020  路  2Comments  路  Source: godotengine/godot

Godot version: 3.2.3 stable

OS/device including version: Kubuntu 20.04

Issue description:

rstrip removes one character before the . when removing patterns such as file extension: "abc.tscn".rstrip(".tscn") outputs ab whereas it should output abc.

  • This can be bypassed by doing "abc.tscn".rstrip("tscn").rstrip(".")
  • No particular error occurs by doing ".tscn".rstrip(".tscn")

Steps to reproduce:

extends EditorScript

func _run() -> void:
    var s := "abc.tscn"
    print(".tscn : " + s.rstrip(".tscn"))
    print("tscn : " + s.rstrip("tscn"))
    print("tscn then . : " + s.rstrip("tscn").rstrip("."))

This outputs:

.tscn : ab
tscn : abc.
tscn then . : abc
archived core

Most helpful comment

rstrip's parameter is a set of characters, not a pattern

So that's why!

image

I guess the docs could be exlpicit about chars being a set of characters rather than a pattern.
The correct way is then to "abc.tscn".replace(".tscn", "")!
I suppose it would be beneficial to mention this in the docs.

I'll go to docs then. Thanks!

All 2 comments

I don't think this is a bug. rstrip's parameter is a set of characters, not a pattern. So, it will remove the trailing characters that are included in the argument. In your first example, it removes the c because it is also included in the argument. Others also make sense this way.

rstrip's parameter is a set of characters, not a pattern

So that's why!

image

I guess the docs could be exlpicit about chars being a set of characters rather than a pattern.
The correct way is then to "abc.tscn".replace(".tscn", "")!
I suppose it would be beneficial to mention this in the docs.

I'll go to docs then. Thanks!

Was this page helpful?
0 / 5 - 0 ratings