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 behaviour of bytes.split_multi to match string.split_multi #2364

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Lperlind
Copy link
Contributor

@Lperlind Lperlind commented Mar 6, 2023

Before it was impossible to split on a substr that was not the smallest length, i.e.

test :: proc() {
    my_slice := []byte { 0, 1, 2, 0, 1, 2, 3, 4, 0, 0 }
    split := bytes.split_multi(my_slice, { { 1 }, { 3, 4 }})
    fmt.println(split) // prints [[0], [2, 0], [2, 3, 4, 0, 0]]
    // the real answer should be [[0], [2, 0], [2], [0, 0]]
}

Tested it with the following

test :: proc() {
    my_slice := []byte { 0, 0, 1, 2, 0, 1, 2, 3, 4, 0, 0 }

    split := bytes.split_multi(my_slice, { { 1 }, { 3, 4 }}, false, context.temp_allocator)
    fmt.println(split) // prints [[0], [2, 0], [2], [0, 0]]
    split_2 := bytes.split_multi(my_slice, { { 1, 2 }, { 3, 4 }}, false, context.temp_allocator)
    fmt.println(split_2) // prints [[0], [0], [], [0, 0]]
    split_3 := bytes.split_multi(my_slice, { { 1, 2 }, { 3, 4 }}, true, context.temp_allocator)
    fmt.println(split_3) // prints [[0], [0], [0, 0]]
    split_4 := bytes.split_multi(my_slice, { {  0, 1, 2, 0, 1, 2, 3, 4, 0, 0 }, { 0, 0 }}, true, context.temp_allocator)
    fmt.println(split_4) // prints [[1, 2, 0, 1, 2, 3, 4]]
    split_5 := bytes.split_multi(my_slice, { { 0 }, {  0, 1, 2, 0, 1, 2, 3, 4, 0 } }, true, context.temp_allocator)
    fmt.println(split_5) // prints []
    split_6 := bytes.split_multi(my_slice, { { 0 }, {  0, 1, 2, 0, 1, 2, 3, 4, 0 } }, false, context.temp_allocator)
    fmt.println(split_6) // prints [[], [], [], []]

    // ensure same behaviour between strings split multi (when skip_empty is set to true)
    as_string_test := "hello~~~world--split.multi!test"
    seps := []string { "~~~", "--", ".", "!" }
    string_split := strings.split_multi(as_string_test, seps, context.temp_allocator)
    fmt.println(string_split)
    string_split_bytes := transmute([]string)bytes.split_multi(transmute([]byte)as_string_test, transmute([][]byte)seps, true, context.temp_allocator)
    fmt.println(string_split_bytes)
    assert(len(string_split) == len(string_split_bytes))
    for _, i in string_split {
        assert(string_split[i] == string_split_bytes[i])
    }
}

@Lperlind
Copy link
Contributor Author

Lperlind commented Mar 6, 2023

Eh looks like I have some local changes in Odin causing the build errors, i'll fix those.

@Lperlind
Copy link
Contributor Author

Lperlind commented Mar 6, 2023

and I should probably do the iterator too...

@Kelimion
Copy link
Member

Kelimion commented Mar 6, 2023

If you want, feel free to add those tests to tests\core\... as well.

@github-actions github-actions bot added the stale label Jul 4, 2023
@github-actions github-actions bot removed the stale label Feb 2, 2024
@github-actions github-actions bot added the stale label Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants