summaryrefslogtreecommitdiff
path: root/js/blob.ts
diff options
context:
space:
mode:
authorTomohito Nakayama <nkym.tmht@hotmail.co.jp>2019-08-01 23:04:39 +0900
committerRyan Dahl <ry@tinyclouds.org>2019-08-01 10:04:39 -0400
commitdeec1b9b97011d1a0ef7312c0a3efde21186b82d (patch)
tree9093e51b246ee439ba02b3bd39b7a84f32e2f074 /js/blob.ts
parent3971dcfe10b94e901a224b5328a9dafd1e2ecc08 (diff)
Implement function convertLineEndingsToNative in blob.ts (#2695)
based on https://w3c.github.io/FileAPI/#convert-line-endings-to-native
Diffstat (limited to 'js/blob.ts')
-rw-r--r--js/blob.ts50
1 files changed, 47 insertions, 3 deletions
diff --git a/js/blob.ts b/js/blob.ts
index 7b4d23c46..f2668b642 100644
--- a/js/blob.ts
+++ b/js/blob.ts
@@ -2,13 +2,57 @@
import * as domTypes from "./dom_types";
import { containsOnlyASCII, hasOwnProperty } from "./util";
import { TextEncoder } from "./text_encoding";
+import { build } from "./build";
export const bytesSymbol = Symbol("bytes");
function convertLineEndingsToNative(s: string): string {
- // TODO(qti3e) Implement convertLineEndingsToNative.
- // https://w3c.github.io/FileAPI/#convert-line-endings-to-native
- return s;
+ let nativeLineEnd = build.os == "win" ? "\r\n" : "\n";
+
+ let position = 0;
+
+ let collectionResult = collectSequenceNotCRLF(s, position);
+
+ let token = collectionResult.collected;
+ position = collectionResult.newPosition;
+
+ let result = token;
+
+ while (position < s.length) {
+ let c = s.charAt(position);
+ if (c == "\r") {
+ result += nativeLineEnd;
+ position++;
+ if (position < s.length && s.charAt(position) == "\n") {
+ position++;
+ }
+ } else if (c == "\n") {
+ position++;
+ result += nativeLineEnd;
+ }
+
+ collectionResult = collectSequenceNotCRLF(s, position);
+
+ token = collectionResult.collected;
+ position = collectionResult.newPosition;
+
+ result += token;
+ }
+
+ return result;
+}
+
+function collectSequenceNotCRLF(
+ s: string,
+ position: number
+): { collected: string; newPosition: number } {
+ const start = position;
+ for (
+ let c = s.charAt(position);
+ position < s.length && !(c == "\r" || c == "\n");
+ c = s.charAt(++position)
+ );
+ return { collected: s.slice(start, position), newPosition: position };
}
function toUint8Arrays(