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
170
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
940
Other Decks in Programming
See All in Programming
Zero Waste, Radical Magic, and Italian Graft – Quarkus Efficiency Secrets
hollycummins
0
230
コードレビューで学ぶ!Kotlinオブジェクト指向デザインパターン
akkie76
2
200
PHPの次期バージョンはこの時期どうなっているのか - Internalsの開発体制について - PHPカンファレンス小田原
youkidearitai
PRO
1
190
データアナリストが行うDatabricksを活用したETLの自動化事例
shinoa
0
260
try! Swift Tokyo 2024 参加報告 / try! Swift Tokyo 2024 Report
hironytic
0
200
サイコロで理解する統計的仮説検定の考え方
tatamiya
4
890
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
640
MicrosoftのPlatform Engineeringガイドを読んで実際になにかやってみた
ymd65536
1
290
見た目から始める生産性向上
ikumatadokoro
7
810
ONE WEDGE_company_guide
1wedge_one
0
450
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
130
Prepare for Jakarta EE 11 - Performance and Developer Productivity
ivargrimstad
0
710
Featured
See All Featured
Building an army of robots
kneath
300
41k
Automating Front-end Workflow
addyosmani
1356
200k
Building Better People: How to give real-time feedback that sticks.
wjessup
355
18k
The MySQL Ecosystem @ GitHub 2015
samlambert
243
12k
Atom: Resistance is Futile
akmur
259
25k
Learning to Love Humans: Emotional Interface Design
aarron
267
39k
The Brand Is Dead. Long Live the Brand.
mthomps
49
28k
It's Worth the Effort
3n
180
27k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
125
32k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
60
14k
The Straight Up "How To Draw Better" Workshop
denniskardys
227
130k
Code Reviewing Like a Champion
maltzj
514
39k
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