V: `str1 + str2 + str2` should be optimized

Created on 19 Jun 2019  路  9Comments  路  Source: vlang/v

Concatenating multiple strings should always result in only one allocation.

Optimization

All 9 comments

Have you thought about python style string formatting "hello {} {}".format(firstName, lastName) or something similar?
Edit: I guess we already have concatenation (+) and interpolation with variables ($var) so don't need the complexity

Also for joining and splitting arrays ",".join(array) or array.split(",") ?

Edit: I guess we already have concatenation (+) and interpolation with variables ($var) so don't need the complexity

You've answered your question correctly :)

Also for joining and splitting arrays ",".join(array) or array.split(",") ?

str_array.join(','), but no array splits.

Is it now
str1 + str2 + str3
or
str1 << str2 << str3
?

@gslicer the first one

@Delta456

@gslicer the first one

That seems again inconsistent with the Array notation where it is stated (according to docs):
"<< is an operator that appends a value to the end of the array. It can also append an entire array. "

So I would expect string (which is a [byte|rune]-Array to support the same notation - why not let + be pure mathematical?

When there was something I liked in C++ it was the "<<" operator on iostreams (which were basically byte-streams or var-length strings ;)

string is not an array.

I'm sorry @medvednikov but in current implementation string looks like an array to me (and you even called the variable representing a string "arr" which resambles Array):

module strings

pub fn repeat(c byte, n int) string {
    if n <= 0 {
        return ''
    }
    //mut arr := malloc(n + 1)
    mut arr := [byte(0)].repeat(n + 1)
    for i := 0; i < n; i++ {
        arr[i] = c
    }
    arr[n] = `\0`
    return string(arr, n)
}

My point is, + could be used only for arithmetic operations, and << for concatenation (of any kind) - this would be more streamlined with the "simple" and "do similar stuff with similar syntax" approach, don't you think?

No, it's not an array. String is an immutable struct with a pointer to string data, len, and capacity integers.

In this function arr refers to []byte from which a string is generated via string().

By the way thanks for referring to this function, it needs to be updated :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

radare picture radare  路  3Comments

clpo13 picture clpo13  路  3Comments

arg2das picture arg2das  路  3Comments

penguindark picture penguindark  路  3Comments

aurora picture aurora  路  3Comments