@@ -1815,4 +1815,81 @@ module ts {
1815
1815
export function getLocalSymbolForExportDefault ( symbol : Symbol ) {
1816
1816
return symbol && symbol . valueDeclaration && ( symbol . valueDeclaration . flags & NodeFlags . Default ) ? symbol . valueDeclaration . localSymbol : undefined ;
1817
1817
}
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
+ }
1818
1895
}
0 commit comments