ConorHolt
ConorHolt

Reputation: 291

Which complexity mb_strlen?

strlen complexity is O(1), because string structure save string length, but what about mb_strlen? And please explain me, why?

Upvotes: 0

Views: 221

Answers (1)

Mateusz Drost
Mateusz Drost

Reputation: 1191

Looking at source code in worst case it is O(N).

mbtab = encoding->mblen_table;
n = 0;
p = string->val;
k = string->len;
/* count */
if (p != NULL) {
    while (n < k) {
        m = mbtab[*p];
        n += m;
        p += m;
        len++;
    };
}

Upvotes: 1

Related Questions