Skip to content
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

Merged
merged 21 commits into from
Feb 28, 2017

Conversation

azu
Copy link
Collaborator

@azu azu commented Feb 23, 2017

文字列を構成する文字とUnicodeについて

  • 文字列
  • 文字とは
  • Code Unit
  • Code Point

長い!

Notes:

以下は意図的に書いてないです。がそこまで正確にUnicodeを理解してないので、間違えてない範囲で省いてるという感じ。

  • Unicode用語の詳細
    • Unicodeを解説したいわけでもないので
  • 結合文字
  • Combining Marks
    • JavaScriptにおいて上手く扱う方法(メソッドのようなもの)があまりないため
    • String#normalizeは今回入れないため

#121

"あ".codePointAt(0); // => 12354
```

逆に、"String.fromCodePoint`メソッドを使うことで、指定したCode Pointの文字を取得できます。
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

" -> ` ?

Copy link
Collaborator Author

@azu azu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ひとまず書いた

- [ ] TODO: リテラルとラッパーオブジェクトの違いについては別途参照

**文字**は**文字列**を構成する要素で、**配列の要素**と**配列**の関係に似ています。
文字列においても、配列と同様にインデックスを指定することで、指定したインデックスにある文字へアクセスできます。
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

冗長。
配列と同様にインデックスでアクセスできます。

ぐらいでよさそう。具体的なコード見れば分かるだろうし

私達は視覚的に文字を認識しますが、コンピュータでは文字の形ではなく、「ひらがなの『あ』という種類の文字」といった情報をやり取りします。
また、視覚的に見えない制御文字や結合文字のように情報を組み合わせて扱うものも存在します。

- [ ] 制御文字の例
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これコンパクトに表を書くか、\nみたいな具体例を幾つか書くか迷う。


- [ ] 制御文字の例

制御文字など含めた文字は、JavaScriptエンジン上で一意なビット列に変換されて扱われます。
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JavaScriptエンジン上で

多分なんか言い回しが変。
UTF-16は外部コードであるはずだから、エンジン内部でそういうふうに変換されるかってたしかではない気も?

- [ ] 制御文字の例

制御文字など含めた文字は、JavaScriptエンジン上で一意なビット列に変換されて扱われます。
文字をビット列へ変換することを符号化(エンコード)と呼びます。
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

() 大体かっこない(カタカナ)の表記をメインとして使ってるので、この辺って逆転した方がいいのかな?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

エンコードで十分伝わると思います

文字をビット列へ変換することを符号化(エンコード)と呼びます。

文字とビット列の組み合わせを定義したものが文字コードであり、JavaScriptでは文字コードとしてUnicodeを採用しています。
また、UnicodeにはUTF-8、UTF-16、UTF-32と文字をエンコードする方法がいくつかありますが、JavaScriptではUTF-16を採用しています。
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

それ以外だとほぼ死んだUTF-7とかがある

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

などでごまかしては

Copy link
Collaborator Author

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を採用しています

ぐらいな感じですかね。


Unicodeでは、文字と1対1で対応するビット列を表のようなもので管理されています。
たとえば、"A"という文字は表の56の位置にあるといった、文字とビット列にはそれぞれ対応位置が決められています。
この、対応表における位置のことを符号位置(Code Point)と呼びます。
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BMPを触れたくなくて抽象的に "表" と言ってる

"あ".codePointAt(0); // => 12354
```

逆に、"String.fromCodePoint`メソッドを使うことで、指定したCode Pointの文字を取得できます。
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

バッククオートおかしくなってる

```

このようにCode Unitは歴史的な経緯もあり、1つまたは2つのCode Unitで1つのCode Pointを表現します。
JavaScriptでは基本的にStringメソッドは文字列をCode Unitが並んでいるものとして扱います。
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

は、文字列を としたい

JavaScriptでは基本的にStringメソッドは文字列をCode Unitが並んでいるものとして扱います。

```
"文字列"
Copy link
Collaborator Author

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`など)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

何かもっと直感的な例があるとよさそう?

- [ ] 文字
ここでいう文字列とはどのようなものでしょうか?

「文字列(string)」とは「文字」が順序に連続したものです。
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この言い回しも多分ばらついてるので統一した言い方にしたい

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

「文字列」とは「文字」が順番に並んでいるものです。

がいいかな。
多分、プログラマのための文字コード技術入門 の表現の影響受けてる。

@azu
Copy link
Collaborator Author

azu commented Feb 23, 2017

JSONでユニコードシーケンスでかくことで " などをエスケープしないで書けるから、よく使われてるという話を書いてない(書いたけど入れる場所がなくて消してた)

符号単位(Code Unit)は、文字を表現する最小の単位ですが、解説をする前にまずUnicodeの歴史を振り返る必要があります。

Unicodeは元々16ビットつまり最大65,536文字で世界中の文字が収まるという前提で、文字とビット列の組み合わせを定義していました。
しかし、今もなお増えている文字が65,536文字で収まるわけもなく、1文字が16ビットで表現できるという前提は崩れてしまいました。
Copy link
Contributor

@yumetodo yumetodo Feb 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1文字が

これでは曖昧なので1codepointとかするべきでは?

このエンコード方式がUTF-16です。

JavaScriptの仕様であるECMAScriptもUTF-16を採用しているため、この16ビットを2つ並べる方式に対応しています。
この16ビット1つのことが**符号単位(Code Unit)**であり、文字列における最小の単位で、すべての文字列はCode Unitが並んでいるものとして扱われます。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

「したがって」のような接続詞がないと、UTF-16のみならずすべてに適用されてしまうのでは?

@azu azu changed the title feat(string): Code PointとCode Unitについて feat(string): 文字列と文字とCode PointとCode Unitについて Feb 24, 2017
@azu azu requested a review from lacolaco February 24, 2017 12:00
- Iterator(`for...or`や`Array.from`など)
- メソッドに`CodePoint`という名前を含むもの

### 文字列とCode UnitとCode Point {#string-code-unit-code-point}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

まとめっぽいものを追加した

@yumetodo
Copy link
Contributor

整理されてわかりやすくなった気がする。

@azu azu merged commit 80ce19c into master Feb 28, 2017
@azu azu deleted the code-unit-point branch February 28, 2017 10:56
@yumetodo yumetodo mentioned this pull request Dec 18, 2018
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants