Skip to content

Allow concat 2 non-const strings? #2184

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

Closed
xzores opened this issue Nov 12, 2022 · 4 comments
Closed

Allow concat 2 non-const strings? #2184

xzores opened this issue Nov 12, 2022 · 4 comments

Comments

@xzores
Copy link
Contributor

xzores commented Nov 12, 2022

How can I concat 2 non-const strings?
Do I really have to make a string builder?

And if so would this really be the best way?
a : strings.Builder = builder_none();
write_string(&a, "This is my string: ");
write_string(&a, My_other_non_const_string);

And how would i get a string from this?

Would i do this:
clone_from_bytes(a.buf[:])?

Why not allow me just to do this:
"This is my string: " + My_other_non_const_string

@z64
Copy link
Contributor

z64 commented Nov 12, 2022

@xzores You're looking for strings.concatenate

import "core:strings"
// ...
s := strings.concatenate({a, b, c})

@Kelimion
Copy link
Member

Kelimion commented Nov 12, 2022

One way:

a: strings.Builder
write_string(&a, "This is my string: ")
write_string(&a, My_other_non_const_string)
result := strings.to_string(a)

Another way:

buf := make([]u8, 500)
a := "This is my string: "
copy(buf, a)
copy(buf[len(a):], b)
result := string(buf[:len(a) + len(b)])

And there are other ways besides, like result := strings.concatenate({a, b, c}), as @z64 pointed out.

As for why not str := "This is my string: " + My_other_non_const_string. Because that is a monumentally bad idea.
Not only does + now mean more than 1 thing, it's now not just a concatenation instead of addition, it also makes an allocation for you.

Odin is a low-level language, not Python. Programs are more maintainable and understandable when the code does what you write, not a whole bunch in addition implicitly.

This explicitness (for sanity's sake) doesn't even inflate the line count compared to other low-level languages. A program ported to Odin from C often results in a third the number of lines.

@xzores
Copy link
Contributor Author

xzores commented Nov 12, 2022

Thanks you guys!

@xzores xzores closed this as completed Nov 12, 2022
@z64
Copy link
Contributor

z64 commented Nov 12, 2022

@xzores I highly recommmend joining the Odin discord for quick questions like this, lots of helpful folk around: https://discord.gg/U8Rd2suP

Otherwise, there is also the Discussions page, which would be a better place for questions - https://github.com/odin-lang/Odin/discussions
Posts there can then be promoted to Issues when necessary.

Issues are usually reserved for bug reports.

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

No branches or pull requests

3 participants