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

remove unused ArrayBuffer <-> UTF8 string converters #390

Merged
merged 2 commits into from
Apr 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions src/arraybuffers/arraybuffers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ var array2 = uint8Array2.buffer;
var string2 = '\x00\x02\x81\x80\x00\x01\x00\x05\x00';
var hexString2 = '0.2.81.80.0.1.0.5.0';

var uint8Array3 = new Uint8Array(
[0xE5, 0xA4, 0xA7, 0xE7, 0xBA, 0xAA, 0xE5, 0x85, 0x83]);
var array3 = uint8Array3.buffer;
var string3 = '大纪元';

var uint8Array12 = new Uint8Array(
[12,118,101,114,105,115,0,2,129,128,0,1,0,5,0]);
var array12 = uint8Array12.buffer;
Expand Down Expand Up @@ -142,36 +137,3 @@ describe('ArrayBuffers <-> strings', function() {
.not.toEqual(string2);
});
});

describe('ArrayBuffers(UTF8) <-> strings', function() {
it('Empty Buffer -> Empty String', function() {
expect(arraybuffers.arrayBufferDecodedAsUtf8String(emptyArray)).toEqual(emptyString);
});
it('Empty String -> Empty Buffer', function() {
expect(arraybuffers.stringToUtf8EncodedArrayBuffer(emptyString).byteLength).toEqual(0);
});

it('Buffer(UTF8) -> String', function() {
expect(arraybuffers.arrayBufferDecodedAsUtf8String(emptyArray)).toEqual(emptyString);
expect(arraybuffers.arrayBufferDecodedAsUtf8String(array1)).toEqual(string1);
expect(arraybuffers.arrayBufferDecodedAsUtf8String(array3)).toEqual(string3);
});

it('String -> Buffer(UTF8)', function() {
expect(arraybuffers.byteEquality(
arraybuffers.stringToUtf8EncodedArrayBuffer(emptyString), emptyArray)).toBe(true);
expect(arraybuffers.byteEquality(
arraybuffers.stringToUtf8EncodedArrayBuffer(string1), array1)).toBe(true);
expect(arraybuffers.byteEquality(
arraybuffers.stringToUtf8EncodedArrayBuffer(string3), array3)).toBe(true);
});

it('String -> Buffer(UTF8) -> String = identity', function() {
expect(arraybuffers.arrayBufferDecodedAsUtf8String(
arraybuffers.stringToUtf8EncodedArrayBuffer(emptyString))).toEqual(emptyString);
expect(arraybuffers.arrayBufferDecodedAsUtf8String(
arraybuffers.stringToUtf8EncodedArrayBuffer(string1))).toEqual(string1);
expect(arraybuffers.arrayBufferDecodedAsUtf8String(
arraybuffers.stringToUtf8EncodedArrayBuffer(string3))).toEqual(string3);
});
});
27 changes: 0 additions & 27 deletions src/arraybuffers/arraybuffers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ export function stringToArrayBuffer(s:string) :ArrayBuffer {
return buffer;
}

// Escape and unescape are actually globally defined functions.
declare function escape(s:string):string;
declare function unescape(s:string):string;

// Converts an ArrayBuffer to a string of hex codes (of the regexp form
// /(hh\.)*hh/).
export function arrayBufferToHexString(buffer:ArrayBuffer) :string {
Expand All @@ -98,29 +94,6 @@ export function hexStringToArrayBuffer(hexString:string) :ArrayBuffer {
return buffer;
}

// Converts arrayBuffer which has a string encoded in UTF8 to a
// Javascript string.
//
// Note: the array buffer should have a valid string with no zero inside.
export function arrayBufferDecodedAsUtf8String(buffer:ArrayBuffer) :string {
var bytes = new Uint8Array(buffer);
var a :string[] = [];
for (var i = 0; i < bytes.length; ++i) {
a.push(String.fromCharCode(bytes[i]));
}
return decodeURIComponent(escape(a.join('')));
}

// Converts javascript string to array buffer using UTF8 encoding.
export function stringToUtf8EncodedArrayBuffer(str:string) :ArrayBuffer {
var strUtf8 = unescape(encodeURIComponent(str));
var ab = new Uint8Array(strUtf8.length);
for (var i = 0; i < strUtf8.length; i++) {
ab[i] = strUtf8.charCodeAt(i);
}
return ab.buffer;
}

// Returns an ArrayBuffer backed by the same memory as the supplied
// Node.js Buffer.
export function bufferToArrayBuffer(buffer: Buffer): ArrayBuffer {
Expand Down