// Compiled by ClojureScript 1.9.562 {}
goog.provide('octet.spec.string');
goog.require('cljs.core');
goog.require('octet.buffer');
goog.require('octet.spec');
/**
 * Given a byte array, returns a number of bytes
 *   allocated with zero padding (zero byte).
 */
octet.spec.string.zeropad_count = (function octet$spec$string$zeropad_count(input){
var mark = (0);
return cljs.core.reduce.call(null,((function (mark){
return (function (sum,index){
var value = (input[index]);
if(cljs.core._EQ_.call(null,value,mark)){
return (sum + (1));
} else {
return cljs.core.reduced.call(null,sum);
}
});})(mark))
,(0),cljs.core.reverse.call(null,cljs.core.range.call(null,cljs.core.count.call(null,input))));
});
octet.spec.string.zeropad_BANG_ = (function octet$spec$string$zeropad_BANG_(input,datalength){
var seq__34356 = cljs.core.seq.call(null,cljs.core.range.call(null,input.length));
var chunk__34357 = null;
var count__34358 = (0);
var i__34359 = (0);
while(true){
if((i__34359 < count__34358)){
var i = cljs.core._nth.call(null,chunk__34357,i__34359);
if((i > datalength)){
(input[i] = (0));
} else {
}

var G__34368 = seq__34356;
var G__34369 = chunk__34357;
var G__34370 = count__34358;
var G__34371 = (i__34359 + (1));
seq__34356 = G__34368;
chunk__34357 = G__34369;
count__34358 = G__34370;
i__34359 = G__34371;
continue;
} else {
var temp__5278__auto__ = cljs.core.seq.call(null,seq__34356);
if(temp__5278__auto__){
var seq__34356__$1 = temp__5278__auto__;
if(cljs.core.chunked_seq_QMARK_.call(null,seq__34356__$1)){
var c__24249__auto__ = cljs.core.chunk_first.call(null,seq__34356__$1);
var G__34372 = cljs.core.chunk_rest.call(null,seq__34356__$1);
var G__34373 = c__24249__auto__;
var G__34374 = cljs.core.count.call(null,c__24249__auto__);
var G__34375 = (0);
seq__34356 = G__34372;
chunk__34357 = G__34373;
count__34358 = G__34374;
i__34359 = G__34375;
continue;
} else {
var i = cljs.core.first.call(null,seq__34356__$1);
if((i > datalength)){
(input[i] = (0));
} else {
}

var G__34376 = cljs.core.next.call(null,seq__34356__$1);
var G__34377 = null;
var G__34378 = (0);
var G__34379 = (0);
seq__34356 = G__34376;
chunk__34357 = G__34377;
count__34358 = G__34378;
i__34359 = G__34379;
continue;
}
} else {
return null;
}
}
break;
}
});

octet.spec.string.bytes__GT_string = (function octet$spec$string$bytes__GT_string(input,length){
var view = input.subarray((0),length);
var view__$1 = (new Uint8Array(view));
var fcc = String.fromCharCode;
return fcc.apply(null,view__$1);
});

octet.spec.string.string__GT_bytes = (function octet$spec$string$string__GT_bytes(value){
var buff = (new ArrayBuffer(cljs.core.count.call(null,value)));
var view = (new Uint8Array(buff));
var seq__34364_34380 = cljs.core.seq.call(null,cljs.core.range.call(null,cljs.core.count.call(null,value)));
var chunk__34365_34381 = null;
var count__34366_34382 = (0);
var i__34367_34383 = (0);
while(true){
if((i__34367_34383 < count__34366_34382)){
var i_34384 = cljs.core._nth.call(null,chunk__34365_34381,i__34367_34383);
(view[i_34384] = value.charCodeAt(i_34384));

var G__34385 = seq__34364_34380;
var G__34386 = chunk__34365_34381;
var G__34387 = count__34366_34382;
var G__34388 = (i__34367_34383 + (1));
seq__34364_34380 = G__34385;
chunk__34365_34381 = G__34386;
count__34366_34382 = G__34387;
i__34367_34383 = G__34388;
continue;
} else {
var temp__5278__auto___34389 = cljs.core.seq.call(null,seq__34364_34380);
if(temp__5278__auto___34389){
var seq__34364_34390__$1 = temp__5278__auto___34389;
if(cljs.core.chunked_seq_QMARK_.call(null,seq__34364_34390__$1)){
var c__24249__auto___34391 = cljs.core.chunk_first.call(null,seq__34364_34390__$1);
var G__34392 = cljs.core.chunk_rest.call(null,seq__34364_34390__$1);
var G__34393 = c__24249__auto___34391;
var G__34394 = cljs.core.count.call(null,c__24249__auto___34391);
var G__34395 = (0);
seq__34364_34380 = G__34392;
chunk__34365_34381 = G__34393;
count__34366_34382 = G__34394;
i__34367_34383 = G__34395;
continue;
} else {
var i_34396 = cljs.core.first.call(null,seq__34364_34390__$1);
(view[i_34396] = value.charCodeAt(i_34396));

var G__34397 = cljs.core.next.call(null,seq__34364_34390__$1);
var G__34398 = null;
var G__34399 = (0);
var G__34400 = (0);
seq__34364_34380 = G__34397;
chunk__34365_34381 = G__34398;
count__34366_34382 = G__34399;
i__34367_34383 = G__34400;
continue;
}
} else {
}
}
break;
}

return (new Int8Array(buff));
});

