import { BitArray , IllegalStateException } from '@zxing/library' ; import StringBuilder from '../../../../../core/util/StringBuilder' ; const ONE = RegExp ( '1' ) ; const ZERO = RegExp ( '0' ) ; const SPACE = RegExp ( ' ' ) ; author Pablo Orduña, University of Deusto (pablo.orduna deusto export default class BinaryUtil { private constructor ( ) { } public static buildBitArrayFromString ( data ) { let dotsAndXs = data . replace ( ONE , 'X' ) . replace ( ZERO , '.' ) ; let binary = new BitArray ( dotsAndXs . replace ( SPACE , '' ) . length ) ; let counter = 0 ; for ( let i = 0 ; i < dotsAndXs . length ; ++ i ) { if ( i % 9 === 0 ) { if ( dotsAndXs . charAt ( i ) !== ' ' ) { throw new IllegalStateException ( 'space expected' ) ; } continue ; } let currentChar = dotsAndXs . charAt ( i ) ; if ( currentChar === 'X' || currentChar === 'x' ) { binary . set ( counter ) ; } counter ++ ; } return binary ; } public static buildBitArrayFromStringWithoutSpaces ( data ) { let sb = new StringBuilder ( ) ; let dotsAndXs = data . replace ( ONE , 'X' ) . replace ( ZERO , '.' ) ; let current = 0 ; while ( current < dotsAndXs . length ) { sb . append ( ' ' ) ; for ( let i = 0 ; i < 8 && current < dotsAndXs . length ; ++ i ) { sb . append ( dotsAndXs . charAt ( current ) ) ; current ++ ; } } return this . buildBitArrayFromString ( sb . toString ( ) ) ; } }