先日以下の記事を書きました。 ここでUTF-8の最初の文字かどうかの判定を以下の自作した関数で実装していました。 const ( t1 = 0b00000000 tx = 0b10000000 t2 = 0b00000110 t3 = 0b00001110 t4 = 0b00011110 ) func isUTF8LeadByte(tmp byte) bool { if tmp&tx == t1 { return true } tmp >>= 3 if tmp == t4 { return true } tmp >>= 1 if tmp == t3 { return true } tmp >>= 1 if tmp == t2 { return true } return false }
![Goの標準のutf8.RuneStart関数がエレガントすぎる](https://cdn-ak-scissors.b.st-hatena.com/image/square/1d8bf6b977f2363b0838bf0a56813e83f81c394d/height=288;version=1;width=512/https%3A%2F%2Fres.cloudinary.com%2Fzenn%2Fimage%2Fupload%2Fs--LidPa6Ko--%2Fc_fit%252Cg_north_west%252Cl_text%3Anotosansjp-medium.otf_55%3AGo%2525E3%252581%2525AE%2525E6%2525A8%252599%2525E6%2525BA%252596%2525E3%252581%2525AEutf8.RuneStart%2525E9%252596%2525A2%2525E6%252595%2525B0%2525E3%252581%25258C%2525E3%252582%2525A8%2525E3%252583%2525AC%2525E3%252582%2525AC%2525E3%252583%2525B3%2525E3%252583%252588%2525E3%252581%252599%2525E3%252581%25258E%2525E3%252582%25258B%252Cw_1010%252Cx_90%252Cy_100%2Fg_south_west%252Cl_text%3Anotosansjp-medium.otf_37%3Acatatsuy%252Cx_203%252Cy_121%2Fg_south_west%252Ch_90%252Cl_fetch%3AaHR0cHM6Ly9zdG9yYWdlLmdvb2dsZWFwaXMuY29tL3plbm4tdXNlci11cGxvYWQvYXZhdGFyL2NiYTVlYjRjMTEuanBlZw%3D%3D%252Cr_max%252Cw_90%252Cx_87%252Cy_95%2Fv1627283836%2Fdefault%2Fog-base-w1200-v2.png)