// 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__34343 = cljs.core.seq.call(null,cljs.core.range.call(null,input.length));
var chunk__34344 = null;
var count__34345 = (0);
var i__34346 = (0);
while(true){
if((i__34346 < count__34345)){
var i = cljs.core._nth.call(null,chunk__34344,i__34346);
if((i > datalength)){
(input[i] = (0));
} else {
}

var G__34355 = seq__34343;
var G__34356 = chunk__34344;
var G__34357 = count__34345;
var G__34358 = (i__34346 + (1));
seq__34343 = G__34355;
chunk__34344 = G__34356;
count__34345 = G__34357;
i__34346 = G__34358;
continue;
} else {
var temp__5278__auto__ = cljs.core.seq.call(null,seq__34343);
if(temp__5278__auto__){
var seq__34343__$1 = temp__5278__auto__;
if(cljs.core.chunked_seq_QMARK_.call(null,seq__34343__$1)){
var c__24249__auto__ = cljs.core.chunk_first.call(null,seq__34343__$1);
var G__34359 = cljs.core.chunk_rest.call(null,seq__34343__$1);
var G__34360 = c__24249__auto__;
var G__34361 = cljs.core.count.call(null,c__24249__auto__);
var G__34362 = (0);
seq__34343 = G__34359;
chunk__34344 = G__34360;
count__34345 = G__34361;
i__34346 = G__34362;
continue;
} else {
var i = cljs.core.first.call(null,seq__34343__$1);
if((i > datalength)){
(input[i] = (0));
} else {
}

var G__34363 = cljs.core.next.call(null,seq__34343__$1);
var G__34364 = null;
var G__34365 = (0);
var G__34366 = (0);
seq__34343 = G__34363;
chunk__34344 = G__34364;
count__34345 = G__34365;
i__34346 = G__34366;
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__34351_34367 = cljs.core.seq.call(null,cljs.core.range.call(null,cljs.core.count.call(null,value)));
var chunk__34352_34368 = null;
var count__34353_34369 = (0);
var i__34354_34370 = (0);
while(true){
if((i__34354_34370 < count__34353_34369)){
var i_34371 = cljs.core._nth.call(null,chunk__34352_34368,i__34354_34370);
(view[i_34371] = value.charCodeAt(i_34371));

var G__34372 = seq__34351_34367;
var G__34373 = chunk__34352_34368;
var G__34374 = count__34353_34369;
var G__34375 = (i__34354_34370 + (1));
seq__34351_34367 = G__34372;
chunk__34352_34368 = G__34373;
count__34353_34369 = G__34374;
i__34354_34370 = G__34375;
continue;
} else {
var temp__5278__auto___34376 = cljs.core.seq.call(null,seq__34351_34367);
if(temp__5278__auto___34376){
var seq__34351_34377__$1 = temp__5278__auto___34376;
if(cljs.core.chunked_seq_QMARK_.call(null,seq__34351_34377__$1)){
var c__24249__auto___34378 = cljs.core.chunk_first.call(null,seq__34351_34377__$1);
var G__34379 = cljs.core.chunk_rest.call(null,seq__34351_34377__$1);
var G__34380 = c__24249__auto___34378;
var G__34381 = cljs.core.count.call(null,c__24249__auto___34378);
var G__34382 = (0);
seq__34351_34367 = G__34379;
chunk__34352_34368 = G__34380;
count__34353_34369 = G__34381;
i__34354_34370 = G__34382;
continue;
} else {
var i_34383 = cljs.core.first.call(null,seq__34351_34377__$1);
(view[i_34383] = value.charCodeAt(i_34383));

var G__34384 = cljs.core.next.call(null,seq__34351_34377__$1);
var G__34385 = null;
var G__34386 = (0);
var G__34387 = (0);
seq__34351_34367 = G__34384;
chunk__34352_34368 = G__34385;
count__34353_34369 = G__34386;
i__34354_34370 = G__34387;
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$string34391 !== '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$string34391 = (function (size,meta34392){
this.size = size;
this.meta34392 = meta34392;
this.cljs$lang$protocol_mask$partition0$ = 393216;
this.cljs$lang$protocol_mask$partition1$ = 0;
})
octet.spec.string.t_octet$spec$string34391.prototype.cljs$core$IWithMeta$_with_meta$arity$2 = (function (_34393,meta34392__$1){
var self__ = this;
var _34393__$1 = this;
return (new octet.spec.string.t_octet$spec$string34391(self__.size,meta34392__$1));
});

octet.spec.string.t_octet$spec$string34391.prototype.cljs$core$IMeta$_meta$arity$1 = (function (_34393){
var self__ = this;
var _34393__$1 = this;
return self__.meta34392;
});

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

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

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

octet.spec.string.t_octet$spec$string34391.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$string34391.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$string34391.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,"meta34392","meta34392",-175801403,null)], null);
});

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

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

octet.spec.string.t_octet$spec$string34391.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$string34391");
});

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

}

return (new octet.spec.string.t_octet$spec$string34391(size,cljs.core.PersistentArrayMap.EMPTY));
});
/**
 * Arbitrary length string type spec.
 */
octet.spec.string.string_STAR_ = (function (){
if(typeof octet.spec.string.t_octet$spec$string34394 !== '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$string34394 = (function (meta34395){
this.meta34395 = meta34395;
this.cljs$lang$protocol_mask$partition0$ = 393217;
this.cljs$lang$protocol_mask$partition1$ = 0;
})
octet.spec.string.t_octet$spec$string34394.prototype.cljs$core$IWithMeta$_with_meta$arity$2 = (function (_34396,meta34395__$1){
var self__ = this;
var _34396__$1 = this;
return (new octet.spec.string.t_octet$spec$string34394(meta34395__$1));
});

octet.spec.string.t_octet$spec$string34394.prototype.cljs$core$IMeta$_meta$arity$1 = (function (_34396){
var self__ = this;
var _34396__$1 = this;
return self__.meta34395;
});

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

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

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

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

octet.spec.string.t_octet$spec$string34394.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$string34394.prototype.octet$spec$ISpec$ = cljs.core.PROTOCOL_SENTINEL;

octet.spec.string.t_octet$spec$string34394.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$string34394.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$string34394.getBasis = (function (){
return new cljs.core.PersistentVector(null, 1, 5, cljs.core.PersistentVector.EMPTY_NODE, [new cljs.core.Symbol(null,"meta34395","meta34395",1221526784,null)], null);
});

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

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

octet.spec.string.t_octet$spec$string34394.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$string34394");
});

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

}

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