4 ms·
As someone's already pointed out, C# strings are composed of UTF-16 codepoints not characters - this means that if you have a character outside the basic multil
by bazzargh 13y ago
As someone's already pointed out, C# strings are composed of UTF-16 codepoints not characters - this means that if you have a character outside the basic multilingual plane it'll be represented as two codepoints using a surrogate, and the character count in the C# string will be wrong (the same is true of Java and JS for example)
That's a hard problem, and avoiding it in every situation would require scanning the strings for surrogates beforehand, when you might never need to know that information. Go makes it explicit that knowing the exact character position and string length in characters comes at a cost.
There's a good discussion of this on Tim Bray's blog:
http://www.tbray.org/ongoing/When/200x/2003/04/26/UTF http://www.tbray.org/ongoing/When/200x/2003/04/26/UTF
- twotwotwo 13y agoJust for fun, here's Go handling a char outside the BMP (😃, U+1F603): http://play.golang.org/p/qg7POYAAOL http://play.golang.org/p/qg7POYAAOL
- samatman 13y agohttps://github.com/mnemnion/emojure/ https://github.com/mnemnion/emojure/ You can even export them without Capital letters ;-)
- twotwotwo 13y agoI did run into at least one eminently reasonable use of how Go source is defined to be in UTF-8. Comments in the crypto libs just use math symbols where they're handy, like this in crypto/rsa[1]: // Check that de ≡ 1 mod p-1, for each prime. // This implies that e is coprime to each p-1 as e has a multiplicative // inverse. Therefore e is coprime to lcm(p-1,q-1,r-1,...) = // exponent(ℤ/nℤ). It also implies that a^de ≡ a mod p as a^(p-1) ≡ 1 // mod p. Thus a^de ≡ a mod n for all a coprime to n, as required. Sadly, the spec requires identifiers to be just Unicode letters and digits, so we will never experience the power and glory of emoji function names in Go. [1] http://golang.org/src/pkg/crypto/rsa/rsa.go http://golang.org/src/pkg/crypto/rsa/rsa.go