You have identified a gap in the social media market for very very short posts. Now that Twitter allows 280 character posts, people wanting quick social media updates aren't being served. You decide to create your own social media network.
To make your product noteworthy, you make it extreme and only allow posts of 5 or less characters. Any posts of more than 5 characters should be truncated to 5.
To allow your users to express themselves fully, you allow Emoji and other Unicode.
The task is to truncate input strings to 5 characters.
Text stored digitally has to be converted to a series of bytes. There are 3 ways to map characters to bytes in common use.
UTF-8 and UTF-16 are both Unicode encodings which means they're capable of representing a massive range of characters including:
UTF-8 and UTF-16 are both variable length encodings, which means that different characters take up different amounts of space.
Consider the letter 'a' and the emoji 'π'. In UTF-16 the letter takes 2 bytes but the emoji takes 4 bytes.
The trick to this exercise is to use APIs designed around Unicode characters (codepoints) instead of Unicode codeunits.
In version 8.1, Tcl strings were able to handle Unicode characters seamlessly. At the time, that was quite groundbreaking. However, as more and more Unicode characters were created, Tcl's ability to handle them fell behind.
Tcl's internal implementation could only handle 16-bit Unicode characters, which only covers the first 65,526 characters. This Tcl wiki page, particularly the discussion about the "AndroWish" language, gives the reasons.
Tcl 9.0 is the first version that can handle the full Unicode character set. Consequently, this exercise is only (easily) solved with Tcl version 9.0.
Sign up to Exercism to learn and master Tcl with 135 exercises, and real human mentoring, all for free.