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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
acomagu
June 29, 2019
Technology
230
1
Share
UTF-8 依存の Go コードとは?
BigLT 2019
acomagu
June 29, 2019
More Decks by acomagu
See All by acomagu
Payment Records API を使って地域通貨を Stripe Dashboard に統合してみた
acomagu
0
59
Restate x Stripe: 安心して眠れる決済システムを目指して
acomagu
0
15
Stripe SSoT をするべきか否か
acomagu
0
78
JP_Stripes: リコンサイル(突合処理)のテスト
acomagu
0
120
「境界付けられたコンテキスト間の関係」についてもっと語ろう
acomagu
0
170
地方 MaaS 事例: アプリの進化に伴って変化してきた Stripe 利用方法
acomagu
0
490
Stripe リコンサイルの勘所
acomagu
0
540
CDK 一発で全てのエラーログを Slack に流す
acomagu
0
2.3k
AWS CDK を支える Constructs について
acomagu
0
190
Other Decks in Technology
See All in Technology
電子辞書Brainをネットに繋げてみた(自力編)
raspython3
0
320
サプライチェーンセキュリティの空白地帯 - 信頼できる”依存性”の未来を考える
rung
PRO
2
470
Javaで学ぶSOLID原則
negima
1
240
最低限これだけ押さえれ大丈夫_Claude Enterprise/Team企業展開ガバナンス入門
tkikuchi
1
540
Oracle AI Database@Google Cloud:サービス概要のご紹介
oracle4engineer
PRO
6
1.5k
形式手法特論:公平性制約の位相的特徴づけ #kernelvm / Kernel VM Study Kansai 12th
ytaka23
1
580
Oracle Cloud Infrastructure:2026年5月度サービス・アップデート
oracle4engineer
PRO
1
270
AI フレンドリーなエラー監視を TypeScript で実現する
shinyaigeek
2
190
AI-DLCを活用した高品質・安全なAI駆動開発実践 / AI Driven Development
yoshidashingo
1
250
人が担う「価値」とは?これからの「QA」とは / Human Value and the Future of Quality Assurance
bitkey
PRO
0
140
Claude code Orchestra
ozakiomumkj
2
670
食べログのサーキットブレーカー導入を振り返って
atpons
1
150
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
135
9.9k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
210
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
150
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.9k
エンジニアに許された特別な時間の終わり
watany
107
240k
Unsuck your backbone
ammeep
672
58k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
Claude Code のすすめ
schroneko
67
220k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
54k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.3k
Typedesign – Prime Four
hannesfritz
42
3.1k
Become a Pro
speakerdeck
PRO
31
6k
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!