Skip to content

Commit 73e22ed

Browse files
committed
Add convertToBase64 method
1 parent 863f0b6 commit 73e22ed

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

Jakefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ var harnessSources = [
145145
"services/documentRegistry.ts",
146146
"services/preProcessFile.ts",
147147
"services/patternMatcher.ts",
148-
"versionCache.ts"
148+
"versionCache.ts",
149+
"convertToBase64.ts"
149150
].map(function (f) {
150151
return path.join(unittestsDirectory, f);
151152
})).concat([

src/compiler/utilities.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,4 +1815,81 @@ module ts {
18151815
export function getLocalSymbolForExportDefault(symbol: Symbol) {
18161816
return symbol && symbol.valueDeclaration && (symbol.valueDeclaration.flags & NodeFlags.Default) ? symbol.valueDeclaration.localSymbol : undefined;
18171817
}
1818+
1819+
/**
1820+
* Replace each instance of non-ascii characters by one, two, three, or four escape sequences
1821+
* representing the UTF-8 encoding of the character, and return the expanded char code list.
1822+
*/
1823+
function getExpandedCharCodes(input: string): number[] {
1824+
let output: number[] = [];
1825+
let length = input.length;
1826+
let leadSurrogate: number = undefined;
1827+
1828+
for (let i = 0; i < length; i++) {
1829+
let charCode = input.charCodeAt(i);
1830+
1831+
// handel utf8
1832+
if (charCode < 0x80) {
1833+
output.push(charCode);
1834+
}
1835+
else if (charCode < 0x800) {
1836+
output.push((charCode >> 6) | 0B11000000);
1837+
output.push((charCode & 0B00111111) | 0B10000000);
1838+
}
1839+
else if (charCode < 0x10000) {
1840+
output.push((charCode >> 12) | 0B11100000);
1841+
output.push(((charCode >> 6) & 0B00111111) | 0B10000000);
1842+
output.push((charCode & 0B00111111) | 0B10000000);
1843+
}
1844+
else if (charCode < 0x20000) {
1845+
output.push((charCode >> 18) | 0B11110000);
1846+
output.push(((charCode >> 12) & 0B00111111) | 0B10000000);
1847+
output.push(((charCode >> 6) & 0B00111111) | 0B10000000);
1848+
output.push((charCode & 0B00111111) | 0B10000000);
1849+
}
1850+
else {
1851+
Debug.assert(false, "Unexpected code point");
1852+
}
1853+
}
1854+
1855+
return output;
1856+
}
1857+
1858+
const base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
1859+
1860+
/**
1861+
* Converts a string to a base-64 encoded ASCII string.
1862+
*/
1863+
export function convertToBase64(input: string): string {
1864+
var result = "";
1865+
let charCodes = getExpandedCharCodes(input);
1866+
let i = 0;
1867+
let length = charCodes.length;
1868+
let byte1: number, byte2: number, byte3: number, byte4: number;
1869+
1870+
while (i < length) {
1871+
// Convert every 6-bits in the input 3 character points
1872+
// into a base64 digit
1873+
byte1 = charCodes[i] >> 2;
1874+
byte2 = (charCodes[i] & 0B00000011) << 4 | charCodes[i + 1] >> 4;
1875+
byte3 = (charCodes[i + 1] & 0B00001111) << 2 | charCodes[i + 2] >> 6;
1876+
byte4 = charCodes[i + 2] & 0B00111111;
1877+
1878+
// We are out of characters in the input, set the extra
1879+
// digits to 64 (padding character).
1880+
if (i + 1 >= length) {
1881+
byte3 = byte4 = 64;
1882+
}
1883+
else if (i + 2 >= length) {
1884+
byte4 = 64;
1885+
}
1886+
1887+
// Write to the ouput
1888+
result += base64Digits.charAt(byte1) + base64Digits.charAt(byte2) + base64Digits.charAt(byte3) + base64Digits.charAt(byte4);
1889+
1890+
i += 3;
1891+
}
1892+
1893+
return result;
1894+
}
18181895
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// <reference path="..\..\..\src\harness\harness.ts" />
2+
3+
module ts {
4+
describe('convertToBase64', () => {
5+
function runTest(input: string): void {
6+
var actual = ts.convertToBase64(input);
7+
var expected = new Buffer(input).toString("base64");
8+
assert.equal(actual, expected, "Encoded string using convertToBase64 does not match buffer.toString('base64')");
9+
}
10+
11+
it("Converts ASCII charaters correctelly", () => {
12+
runTest(" !\"#$ %&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~");
13+
});
14+
15+
it("Converts escape sequences correctelly", () => {
16+
runTest("\t\n\r\\\"\'\u0062");
17+
});
18+
19+
it("Converts simple unicode characters correctelly", () => {
20+
runTest("ΠΣ ٵپ औठ ⺐⺠");
21+
});
22+
23+
it("Converts simple code snippit correctelly", () => {
24+
runTest(`/// <reference path="file.ts" />
25+
var x: string = "string";
26+
console.log(x);`);
27+
});
28+
29+
it("Converts simple code snippit with unicode characters correctelly", () => {
30+
runTest(`var Π = 3.1415; console.log(Π);`);
31+
});
32+
});
33+
}

0 commit comments

Comments
 (0)