octet.spec.string.arraycopy = (function octet$spec$string$arraycopy(input,output,length){
return cljs.core.reduce.call(null,(function (_,i){
return (output[i] = (input[i]));
}),null,cljs.core.range.call(null,input.length));
});

octet.spec.string.byte_array = (function octet$spec$string$byte_array(length){
return (new Int8Array(length));
});

Int8Array.prototype.cljs$core$ICounted$ = cljs.core.PROTOCOL_SENTINEL;

Int8Array.prototype.cljs$core$ICounted$_count$arity$1 = (function (s){
var s__$1 = this;
return s__$1.length;
});
/**
 * Fixed length string type spec constructor.
 */
octet.spec.string.string = (function octet$spec$string$string(size){
if(typeof octet.spec.string.t_octet$spec$string34404 !== 'undefined'){
} else {

/**
* @constructor
 * @implements {octet.spec.ISpec}
 * @implements {cljs.core.IMeta}
 * @implements {cljs.core.IWithMeta}
 * @implements {octet.spec.ISpecSize}
*/
octet.spec.string.t_octet$spec$string34404 = (function (size,meta34405){
this.size = size;
this.meta34405 = meta34405;
this.cljs$lang$protocol_mask$partition0$ = 393216;
this.cljs$lang$protocol_mask$partition1$ = 0;
})
octet.spec.string.t_octet$spec$string34404.prototype.cljs$core$IWithMeta$_with_meta$arity$2 = (function (_34406,meta34405__$1){
var self__ = this;
var _34406__$1 = this;
return (new octet.spec.string.t_octet$spec$string34404(self__.size,meta34405__$1));
});

octet.spec.string.t_octet$spec$string34404.prototype.cljs$core$IMeta$_meta$arity$1 = (function (_34406){
var self__ = this;
var _34406__$1 = this;
return self__.meta34405;
});

octet.spec.string.t_octet$spec$string34404.prototype.octet$spec$ISpecSize$ = cljs.core.PROTOCOL_SENTINEL;

octet.spec.string.t_octet$spec$string34404.prototype.octet$spec$ISpecSize$size$arity$1 = (function (_){
var self__ = this;
var ___$1 = this;
return self__.size;
});

octet.spec.string.t_octet$spec$string34404.prototype.octet$spec$ISpec$ = cljs.core.PROTOCOL_SENTINEL;

octet.spec.string.t_octet$spec$string34404.prototype.octet$spec$ISpec$read$arity$3 = (function (_,buff,pos){
var self__ = this;
var ___$1 = this;
var rawdata = octet.buffer.read_bytes.call(null,buff,pos,self__.size);
var length = (self__.size - octet.spec.string.zeropad_count.call(null,rawdata));
var data = octet.spec.string.bytes__GT_string.call(null,rawdata,length);
return new cljs.core.PersistentVector(null, 2, 5, cljs.core.PersistentVector.EMPTY_NODE, [self__.size,data], null);
});

octet.spec.string.t_octet$spec$string34404.prototype.octet$spec$ISpec$write$arity$4 = (function (_,buff,pos,value){
var self__ = this;
var ___$1 = this;
var input = octet.spec.string.string__GT_bytes.call(null,value);
var length = cljs.core.count.call(null,input);
var tmpbuf = octet.spec.string.byte_array.call(null,self__.size);
if((length < self__.size)){
octet.spec.string.arraycopy.call(null,input,tmpbuf,length);
} else {
octet.spec.string.arraycopy.call(null,input,tmpbuf,self__.size);
}

if((length < self__.size)){
octet.spec.string.zeropad_BANG_.call(null,tmpbuf,length);
} else {
}

octet.buffer.write_bytes.call(null,buff,pos,self__.size,tmpbuf);

return self__.size;
});

octet.spec.string.t_octet$spec$string34404.getBasis = (function (){
return new cljs.core.PersistentVector(null, 2, 5, cljs.core.PersistentVector.EMPTY_NODE, [new cljs.core.Symbol(null,"size","size",-1555742762,null),new cljs.core.Symbol(null,"meta34405","meta34405",1579282170,null)], null);
});

octet.spec.string.t_octet$spec$string34404.cljs$lang$type = true;

octet.spec.string.t_octet$spec$string34404.cljs$lang$ctorStr = "octet.spec.string/t_octet$spec$string34404";

octet.spec.string.t_octet$spec$string34404.cljs$lang$ctorPrWriter = (function (this__23995__auto__,writer__23996__auto__,opt__23997__auto__){
return cljs.core._write.call(null,writer__23996__auto__,"octet.spec.string/t_octet$spec$string34404");
});

octet.spec.string.__GT_t_octet$spec$string34404 = (function octet$spec$string$string_$___GT_t_octet$spec$string34404(size__$1,meta34405){
return (new octet.spec.string.t_octet$spec$string34404(size__$1,meta34405));
});

}

return (new octet.spec.string.t_octet$spec$string34404(size,cljs.core.PersistentArrayMap.EMPTY));
});
/**
 * Arbitrary length string type spec.
 */
