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
180
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
A New Era of Testing
mannodermaus
2
510
Rechartsで楽にゴリゴリにカスタマイズする!
10tera
1
170
RAGの回答精度評価用のQAデータセットを生成AIに作らせた話
kurahara
0
250
令和トラベルにおけるLLM活用事例:社内ツール開発から得た学びと実践
ippo012
0
210
Rubyとクリエイティブコーディングの輪の広がり / The Growing Circle of Ruby and Creative Coding
chobishiba
1
270
The Sequel to a Dream of Ruby Parser's Grammar
ydah
1
220
[DroidKaigi 2024] Android ViewからJetpack Composeへ 〜Jetpack Compose移行のすゝめ〜 / From Android View to Jetpack Compose: A Guide to Migration
syarihu
1
630
The Shape of a Service Object
inem
0
520
Ebitengineの1vs1ゲーム WebRTCの活用
ponyo877
0
380
AndroidアプリのUIバリエーションをあの手この手で確認する / Check UI variations of Android apps by various means
tkmnzm
1
180
Amazon Neptuneで始める初めてのグラフDB ー グラフDBを使う意味を考える ー
satoshi256kbyte
2
260
仮想ファイルシステムを導入して開発環境のストレージ課題を解消する
segadevtech
2
550
Featured
See All Featured
How to Ace a Technical Interview
jacobian
274
23k
10 Git Anti Patterns You Should be Aware of
lemiorhan
653
58k
Building Your Own Lightsaber
phodgson
101
6k
Creatively Recalculating Your Daily Design Routine
revolveconf
215
12k
Fontdeck: Realign not Redesign
paulrobertlloyd
80
5.1k
The Straight Up "How To Draw Better" Workshop
denniskardys
230
130k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
23
1.7k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
190
16k
Optimising Largest Contentful Paint
csswizardry
31
2.8k
The Language of Interfaces
destraynor
153
23k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
8.9k
Code Reviewing Like a Champion
maltzj
517
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