15
15
* limitations under the License.
16
16
*/
17
17
import { debugAssert } from '../util/assert' ;
18
-
18
+ import { ByteString } from '../util/byte_string' ;
19
19
20
20
/** These constants are taken from the backend. */
21
21
const MIN_SURROGATE = '\uD800' ;
@@ -26,6 +26,7 @@ const NULL_BYTE = 0xff; // Combined with ESCAPE1
26
26
const SEPARATOR = 0x01 ; // Combined with ESCAPE1
27
27
28
28
const ESCAPE2 = 0xff ;
29
+ const INFINITY = 0xff ; // Combined with ESCAPE2
29
30
const FF_BYTE = 0x00 ; // Combined with ESCAPE2
30
31
31
32
const LONG_SIZE = 64 ;
@@ -77,7 +78,7 @@ export function numberOfLeadingZerosInByte(x: number): number {
77
78
/** Counts the number of leading zeros in the given byte array. */
78
79
function numberOfLeadingZeros ( bytes : Uint8Array ) : number {
79
80
debugAssert (
80
- bytes . length == 8 ,
81
+ bytes . length === 8 ,
81
82
'Can only count leading zeros in 64-bit numbers'
82
83
) ;
83
84
let leadingZeros = 0 ;
@@ -111,6 +112,26 @@ export class OrderedCodeWriter {
111
112
buffer = new Uint8Array ( DEFAULT_BUFFER_SIZE ) ;
112
113
position = 0 ;
113
114
115
+ writeBytesAscending ( value : ByteString ) : void {
116
+ const it = value [ Symbol . iterator ] ( ) ;
117
+ let byte = it . next ( ) ;
118
+ while ( ! byte . done ) {
119
+ this . writeByteAscending ( byte . value ) ;
120
+ byte = it . next ( ) ;
121
+ }
122
+ this . writeSeparatorAscending ( ) ;
123
+ }
124
+
125
+ writeBytesDescending ( value : ByteString ) : void {
126
+ const it = value [ Symbol . iterator ] ( ) ;
127
+ let byte = it . next ( ) ;
128
+ while ( ! byte . done ) {
129
+ this . writeByteDescending ( byte . value ) ;
130
+ byte = it . next ( ) ;
131
+ }
132
+ this . writeSeparatorDescending ( ) ;
133
+ }
134
+
114
135
/** Writes utf8 bytes into this byte sequence, ascending. */
115
136
writeUtf8Ascending ( sequence : string ) : void {
116
137
for ( const c of sequence ) {
@@ -183,6 +204,24 @@ export class OrderedCodeWriter {
183
204
}
184
205
}
185
206
207
+ /**
208
+ * Writes the "infinity" byte sequence that sorts after all other byte
209
+ * sequences written in ascending order.
210
+ */
211
+ writeInfinityAscending ( ) : void {
212
+ this . writeEscapedByteAscending ( ESCAPE2 ) ;
213
+ this . writeEscapedByteAscending ( INFINITY ) ;
214
+ }
215
+
216
+ /**
217
+ * Writes the "infinity" byte sequence that sorts before all other byte
218
+ * sequences written in descending order.
219
+ */
220
+ writeInfinityDescending ( ) : void {
221
+ this . writeEscapedByteDescending ( ESCAPE2 ) ;
222
+ this . writeEscapedByteDescending ( INFINITY ) ;
223
+ }
224
+
186
225
/**
187
226
* Encodes `val` into an encoding so that the order matches the IEEE 754
188
227
* floating-point comparison results with the following exceptions:
@@ -278,4 +317,10 @@ export class OrderedCodeWriter {
278
317
newBuffer . set ( this . buffer ) ; // copy old data
279
318
this . buffer = newBuffer ;
280
319
}
320
+
321
+ seed ( encodedBytes : Uint8Array ) : void {
322
+ this . ensureAvailable ( encodedBytes . length ) ;
323
+ this . buffer . set ( encodedBytes , this . position ) ;
324
+ this . position += encodedBytes . length ;
325
+ }
281
326
}
0 commit comments