Skip to content

Commit

Permalink
chore: translate to English
Browse files Browse the repository at this point in the history
  • Loading branch information
MH4GF committed Dec 19, 2024
1 parent 0e04fd3 commit 8a2a9c4
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions frontend/packages/erd-core/src/utils/compressionString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,54 @@ export async function compressToUTF16(input: string): Promise<string> {
const encoder = new TextEncoder()
const inputBytes = encoder.encode(input)

// Deflate圧縮
// Deflate compression
const compressionStream = new CompressionStream('deflate')
const compressedStream =
createReadableStreamFromBytes(inputBytes).pipeThrough(compressionStream)
const compressedBuffer = await new Response(compressedStream).arrayBuffer()
const compressedBytes = new Uint8Array(compressedBuffer)

// 圧縮後のバイト長を格納するため、先頭4バイトに長さ情報を入れる
// Store the length of the compressed bytes in the first 4 bytes
const length = compressedBytes.length
const totalLength = 4 + length // 最初の4バイトで長さを格納
// 2バイト(UTF-16コードユニット)境界に揃えるため、必要なら1バイトパディング
const totalLength = 4 + length // Store length in the first 4 bytes
// Align to 2-byte (UTF-16 code unit) boundary, add 1 byte padding if necessary
const paddedLength = totalLength % 2 === 0 ? totalLength : totalLength + 1

const resultBytes = new Uint8Array(paddedLength)
const dataView = new DataView(resultBytes.buffer)

// 先頭4バイトに長さ情報を格納 (Little Endian)
// Store length information in the first 4 bytes (Little Endian)
dataView.setUint32(0, length, true)
// その後に圧縮データ本体を格納
// Store the compressed data after the first 4 bytes
resultBytes.set(compressedBytes, 4)

// UTF-16コードユニットとして解釈するためにUint16Arrayに変換
// Convert to Uint16Array to interpret as UTF-16 code units
const uint16Array = new Uint16Array(resultBytes.buffer)

// 各Uint16要素をコードユニットとして文字列化
// Convert each Uint16 element to a character
return String.fromCharCode(...Array.from(uint16Array))
}

export async function decompressFromUTF16(input: string): Promise<string> {
const decoder = new TextDecoder()

// UTF-16コードユニット列をUint16Arrayとして再構成
// Reconstruct Uint16Array from UTF-16 code units
const codeUnits = new Uint16Array(input.length)
for (let i = 0; i < input.length; i++) {
codeUnits[i] = input.charCodeAt(i)
}

// Uint16ArrayからUint8Arrayを参照取得
// Reference Uint8Array from Uint16Array
const bytes = new Uint8Array(codeUnits.buffer)
const dataView = new DataView(bytes.buffer)

// 先頭4バイトから圧縮データの実際の長さを取得
// Get the actual length of the compressed data from the first 4 bytes
const length = dataView.getUint32(0, true)

// 圧縮データ部分を切り出し
// Extract the compressed data part
const compressedBytes = bytes.subarray(4, 4 + length)

// 解凍
// Decompress
const decompressionStream = new DecompressionStream('deflate')
const decompressedStream =
createReadableStreamFromBytes(compressedBytes).pipeThrough(
Expand Down

0 comments on commit 8a2a9c4

Please sign in to comment.