Follow

So, challenge to the wizards out there: is this the best we can do to count Unicode glyphs in a string (equivalent of string.characters.count in Swift) or is there a more performant way I don’t know about?

(It’s fast enough for my needs but not ideal by any means.)

ar.al/2021/10/31/how-to-count-

· · Web · 3 · 2 · 2

@aral

never used Vala personally but curious if `char_count()` may do what you are looking for. It appears `length()` returns the number of bytes.

valadoc.org/glib-2.0/string.le

@bmallred Nope, sadly char_count does not take grapheme clusters into consideration. Did try :)

@aral a sign of a great programming language is when it makes the easy things hard!

@aral Unicode does not have a concept of glyph, so it's an incorrect question to ask.

In all cases, one has to look at what one actually needs, and decide on the solution based on that. Sometimes it's bytes, sometimes it's unicode codepoints, sometimes it's grapheme clusters, or could could be something entirely different.

Sign in to participate in the conversation
Aral’s Mastodon

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!