|
17 | 17 | import { debugAssert, fail } from '../util/assert';
|
18 | 18 | import { ByteString } from '../util/byte_string';
|
19 | 19 |
|
| 20 | +/** These constants are taken from the backend. */ |
| 21 | +const MIN_SURROGATE = '\uD800'; |
| 22 | +const MAX_SURROGATE = '\uDBFF'; |
| 23 | + |
| 24 | +const ESCAPE1 = 0x00; |
| 25 | +const NULL_BYTE = 0xff; // Combined with ESCAPE1 |
| 26 | +const SEPARATOR = 0x01; // Combined with ESCAPE1 |
| 27 | + |
| 28 | +const ESCAPE2 = 0xff; |
| 29 | +const FF_BYTE = 0x00; // Combined with ESCAPE2 |
| 30 | + |
20 | 31 | const LONG_SIZE = 64;
|
21 | 32 | const BYTE_SIZE = 8;
|
22 | 33 |
|
@@ -100,6 +111,54 @@ export class OrderedCodeWriter {
|
100 | 111 | buffer = new Uint8Array(DEFAULT_BUFFER_SIZE);
|
101 | 112 | position = 0;
|
102 | 113 |
|
| 114 | + /** Writes utf8 bytes into this byte sequence, ascending. */ |
| 115 | + writeUtf8Ascending(sequence: string): void { |
| 116 | + for (const c of sequence) { |
| 117 | + const charCode = c.charCodeAt(0); |
| 118 | + if (charCode < 0x80) { |
| 119 | + this.writeByteAscending(charCode); |
| 120 | + } else if (charCode < 0x800) { |
| 121 | + this.writeByteAscending((0x0f << 6) | (charCode >>> 6)); |
| 122 | + this.writeByteAscending(0x80 | (0x3f & charCode)); |
| 123 | + } else if (c < MIN_SURROGATE || MAX_SURROGATE < c) { |
| 124 | + this.writeByteAscending((0x0f << 5) | (charCode >>> 12)); |
| 125 | + this.writeByteAscending(0x80 | (0x3f & (charCode >>> 6))); |
| 126 | + this.writeByteAscending(0x80 | (0x3f & charCode)); |
| 127 | + } else { |
| 128 | + const codePoint = c.codePointAt(0)!; |
| 129 | + this.writeByteAscending((0x0f << 4) | (codePoint >>> 18)); |
| 130 | + this.writeByteAscending(0x80 | (0x3f & (codePoint >>> 12))); |
| 131 | + this.writeByteAscending(0x80 | (0x3f & (codePoint >>> 6))); |
| 132 | + this.writeByteAscending(0x80 | (0x3f & codePoint)); |
| 133 | + } |
| 134 | + } |
| 135 | + this.writeSeparatorAscending(); |
| 136 | + } |
| 137 | + |
| 138 | + /** Writes utf8 bytes into this byte sequence, descending */ |
| 139 | + writeUtf8Descending(sequence: string): void { |
| 140 | + for (const c of sequence) { |
| 141 | + const charCode = c.charCodeAt(0); |
| 142 | + if (charCode < 0x80) { |
| 143 | + this.writeByteDescending(charCode); |
| 144 | + } else if (charCode < 0x800) { |
| 145 | + this.writeByteDescending((0x0f << 6) | (charCode >>> 6)); |
| 146 | + this.writeByteDescending(0x80 | (0x3f & charCode)); |
| 147 | + } else if (c < MIN_SURROGATE || MAX_SURROGATE < c) { |
| 148 | + this.writeByteDescending((0x0f << 5) | (charCode >>> 12)); |
| 149 | + this.writeByteDescending(0x80 | (0x3f & (charCode >>> 6))); |
| 150 | + this.writeByteDescending(0x80 | (0x3f & charCode)); |
| 151 | + } else { |
| 152 | + const codePoint = c.codePointAt(0)!; |
| 153 | + this.writeByteDescending((0x0f << 4) | (codePoint >>> 18)); |
| 154 | + this.writeByteDescending(0x80 | (0x3f & (codePoint >>> 12))); |
| 155 | + this.writeByteDescending(0x80 | (0x3f & (codePoint >>> 6))); |
| 156 | + this.writeByteDescending(0x80 | (0x3f & codePoint)); |
| 157 | + } |
| 158 | + } |
| 159 | + this.writeSeparatorDescending(); |
| 160 | + } |
| 161 | + |
103 | 162 | writeNumberAscending(val: number): void {
|
104 | 163 | // Values are encoded with a single byte length prefix, followed by the
|
105 | 164 | // actual value in big-endian format with leading 0 bytes dropped.
|
@@ -155,19 +214,59 @@ export class OrderedCodeWriter {
|
155 | 214 | return this.buffer.slice(0, this.position);
|
156 | 215 | }
|
157 | 216 |
|
158 |
| - writeBytesAscending(value: ByteString): void { |
159 |
| - fail('Not implemented'); |
| 217 | + /** Writes a single byte ascending to the buffer. */ |
| 218 | + private writeByteAscending(b: number): void { |
| 219 | + const masked = b & 0xff; |
| 220 | + if (masked === ESCAPE1) { |
| 221 | + this.writeEscapedByteAscending(ESCAPE1); |
| 222 | + this.writeEscapedByteAscending(NULL_BYTE); |
| 223 | + } else if (masked === ESCAPE2) { |
| 224 | + this.writeEscapedByteAscending(ESCAPE2); |
| 225 | + this.writeEscapedByteAscending(FF_BYTE); |
| 226 | + } else { |
| 227 | + this.writeEscapedByteAscending(masked); |
| 228 | + } |
160 | 229 | }
|
161 | 230 |
|
162 |
| - writeBytesDescending(value: ByteString): void { |
163 |
| - fail('Not implemented'); |
| 231 | + /** Writes a single byte descending to the buffer. */ |
| 232 | + private writeByteDescending(b: number): void { |
| 233 | + const masked = b & 0xff; |
| 234 | + if (masked === ESCAPE1) { |
| 235 | + this.writeEscapedByteDescending(ESCAPE1); |
| 236 | + this.writeEscapedByteDescending(NULL_BYTE); |
| 237 | + } else if (masked === ESCAPE2) { |
| 238 | + this.writeEscapedByteDescending(ESCAPE2); |
| 239 | + this.writeEscapedByteDescending(FF_BYTE); |
| 240 | + } else { |
| 241 | + this.writeEscapedByteDescending(b); |
| 242 | + } |
164 | 243 | }
|
165 | 244 |
|
166 |
| - writeUtf8Ascending(sequence: string): void { |
| 245 | + private writeSeparatorAscending(): void { |
| 246 | + this.writeEscapedByteAscending(ESCAPE1); |
| 247 | + this.writeEscapedByteAscending(SEPARATOR); |
| 248 | + } |
| 249 | + |
| 250 | + private writeSeparatorDescending(): void { |
| 251 | + this.writeEscapedByteDescending(ESCAPE1); |
| 252 | + this.writeEscapedByteDescending(SEPARATOR); |
| 253 | + } |
| 254 | + |
| 255 | + private writeEscapedByteAscending(b: number): void { |
| 256 | + this.ensureAvailable(1); |
| 257 | + this.buffer[this.position++] = b; |
| 258 | + } |
| 259 | + |
| 260 | + private writeEscapedByteDescending(b: number): void { |
| 261 | + this.ensureAvailable(1); |
| 262 | + this.buffer[this.position++] = ~b; |
| 263 | + } |
| 264 | + |
| 265 | + writeBytesAscending(value: ByteString): void { |
167 | 266 | fail('Not implemented');
|
168 | 267 | }
|
169 | 268 |
|
170 |
| - writeUtf8Descending(sequence: string): void { |
| 269 | + writeBytesDescending(value: ByteString): void { |
171 | 270 | fail('Not implemented');
|
172 | 271 | }
|
173 | 272 |
|
|
0 commit comments