octet.spec.string.string_STAR_ = (function (){
if(typeof octet.spec.string.t_octet$spec$string34407 !== 'undefined'){
} else {

/**
* @constructor
 * @implements {cljs.core.IFn}
 * @implements {octet.spec.ISpecDynamicSize}
 * @implements {octet.spec.ISpec}
 * @implements {cljs.core.IMeta}
 * @implements {cljs.core.IWithMeta}
*/
octet.spec.string.t_octet$spec$string34407 = (function (meta34408){
this.meta34408 = meta34408;
this.cljs$lang$protocol_mask$partition0$ = 393217;
this.cljs$lang$protocol_mask$partition1$ = 0;
})
octet.spec.string.t_octet$spec$string34407.prototype.cljs$core$IWithMeta$_with_meta$arity$2 = (function (_34409,meta34408__$1){
var self__ = this;
var _34409__$1 = this;
return (new octet.spec.string.t_octet$spec$string34407(meta34408__$1));
});

octet.spec.string.t_octet$spec$string34407.prototype.cljs$core$IMeta$_meta$arity$1 = (function (_34409){
var self__ = this;
var _34409__$1 = this;
return self__.meta34408;
});

octet.spec.string.t_octet$spec$string34407.prototype.call = (function (self__){
var self__ = this;
var self____$1 = this;
var s = self____$1;
return s;
});

octet.spec.string.t_octet$spec$string34407.prototype.apply = (function (self__,args34410){
var self__ = this;
var self____$1 = this;
return self____$1.call.apply(self____$1,[self____$1].concat(cljs.core.aclone.call(null,args34410)));
});

octet.spec.string.t_octet$spec$string34407.prototype.cljs$core$IFn$_invoke$arity$0 = (function (){
var self__ = this;
var s = this;
return s;
});

octet.spec.string.t_octet$spec$string34407.prototype.octet$spec$ISpecDynamicSize$ = cljs.core.PROTOCOL_SENTINEL;

octet.spec.string.t_octet$spec$string34407.prototype.octet$spec$ISpecDynamicSize$size_STAR_$arity$2 = (function (_,data){
var self__ = this;
var ___$1 = this;
var data__$1 = octet.spec.string.string__GT_bytes.call(null,data);
return ((4) + cljs.core.count.call(null,data__$1));
});

octet.spec.string.t_octet$spec$string34407.prototype.octet$spec$ISpec$ = cljs.core.PROTOCOL_SENTINEL;

octet.spec.string.t_octet$spec$string34407.prototype.octet$spec$ISpec$read$arity$3 = (function (_,buff,pos){
var self__ = this;
var ___$1 = this;
var datasize = octet.buffer.read_int.call(null,buff,pos);
var data = octet.buffer.read_bytes.call(null,buff,(pos + (4)),datasize);
var data__$1 = octet.spec.string.bytes__GT_string.call(null,data,datasize);
return new cljs.core.PersistentVector(null, 2, 5, cljs.core.PersistentVector.EMPTY_NODE, [(datasize + (4)),data__$1], null);
});

octet.spec.string.t_octet$spec$string34407.prototype.octet$spec$ISpec$write$arity$4 = (function (_,buff,pos,value){
var self__ = this;
var ___$1 = this;
var input = octet.spec.string.string__GT_bytes.call(null,value);
var length = cljs.core.count.call(null,input);
octet.buffer.write_int.call(null,buff,pos,length);

octet.buffer.write_bytes.call(null,buff,(pos + (4)),length,input);

return (length + (4));
});

octet.spec.string.t_octet$spec$string34407.getBasis = (function (){
return new cljs.core.PersistentVector(null, 1, 5, cljs.core.PersistentVector.EMPTY_NODE, [new cljs.core.Symbol(null,"meta34408","meta34408",-57507363,null)], null);
});

octet.spec.string.t_octet$spec$string34407.cljs$lang$type = true;

octet.spec.string.t_octet$spec$string34407.cljs$lang$ctorStr = "octet.spec.string/t_octet$spec$string34407";

octet.spec.string.t_octet$spec$string34407.cljs$lang$ctorPrWriter = (function (this__23995__auto__,writer__23996__auto__,opt__23997__auto__){
return cljs.core._write.call(null,writer__23996__auto__,"octet.spec.string/t_octet$spec$string34407");
});

octet.spec.string.__GT_t_octet$spec$string34407 = (function octet$spec$string$__GT_t_octet$spec$string34407(meta34408){
return (new octet.spec.string.t_octet$spec$string34407(meta34408));
});

}

return (new octet.spec.string.t_octet$spec$string34407(cljs.core.PersistentArrayMap.EMPTY));
})()
;
