From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/ja/web/api/textencoder/index.html | 156 ++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 files/ja/web/api/textencoder/index.html (limited to 'files/ja/web/api/textencoder/index.html') diff --git a/files/ja/web/api/textencoder/index.html b/files/ja/web/api/textencoder/index.html new file mode 100644 index 0000000000..4d622bf7a6 --- /dev/null +++ b/files/ja/web/api/textencoder/index.html @@ -0,0 +1,156 @@ +--- +title: TextEncoder +slug: Web/API/TextEncoder +tags: + - API + - Encoding + - Experimental + - Interface + - Reference + - TextEncoder +translation_of: Web/API/TextEncoder +--- +

{{APIRef("Encoding API")}}

+ +

TextEncoder はコードポイントのストリームを入力として受け取り、 UTF-8 バイトのストリームを出力します。

+ +
+

: UTF-8 以外のテキストエンコーディングに対応したポリフィルの実装が GitHub にあります。

+
+ +

+ +
const encoder = new TextEncoder()
+const view = encoder.encode('€')
+console.log(view); // Uint8Array(3) [226, 130, 172]
+
+ +

コンストラクター

+ +
+
{{DOMxRef("TextEncoder.TextEncoder", "TextEncoder()")}}
+
新たに生成した TextEncoder を返します。これは、 UTF-8 エンコーディングのバイトストリームを生成します。
+
+ +

プロパティ

+ +

TextEncoder インターフェイスは、何もプロパティを継承していません。

+ +
+
{{DOMxRef("TextEncoder.prototype.encoding")}}{{ReadOnlyInline}}
+
常に "utf-8" を返します。
+
+ +

メソッド

+ +

TextEncoder インターフェイスは、何もメソッドを継承していません。

+ +
+
{{DOMxRef("TextEncoder.prototype.encode()")}}
+
入力として {{domxref("USVString")}} を取り、 UTF-8 でエンコードされたテキストを含む {{jsxref("Uint8Array")}} を返します。
+
{{DOMxRef("TextEncoder.prototype.encodeInto()")}}
+
エンコードする {{domxref("USVString")}} と、 UTF-8 でエンコードされたテキストをが入る宛先の {{jsxref("Uint8Array")}} を取り、エンコーディングの進捗を示す辞書オブジェクトを返します。これは encode() よりも新しく、より性能が高くなる可能性があります。
+
+ +

ポリフィル

+ +

以下のポリフィルは、W3 が要求する仕様だけを満たします。 "これだけで" IE5 で動作するように設計されていますが、IE5 から IE9 では TypedArray に代わって通常の配列を返します。メモリの効率が悪く遅いブラウザーのような状況では、このポリフィル (さらに言えばどのポリフィルも) は古いブラウザーと大きな文字列に対して実用的でないでしょう。最後に、 0x1e << 3 のようなシーケンスを 0xf0 へ変換するため、以下のコードをミニファイアー (特にクロージャコンパイラー) を通して実行すべきであることに注意してください。これらのシーケンスはポリフィルの動作を美的に示すため、事前に計算されていません。

+ +
if (typeof TextEncoder === "undefined") {
+    TextEncoder=function TextEncoder(){};
+    TextEncoder.prototype.encode = function encode(str) {
+        "use strict";
+        var Len = str.length, resPos = -1;
+        // The Uint8Array's length must be at least 3x the length of the string because an invalid UTF-16
+        //  takes up the equivelent space of 3 UTF-8 characters to encode it properly. However, Array's
+        //  have an auto expanding length and 1.5x should be just the right balance for most uses.
+        var resArr = typeof Uint8Array === "undefined" ? new Array(Len * 1.5) : new Uint8Array(Len * 3);
+        for (var point=0, nextcode=0, i = 0; i !== Len; ) {
+            point = str.charCodeAt(i), i += 1;
+            if (point >= 0xD800 && point <= 0xDBFF) {
+                if (i === Len) {
+                    resArr[resPos += 1] = 0xef/*0b11101111*/; resArr[resPos += 1] = 0xbf/*0b10111111*/;
+                    resArr[resPos += 1] = 0xbd/*0b10111101*/; break;
+                }
+                // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
+                nextcode = str.charCodeAt(i);
+                if (nextcode >= 0xDC00 && nextcode <= 0xDFFF) {
+                    point = (point - 0xD800) * 0x400 + nextcode - 0xDC00 + 0x10000;
+                    i += 1;
+                    if (point > 0xffff) {
+                        resArr[resPos += 1] = (0x1e/*0b11110*/<<3) | (point>>>18);
+                        resArr[resPos += 1] = (0x2/*0b10*/<<6) | ((point>>>12)&0x3f/*0b00111111*/);
+                        resArr[resPos += 1] = (0x2/*0b10*/<<6) | ((point>>>6)&0x3f/*0b00111111*/);
+                        resArr[resPos += 1] = (0x2/*0b10*/<<6) | (point&0x3f/*0b00111111*/);
+                        continue;
+                    }
+                } else {
+                    resArr[resPos += 1] = 0xef/*0b11101111*/; resArr[resPos += 1] = 0xbf/*0b10111111*/;
+                    resArr[resPos += 1] = 0xbd/*0b10111101*/; continue;
+                }
+            }
+            if (point <= 0x007f) {
+                resArr[resPos += 1] = (0x0/*0b0*/<<7) | point;
+            } else if (point <= 0x07ff) {
+                resArr[resPos += 1] = (0x6/*0b110*/<<5) | (point>>>6);
+                resArr[resPos += 1] = (0x2/*0b10*/<<6)  | (point&0x3f/*0b00111111*/);
+            } else {
+                resArr[resPos += 1] = (0xe/*0b1110*/<<4) | (point>>>12);
+                resArr[resPos += 1] = (0x2/*0b10*/<<6)    | ((point>>>6)&0x3f/*0b00111111*/);
+                resArr[resPos += 1] = (0x2/*0b10*/<<6)    | (point&0x3f/*0b00111111*/);
+            }
+        }
+        if (typeof Uint8Array !== "undefined") return resArr.subarray(0, resPos + 1);
+        // else // IE 6-9
+        resArr.length = resPos + 1; // trim off extra weight
+        return resArr;
+    };
+    TextEncoder.prototype.toString = function(){return "[object TextEncoder]"};
+    try { // Object.defineProperty only works on DOM prototypes in IE8
+        Object.defineProperty(TextEncoder.prototype,"encoding",{
+            get:function(){if(TextEncoder.prototype.isPrototypeOf(this)) return"utf-8";
+                           else throw TypeError("Illegal invocation");}
+        });
+    } catch(e) { /*IE6-8 fallback*/ TextEncoder.prototype.encoding = "utf-8"; }
+    if(typeof Symbol!=="undefined")TextEncoder.prototype[Symbol.toStringTag]="TextEncoder";
+}
+
+ +

引用元: https://github.com/anonyco/FastestSmallestTextEncoderDecoder

+ +

仕様書

+ + + + + + + + + + + + + + + + +
仕様書状態備考
{{SpecName("Encoding", "#interface-textencoder", "TextEncoder")}}{{Spec2("Encoding")}}初回定義
+ +

ブラウザーの互換性

+ +
+ + +

{{Compat("api.TextEncoder")}}

+
+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf