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
Goでテキストエディタを作った話@GDG Devfest2020
Search
arakawa
October 17, 2020
Programming
0
190
Goでテキストエディタを作った話@GDG Devfest2020
https://tokyo.gdgjapan.org/devfest2020
arakawa
October 17, 2020
Tweet
Share
More Decks by arakawa
See All by arakawa
Goのソースコードから読み解くオブジェクト指向プログラミング@Object-Oriented Conference 2020
adsholoko
5
1k
Other Decks in Programming
See All in Programming
データマイグレーションの成功戦略~サービスリニューアルで失敗しないための実践ガイド~
tkzwtks
8
850
watsonx.ai Dojo #3 プロンプトエンジニアリング入門
oniak3ibm
PRO
0
410
Folding Cheat Sheet #8
philipschwarz
PRO
0
140
Cloud Adoption Framework にみる組織とクラウド導入戦略
tomokusaba
2
690
Vertical Architectures for Scalable Angular Applications
manfredsteyer
PRO
0
210
書籍『LangChainとLangGraphによるRAG・AIエージェント[実践]入門』の紹介
os1ma
2
120
今日で分かる!カスタムコップの作り方
krpk1900
2
270
ActiveRecordの力でDBのメタデータを迅速に解析する
lnit
5
2k
Jakarta EE as Seen Trough the Lens of the ASF
ivargrimstad
0
860
Going Staff - Keynote edition
pragtob
0
360
Vitest Browser Mode への期待 / Vitest Browser Mode
odanado
PRO
1
1.2k
Honoの来た道とこれから
yusukebe
14
2.2k
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
3
340
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.6k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
37
1.8k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
It's Worth the Effort
3n
183
27k
A designer walks into a library…
pauljervisheath
202
24k
Typedesign – Prime Four
hannesfritz
39
2.4k
The Cult of Friendly URLs
andyhume
77
6k
Transcript
Goでテキストエディタを作った話
荒川聖悟 Sansan事業部 プロダクト開発部 iOSエンジニア 自己紹介 2 @adsholoko
車輪の再発明が好きです 3 NESエミュレータ『goones』 https://github.com/ad-sho-loko/goones DB『bogoDB』 https://github.com/ad-sho-loko/bogoDB 他にもコンパイラなど...
ターミナル上で動作するテキストエディタmille
milleの特徴 - ターミナルで動作する自作テキストエディタ - ソースコードは1000行以内(テストコードは除く) - Goで実装 - 外部ライブラリを利用していない
milleの機能 - ASCII対応 - キーボードショートカット - ファイルの読み込み/保存 - Goのキーワードハイライト機能
参考実装
どうやって作るのか3つほど紹介 - キーボード入力を受け付ける仕組み - ターミナルをテキストエディタに変える仕組み - 文字入力を効率化するためのデータ構造
キーボードからの入力受け付け - 当然、標準入力から受け付ける - ただし処理中にキー入力があった場 合にも取りこぼさないように実装する 必要がある goroutineにて別スレッドとして実行させ、排 他制御のためにchannelを利用
ターミナルをテキストエディタに変える仕組み 通常、プロセスは標準入力のデータをバッファし、改行が行われたときにプロセスに渡 されるようになっている(Cookedモード) しかしテキストエディタでは1文字入力するごとにプロセスに渡したい場合はrawモード にする必要がある。
Rawモードへ移行する方法 https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html https://en.wikipedia.org/wiki/Terminal_mode 参考
テキストエディタに適したデータ構造選び - 当然ながらテキストエディタは文字を入力し、それを格納する必要がある - 文字の参照・入力・挿入・削除はなるべく早く実行したい! - しかしすべてがO(1)なデータ構造はない... GapBufferというデータ構造を採用することに
“Hello, World”を入力することを考えてみる 文字入力の動き
文字入力の動き ‘H’を挿入 H
H e ‘e’を挿入 文字入力の動き
H e l 文字入力の動き
H e l l o , W o r l
d ! ‘!’を挿入 文字入力の動き
H e l , W o r l d !
’lo’を入力し忘れた! もし入力ミスが起きたときに...
配列の場合 H e l , W o r l d
! ‘l’を挿入
配列の場合 H e l , , W o r l
d ! O(n)操作が発生! 挿入処理のためにずらす必要がある
配列の場合 H e l l , W o r l
d ! ‘o’を挿入
配列の場合 H e l l , W o r l
d ! O(n)操作が発生!(二回目)
配列の場合 H e l l o , W o r
l d ! 挿入操作が走るたびにO(n)発生する
GapBuffer H e l , W o r l d
! ’lo’を入力し忘れた!
GapBuffer H e l , W o r l d
! ‘l’を挿入
GapBuffer H e l , W , W o r
l d ! 挿入位置以降の文字列を後方に移す
GapBuffer H e l , W , W o r
l d ! O(n)操作が発生! 挿入位置以降の文字列を後方に移す
GapBuffer H e l l , W o r l
d ! ‘o’を挿入
GapBuffer H e l l o , W o r
l d ! ‘o’を挿入 O(1)で挿入可能!
GapBuffer - データ構造 H e l l o , W
o r l d ! array startPieceIndex endPieceIndex https://github.com/ad-sho-loko/mille/blob/master/gap_table.go
GapBuffer メリット 挿入をする箇所から連続で入力する人間の動きに適したデータ構造である - 実装コードがシンプル - 参照はO(1)で可能 - 一定の条件下では挿入O(1)、削除O(1) デメリット
- 要素をランダムに挿入・削除すると、配列と同様にO(n) 分のコストがかかる
まとめ - テキストエディタに適したデータ構造やアルゴリズムが存在する - Goが低レイヤな箇所は抽象化しており、本質的な箇所の実装だけで良い - 1000行未満のコードなのでぜひ詳細はコードリーディングしてみてください - 車輪の再発明は楽しい!!
テキストエディタ作りましょう
質問はTwitterにてどうぞ! @adsholoko 34