Skip to content

User defined REVERSE() function Unicode support 🎉#453

Open
teizz wants to merge 1 commit into
WordPress:trunkfrom
teizz:trunk
Open

User defined REVERSE() function Unicode support 🎉#453
teizz wants to merge 1 commit into
WordPress:trunkfrom
teizz:trunk

Conversation

@teizz

@teizz teizz commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

As discussed with @JanJakes in pull request #434 I've gone ahead and added the code to support unicode characters while keeping behavior aligned with how mysql reverses a utf8mb4 unicode string, i.e. it correctly reverses multi-byte characters, but fails to preserve the correct order for grapheme clusters / combining characters.

As a practical example: both this implementation and mysql's REVERSE() function will reverse '🧟 🧟‍♂️ 🧟‍♀️' to become '♀‍🧟 ️♂‍🧟 🧟' instead of '🧟‍♀️ 🧟‍♂️ 🧟'.

I've also gone ahead and added a check to enable a fast path with strrev() in case all characters are normal ASCII, this improves both speed and prevents additional allocation for arrays.

Finally, I checked to see that

    preg_match_all( '/./u', $str, $matches );
    return implode( '', array_reverse( $matches[0] ) );

is actually about twice as fast as

    $chars = preg_split( '//u', $str, -1, PREG_SPLIT_NO_EMPTY );
    return implode( '', array_reverse( $chars ) );

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.

1 participant