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

Add String.invert()/reverse() #3503

Closed
me2beats opened this issue Nov 5, 2021 · 3 comments · Fixed by godotengine/godot#78529
Closed

Add String.invert()/reverse() #3503

me2beats opened this issue Nov 5, 2021 · 3 comments · Fixed by godotengine/godot#78529
Milestone

Comments

@me2beats
Copy link

me2beats commented Nov 5, 2021

Describe the project you are working on

Plugins

Describe the problem or limitation you are having in your project

I need to invert a string but I can't to do this simply and efficient.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Adding String.invert()/String.reverse() would solve the problem

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

The method would return new inverted string
"hello".invert() would give "olleh"

If this enhancement will not be used often, can it be worked around with a few lines of script?

There are some workrounds, for example

static func invert_string(s:String)->String:
    var chars_pool = PoolStringArray()
    var length = s.length()
    chars_pool.resize(length)
    for i in length:
        chars_pool[i] = s[i]
    chars_pool.invert()
    return chars_pool.join("")

This doesn't look efficient tho

Is there a reason why this should be core and not an add-on in the asset library?

Inverting strings is quite frequent task when working with text. For example in Python you can use [::-1] to do this.

@me2beats
Copy link
Author

me2beats commented Nov 7, 2021

Updated the workaround example. It still requires using loop in gdscript.
First of all I tried split() but it doesn't work with empty delimiter.

@Calinou
Copy link
Member

Calinou commented Nov 7, 2021

First of all I tried split() but it doesn't work with empty delimiter.

See godotengine/godot#21699 which was rejected.

Edit: This behavior works in 4.0 and later though.

@AThousandShips
Copy link
Member

I also want to suggest this trivial workaround in 4.x:

func reverse(arg: String) -> String:
	var arr := arg.to_utf32_buffer().to_int32_array()
	arr.reverse()
	return arr.to_byte_array().get_string_from_utf32()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants