Skip to content

builtin: add array.last_index()#26252

Merged
medvednikov merged 5 commits into
vlang:masterfrom
kbkpbot:add-array-last-index
Jan 3, 2026
Merged

builtin: add array.last_index()#26252
medvednikov merged 5 commits into
vlang:masterfrom
kbkpbot:add-array-last-index

Conversation

@kbkpbot

@kbkpbot kbkpbot commented Jan 3, 2026

Copy link
Copy Markdown
Contributor

Feature request by #26247

generated C code would like this:

// V auto functions:
static int Array_int_index(Array_int a, int v) {
        int* pelem = a.data;
        for (int i = 0; i < a.len; ++i, ++pelem) {
                if (*pelem == v) {
                        return i;
                }
        }
        return -1;
}

static int Array_int_last_index(Array_int a, int v) {
        if (a.len == 0) return -1;
        static int Array_int_last_index(Array_int a, int v) {
        if (a.len == 0) return -1;
        int* pelem = (byte*)a.data + (a.len-1)*a.element_size;
        for (int i = a.len-1; i >= 0; --i, --pelem) {
                if (*pelem == v) {
                        return i;
                }
        }
        return -1;
}

@kbkpbot kbkpbot linked an issue Jan 3, 2026 that may be closed by this pull request
2 tasks

@spytheman spytheman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.
Thank you @kbkpbot .

@medvednikov medvednikov merged commit ef05fb1 into vlang:master Jan 3, 2026
85 checks passed
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

Successfully merging this pull request may close these issues.

add array method last_index()

3 participants