Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
UTF-8 依存の Go コードとは?
Search
acomagu
June 29, 2019
Technology
1
170
UTF-8 依存の Go コードとは?
BigLT 2019
acomagu
June 29, 2019
Tweet
Share
More Decks by acomagu
See All by acomagu
Stripe SSoT をするべきか否か
acomagu
0
37
JP_Stripes: リコンサイル(突合処理)のテスト
acomagu
0
62
「境界付けられたコンテキスト間の関係」についてもっと語ろう
acomagu
0
62
地方 MaaS 事例: アプリの進化に伴って変化してきた Stripe 利用方法
acomagu
0
210
Stripe リコンサイルの勘所
acomagu
0
390
CDK 一発で全てのエラーログを Slack に流す
acomagu
0
2k
AWS CDK を支える Constructs について
acomagu
0
160
DDDとは結局何なのか
acomagu
0
270
API Gateway HTTP API について
acomagu
0
120
Other Decks in Technology
See All in Technology
滅・サービスクラス🔥 / Destruction Service Class
sinsoku
6
1.6k
【Developers Summit 2025】プロダクトエンジニアから学ぶ、 ユーザーにより高い価値を届ける技術
niwatakeru
2
1.4k
Raycast AI APIを使ってちょっと便利な拡張機能を作ってみた / created-a-handy-extension-using-the-raycast-ai-api
kawamataryo
0
100
目の前の仕事と向き合うことで成長できる - 仕事とスキルを広げる / Every little bit counts
soudai
25
7.2k
飲食店予約台帳を支えるインタラクティブ UI 設計と実装
siropaca
7
1.8k
エンジニアの育成を支える爆速フィードバック文化
sansantech
PRO
3
1.1k
Tech Blogを書きやすい環境づくり
lycorptech_jp
PRO
1
240
SA Night #2 FinatextのSA思想/SA Night #2 Finatext session
satoshiimai
1
140
レビューを増やしつつ 高評価維持するテクニック
tsuzuki817
1
740
室長と気ままに学ぶマイクロソフトのビジネスアプリケーションとビジネスプロセス
ryoheig0405
0
370
インフラをつくるとはどういうことなのか、 あるいはPlatform Engineeringについて
nwiizo
5
2.6k
『衛星データ利用の方々にとって近いようで触れる機会のなさそうな小話 ~ 衛星搭載ソフトウェアと衛星運用ソフトウェア (実物) を動かしながらわいわいする編 ~』 @日本衛星データコミニティ勉強会
meltingrabbit
0
150
Featured
See All Featured
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Adopting Sorbet at Scale
ufuk
74
9.2k
Being A Developer After 40
akosma
89
590k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
193
16k
Faster Mobile Websites
deanohume
306
31k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3k
How to Think Like a Performance Engineer
csswizardry
22
1.3k
Embracing the Ebb and Flow
colly
84
4.6k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Unsuck your backbone
ammeep
669
57k
Transcript
UTF-8依存のGoコードとは 190629 Aizu BigLT @acomagu
今日のゴール - Goの標準の文字コードとは何か? - UTF-8依存のコードとは? - 正しくUTF-8を処理する方法を知る → ドキュメントと実装に差異がないようにしよう
Go標準の文字コードとは?
s に入れるべき文字コードはわかりますか? (ここにある情報のみで)
godoc.org/builtin
godoc.org/builtin
特定の文字コードにしか対応しないなら ドキュメントに書かなければならない!
None
None
でも... UTF-8依存のコードなんて 書いた覚えないけど?
UTF-8依存のコードとは?
UTF-8依存クイズ~!!
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
}
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
return string(bts) } []byte -> string
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
return string(bts) } []byte -> string 非依存
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
return string(rns) } []rune -> string
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
return string(rns) } []rune -> string 依存
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
return []byte(str) } string -> []byte
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
return []byte(str) } string -> []byte 非依存
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
return []byte(rns) } []rune -> []byte
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
return []byte(rns) } []rune -> []byte 依存
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
return []rune(str) } string -> []rune
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
return []rune(str) } string -> []rune 依存
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
return []rune(bts) } []byte -> []rune
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
return []rune(bts) } []byte -> []rune 依存
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
return len(str) }
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
return len(str) } 非依存
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
for i := 0; i < len(str); i++ { _ = str[i] } }
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
for i := 0; i < len(str); i++ { _ = str[i] } } 非依存
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
for i := range str { _ = str[i] } }
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
for i := range str { _ = str[i] } } 依存
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
bytes.Index([]byte(str1), []byte(str2)) }
UTF-8依存? 非依存? func cast(str string, bts []byte, rns []rune) {
bytes.Index([]byte(str1), []byte(str2)) } 依存
お疲れ様です (ありがとうございます)
まとめ - rune が絡むキャストはUTF-8依存 - string を range にかけるとUTF-8依存 -
しかし len(str) や str[i] は非依存
bytes.Index 問について... Shift-JISだと “表” “\” 0x95 0x5C 0x5C
bytes.Index 問について... Shift-JISだと “表” “\” 0x95 0x5C 0x5C bytes.Index にかけるとまずい...
bytes.Index 問について... 1バイト目 2バイト目以降
bytes.Index 問について... 1バイト目 2バイト目以降 違う → bytes.Index大丈夫
正しくUTF-8を処理するには
① ASCII 依存にしない 例えば: UTF-8 で ”Number” はたくさんある → Unicode
のキャラクタクラスを適切に利用する → コメントに ASCII Number のみと記載する
② 正規化/比較で正しい手順を踏む - 例えば: - Unicode の正規化と比較方法は RFC8264(PRECIS) として 定義されている
- 「いつ使うべきか」も記述がある - golang.org/x/text/secure/precis
③ Grapheme Cluster を意識する Grapheme Cluster とは? → 「1文字の境界」を定義する仕様
③ Grapheme Cluster を意識する Grapheme Cluster とは? → 「1文字の境界」を定義する仕様 「1文字
== 1コードポイント」じゃないの? → Variation Selector が付与されている場合がある
③ Grapheme Cluster を意識する 絵文字とか良くね?
③ Grapheme Cluster を意識する Go では大変... - 標準ライブラリにはない - 外部ライブラリを使用
- Proposal は出てる - 正規表現での対応は不可能そう - \X ...
まとめ - Go の型には UTF-8 依存のものはないが、キャストなどには いくつかある - ASCII 依存
/ PRECIS / Grepheme Cluster を意識する → どこまで対応しているのかドキュメントでしっかり明確にしよ う
Thanks!