Skip to content

Commit

Permalink
feat!: breaking changes
Browse files Browse the repository at this point in the history
* deprecate UCS-2 methods
* reimplemented UTF-8 reading/writing
  • Loading branch information
Braden Lamb committed Jun 6, 2022
1 parent 00d9e4a commit a14a695
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 50 deletions.
22 changes: 8 additions & 14 deletions src/dynwriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,11 @@ export class DynWriter {
*
* @param {string} string Value
*
* @deprecated
* @returns {DynWriter}
*/
writeZTStringUCS2(string: string): DynWriter {
if (string) {
for (const char of string) {
const code = char.charCodeAt(0)
this.writeUint16(code)
}
}
this.writeUint16(0)
return this
return this.writeZTStringUTF8(string)
}

/**
Expand All @@ -198,13 +192,13 @@ export class DynWriter {
* @returns {DynWriter}
*/
writeZTStringUTF8(string: string): DynWriter {
if (string) {
for (const char of string) {
const code = char.charCodeAt(0)
this.writeUint8(code)
}
}
const escapedString = unescape(encodeURIComponent(string))

for (let i = 0, l = escapedString.length; i < l; i++)
this.writeUint8(escapedString.charCodeAt(i))

this.writeUint8(0)

return this
}

Expand Down
31 changes: 9 additions & 22 deletions src/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,11 @@ export class Reader {
/**
* Read the UCS-2 encoded string from the current position in the Buffer.
*
* @deprecated
* @returns {string}
*/
readZTStringUCS2(): string {
let index = this.offset
const array: number[] = []
while (index + 2 < this.length) {
const code = this.readUint16()
if (code === 0) break
else {
array.push(code)
index += 2
}
}
return array.reduce((s, c) => s + String.fromCharCode(c), '')
return this.readZTStringUTF8()
}

/**
Expand All @@ -202,16 +193,12 @@ export class Reader {
* @returns {string}
*/
readZTStringUTF8(): string {
let index = this.offset
const array: number[] = []
while (index + 1 < this.length) {
const code = this.readUint8()
if (code === 0) break
else {
array.push(code)
index += 1
}
}
return array.reduce((s, c) => s + String.fromCharCode(c), '')
let string = ''
let byte: number

while ((byte = this.readUint8()) !== 0)
string += String.fromCharCode(byte)

return decodeURIComponent(escape(string))
}
}
22 changes: 8 additions & 14 deletions src/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,11 @@ export class Writer {
*
* @param {string} string Value
*
* @deprecated
* @returns {Writer}
*/
writeZTStringUCS2(string: string): Writer {
if (string) {
for (const char of string) {
const code = char.charCodeAt(0)
this.writeUint16(code)
}
}
this.writeUint16(0)
return this
return this.writeZTStringUTF8(string)
}

/**
Expand All @@ -211,13 +205,13 @@ export class Writer {
* @returns {Writer}
*/
writeZTStringUTF8(string: string): Writer {
if (string) {
for (const char of string) {
const code = char.charCodeAt(0)
this.writeUint8(code)
}
}
const escapedString = unescape(encodeURIComponent(string))

for (let i = 0, l = escapedString.length; i < l; i++)
this.writeUint8(escapedString.charCodeAt(i))

this.writeUint8(0)

return this
}

Expand Down
16 changes: 16 additions & 0 deletions tests/dynwriter_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,18 @@ Deno.test('write a UCS-2 encoded string', () => {
writer.writeZTStringUCS2('test')
})

Deno.test('write a UCS-2 encoded string (unicode)', () => {
writer.writeZTStringUCS2('𝙩𝙚𝙨𝙩')
})

Deno.test('write a UTF-8 encoded string', () => {
writer.writeZTStringUTF8('test')
})

Deno.test('write a UTF-8 encoded string (unicode)', () => {
writer.writeZTStringUTF8('𝙩𝙚𝙨𝙩')
})

Deno.test('write to buffer', () => {
buffer = writer.finalize()
reader = new Reader(buffer)
Expand Down Expand Up @@ -103,6 +111,14 @@ Deno.test('read a UCS-2 encoded string', () => {
assertEquals(reader?.readZTStringUCS2(), 'test')
})

Deno.test('read a UCS-2 encoded string (unicode)', () => {
assertEquals(reader?.readZTStringUCS2(), '𝙩𝙚𝙨𝙩')
})

Deno.test('read a UTF-8 encoded string', () => {
assertEquals(reader?.readZTStringUTF8(), 'test')
})

Deno.test('read a UTF-8 encoded string (unicode)', () => {
assertEquals(reader?.readZTStringUTF8(), '𝙩𝙚𝙨𝙩')
})
16 changes: 16 additions & 0 deletions tests/writer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,18 @@ Deno.test('write a UCS-2 encoded string', () => {
writer.writeZTStringUCS2('test')
})

Deno.test('write a UCS-2 encoded string (unicode)', () => {
writer.writeZTStringUCS2('𝙩𝙚𝙨𝙩')
})

Deno.test('write a UTF-8 encoded string', () => {
writer.writeZTStringUTF8('test')
})

Deno.test('write a UTF-8 encoded string (unicode)', () => {
writer.writeZTStringUTF8('𝙩𝙚𝙨𝙩')
})

Deno.test('write to buffer', () => {
buffer = writer.finalize()
reader = new Reader(buffer)
Expand Down Expand Up @@ -103,6 +111,14 @@ Deno.test('read a UCS-2 encoded string', () => {
assertEquals(reader?.readZTStringUCS2(), 'test')
})

Deno.test('read a UCS-2 encoded string (unicode)', () => {
assertEquals(reader?.readZTStringUCS2(), '𝙩𝙚𝙨𝙩')
})

Deno.test('read a UTF-8 encoded string', () => {
assertEquals(reader?.readZTStringUTF8(), 'test')
})

Deno.test('read a UTF-8 encoded string (unicode)', () => {
assertEquals(reader?.readZTStringUTF8(), '𝙩𝙚𝙨𝙩')
})

0 comments on commit a14a695

Please sign in to comment.