Skip to content

Commit

Permalink
Add CRLF ending for output file. Add BOM to output file.
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxShoshin committed Dec 23, 2018
1 parent 280ef19 commit 029621f
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/app/result-viewer/result-viewer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class ResultViewerComponent {
public download(element: any, fileName: string) {
const contentType = 'application/octet-stream';

const content = this.encodeUTF8(element.textContent);
const content = this.encodeUTF8BomCrlf(element.textContent);

var a = document.createElement('a');
var blob = new Blob([content], {'type': contentType});
Expand All @@ -39,12 +39,20 @@ export class ResultViewerComponent {
return utcDate.format('YYYY-MM-DDTHH:mm:ss');
}

private encodeUTF8(s: string) {
var i = 0;
private encodeUTF8BomCrlf(s: string) {
var i = 3;
var bytes = new Uint8Array(s.length * 4);

bytes[0] = 0xEF;
bytes[1] = 0xBB;
bytes[2] = 0xBF;

for (var ci = 0; ci != s.length; ci++) {
var c = s.charCodeAt(ci);
if (c < 128) {
if (c === 0x0A && ci !== 0 && s.charCodeAt(ci - 1) !== 0x0D) {
bytes[i++] = 0x0D;
}
bytes[i++] = c;
continue;
}
Expand Down

0 comments on commit 029621f

Please sign in to comment.