11import { logError } from './log' ;
2- import { LongBits } from './LongBits' ;
3- import { VarNumberUtils } from './VarNumberUtils' ;
42
53const BIG : number = 4294967296 ;
64
@@ -20,24 +18,10 @@ type REleType = ByteType | RVoNewType | RVoCreateType;
2018
2119type WEleType = ByteType | WVoType ;
2220
23- const tmpLongBits = new LongBits ( 0 , 0 ) ;
24-
25- export function readBit ( ori : number , index = 0 , len = 4 ) : number {
26- let flag = 0 ;
27- for ( let i = 0 ; i < len ; i ++ ) {
28- flag |= 1 << i ;
29- }
30- return ( ori >> index ) & flag ;
31- }
32-
33- export function writeBit ( ori : number , value : number , index : number ) : number {
34- return ori | ( value << index ) ;
35- }
36-
3721export class ByteArray {
3822 private _len_ = 0 ;
3923
40- private _pos_ = 0 ;
24+ public _pos_ = 0 ;
4125
4226 private _byteView_ : Uint8Array = null as never ;
4327
@@ -422,88 +406,6 @@ export class ByteArray {
422406 this . _byteView_ . set ( uint8Array . subarray ( offset , offset + rlen ) , this . _pos_ ) ;
423407 this . _pos_ += rlen ;
424408 }
425-
426- readVarInt ( ) : number {
427- return VarNumberUtils . readInt ( this ) ;
428- }
429-
430- readVarLong ( ) : number {
431- return this . _readLongVarint ( ) . toNumber ( false ) ;
432- }
433-
434- readUnsignedVarLong ( ) : number {
435- return this . _readLongVarint ( ) . toNumber ( true ) ;
436- }
437-
438- writeVarInt ( value : number ) : void {
439- VarNumberUtils . writeInt ( value , this ) ;
440- }
441-
442- writeVarLong ( value : number ) : void {
443- const bits = tmpLongBits . setToNumber ( value ) ;
444- const length = bits . length ( ) ;
445- this . ensureWrite ( this . _pos_ + length ) ;
446- this . _writeVarint64 ( bits , this . _data_ , this . _pos_ ) ;
447- this . _pos_ += length ;
448- }
449-
450- private _readLongVarint ( ) : LongBits {
451- // tends to deopt with local vars for octet etc.
452- const bits = tmpLongBits . setTo ( 0 , 0 ) ;
453- const buf = this . _byteView_ ;
454- let i = 0 ;
455- if ( this . _len_ - this . _pos_ > 4 ) {
456- // fast route(lo)
457- for ( ; i < 4 ; i ++ ) {
458- // 1st.4th
459- bits . lo = ( bits . lo | ( ( buf [ this . _pos_ ] & 127 ) << ( i * 7 ) ) ) >>> 0 ;
460- if ( buf [ this . _pos_ ++ ] < 128 ) return bits ;
461- }
462- // 5th
463- bits . lo = ( bits . lo | ( ( buf [ this . _pos_ ] & 127 ) << 28 ) ) >>> 0 ;
464- bits . hi = ( bits . hi | ( ( buf [ this . _pos_ ] & 127 ) >> 4 ) ) >>> 0 ;
465- if ( buf [ this . _pos_ ++ ] < 128 ) return bits ;
466- i = 0 ;
467- } else {
468- for ( ; i < 3 ; i ++ ) {
469- /* istanbul ignore if */
470- if ( this . _pos_ >= this . _len_ ) throw new Error ( `indexOutOfRange` ) ;
471- //1st..3th
472- bits . lo = ( bits . lo | ( ( buf [ this . _pos_ ] & 127 ) << ( i * 7 ) ) ) >>> 0 ;
473- return bits ;
474- }
475- }
476- if ( this . _len_ - this . _pos_ > 4 ) {
477- // fast route (hi)
478- for ( ; i < 5 ; i ++ ) {
479- //6th..10th
480- bits . hi = ( bits . hi | ( ( buf [ this . _pos_ ] & 127 ) << ( i * 7 + 3 ) ) ) >>> 0 ;
481- if ( buf [ this . _pos_ ++ ] < 128 ) return bits ;
482- }
483- } else {
484- for ( ; i < 5 ; i ++ ) {
485- /* istanbul ignore if */
486- if ( this . _pos_ >= this . _len_ ) throw new Error ( `indexOutRange` ) ;
487- //6th..10th
488- bits . hi = ( bits . hi | ( ( buf [ this . _pos_ ] & 127 ) << ( i * 7 + 3 ) ) ) >>> 0 ;
489- if ( buf [ this . _pos_ ++ ] < 128 ) return bits ;
490- }
491- }
492- throw Error ( 'invalid varint encoding' ) ;
493- }
494-
495- private _writeVarint64 ( val : LongBits , buf : DataView , pos : number ) : void {
496- while ( val . hi ) {
497- buf . setInt8 ( pos ++ , ( val . lo & 127 ) | 128 ) ;
498- val . lo = ( ( val . lo >>> 7 ) | ( val . hi << 25 ) ) >>> 0 ;
499- val . hi >>>= 7 ;
500- }
501- while ( val . lo > 127 ) {
502- buf . setInt8 ( pos ++ , ( val . lo & 127 ) | 128 ) ;
503- val . lo >>>= 7 ;
504- }
505- buf . setInt8 ( pos ++ , val . lo ) ;
506- }
507409}
508410
509411const _byteReadFunc : Record < number , Function > = {
0 commit comments