-
-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(string): 文字列と文字とCode PointとCode Unitについて #188
Conversation
source/basic/string/README.md
Outdated
"あ".codePointAt(0); // => 12354 | ||
``` | ||
|
||
逆に、"String.fromCodePoint`メソッドを使うことで、指定したCode Pointの文字を取得できます。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
" -> ` ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ひとまず書いた
source/basic/string/README.md
Outdated
- [ ] TODO: リテラルとラッパーオブジェクトの違いについては別途参照 | ||
|
||
**文字**は**文字列**を構成する要素で、**配列の要素**と**配列**の関係に似ています。 | ||
文字列においても、配列と同様にインデックスを指定することで、指定したインデックスにある文字へアクセスできます。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
冗長。
配列と同様にインデックスでアクセスできます。
ぐらいでよさそう。具体的なコード見れば分かるだろうし
source/basic/string/README.md
Outdated
私達は視覚的に文字を認識しますが、コンピュータでは文字の形ではなく、「ひらがなの『あ』という種類の文字」といった情報をやり取りします。 | ||
また、視覚的に見えない制御文字や結合文字のように情報を組み合わせて扱うものも存在します。 | ||
|
||
- [ ] 制御文字の例 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これコンパクトに表を書くか、\n
みたいな具体例を幾つか書くか迷う。
source/basic/string/README.md
Outdated
|
||
- [ ] 制御文字の例 | ||
|
||
制御文字など含めた文字は、JavaScriptエンジン上で一意なビット列に変換されて扱われます。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JavaScriptエンジン上で
多分なんか言い回しが変。
UTF-16は外部コードであるはずだから、エンジン内部でそういうふうに変換されるかってたしかではない気も?
source/basic/string/README.md
Outdated
- [ ] 制御文字の例 | ||
|
||
制御文字など含めた文字は、JavaScriptエンジン上で一意なビット列に変換されて扱われます。 | ||
文字をビット列へ変換することを符号化(エンコード)と呼びます。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
() 大体かっこない(カタカナ)の表記をメインとして使ってるので、この辺って逆転した方がいいのかな?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
エンコードで十分伝わると思います
source/basic/string/README.md
Outdated
文字をビット列へ変換することを符号化(エンコード)と呼びます。 | ||
|
||
文字とビット列の組み合わせを定義したものが文字コードであり、JavaScriptでは文字コードとしてUnicodeを採用しています。 | ||
また、UnicodeにはUTF-8、UTF-16、UTF-32と文字をエンコードする方法がいくつかありますが、JavaScriptではUTF-16を採用しています。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
それ以外だとほぼ死んだUTF-7とかがある
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
など
でごまかしては
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
そうですね。
UnicodeにはUTF-8、UTF-16などエンコードする方式がいくつかありますが、JavaScriptではUTF-16を採用しています
ぐらいな感じですかね。
source/basic/string/README.md
Outdated
|
||
Unicodeでは、文字と1対1で対応するビット列を表のようなもので管理されています。 | ||
たとえば、"A"という文字は表の56の位置にあるといった、文字とビット列にはそれぞれ対応位置が決められています。 | ||
この、対応表における位置のことを符号位置(Code Point)と呼びます。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BMPを触れたくなくて抽象的に "表" と言ってる
source/basic/string/README.md
Outdated
"あ".codePointAt(0); // => 12354 | ||
``` | ||
|
||
逆に、"String.fromCodePoint`メソッドを使うことで、指定したCode Pointの文字を取得できます。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
バッククオートおかしくなってる
source/basic/string/README.md
Outdated
``` | ||
|
||
このようにCode Unitは歴史的な経緯もあり、1つまたは2つのCode Unitで1つのCode Pointを表現します。 | ||
JavaScriptでは基本的にStringメソッドは文字列をCode Unitが並んでいるものとして扱います。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
は、文字列を
としたい
source/basic/string/README.md
Outdated
JavaScriptでは基本的にStringメソッドは文字列をCode Unitが並んでいるものとして扱います。 | ||
|
||
``` | ||
"文字列" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
js
抜けてる
|
||
次の2つは例外として、文字列をCode Pointが並んでいるように扱います。 | ||
|
||
- Iterator(`for...or`や`Array.from`など) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
何かもっと直感的な例があるとよさそう?
source/basic/string/README.md
Outdated
- [ ] 文字 | ||
ここでいう文字列とはどのようなものでしょうか? | ||
|
||
「文字列(string)」とは「文字」が順序に連続したものです。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この言い回しも多分ばらついてるので統一した言い方にしたい
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
「文字列」とは「文字」が順番に並んでいるものです。
がいいかな。
多分、プログラマのための文字コード技術入門 の表現の影響受けてる。
JSONでユニコードシーケンスでかくことで |
source/basic/string/README.md
Outdated
符号単位(Code Unit)は、文字を表現する最小の単位ですが、解説をする前にまずUnicodeの歴史を振り返る必要があります。 | ||
|
||
Unicodeは元々16ビットつまり最大65,536文字で世界中の文字が収まるという前提で、文字とビット列の組み合わせを定義していました。 | ||
しかし、今もなお増えている文字が65,536文字で収まるわけもなく、1文字が16ビットで表現できるという前提は崩れてしまいました。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1文字が
これでは曖昧なので1codepointとかするべきでは?
source/basic/string/README.md
Outdated
このエンコード方式がUTF-16です。 | ||
|
||
JavaScriptの仕様であるECMAScriptもUTF-16を採用しているため、この16ビットを2つ並べる方式に対応しています。 | ||
この16ビット1つのことが**符号単位(Code Unit)**であり、文字列における最小の単位で、すべての文字列はCode Unitが並んでいるものとして扱われます。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
「したがって」のような接続詞がないと、UTF-16のみならずすべてに適用されてしまうのでは?
- Iterator(`for...or`や`Array.from`など) | ||
- メソッドに`CodePoint`という名前を含むもの | ||
|
||
### 文字列とCode UnitとCode Point {#string-code-unit-code-point} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
まとめっぽいものを追加した
整理されてわかりやすくなった気がする。 |
文字列を構成する文字とUnicodeについて
長い!
Notes:
以下は意図的に書いてないです。がそこまで正確にUnicodeを理解してないので、間違えてない範囲で省いてるという感じ。
#121