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

var G__34431 = seq__34419;
var G__34432 = chunk__34420;
var G__34433 = count__34421;
var G__34434 = (i__34422 + (1));
seq__34419 = G__34431;
chunk__34420 = G__34432;
count__34421 = G__34433;
i__34422 = G__34434;
continue;
} else {
var temp__5278__auto__ = cljs.core.seq.call(null,seq__34419);
if(temp__5278__auto__){
var seq__34419__$1 = temp__5278__auto__;
if(cljs.core.chunked_seq_QMARK_.call(null,seq__34419__$1)){
var c__24059__auto__ = cljs.core.chunk_first.call(null,seq__34419__$1);
var G__34435 = cljs.core.chunk_rest.call(null,seq__34419__$1);
var G__34436 = c__24059__auto__;
var G__34437 = cljs.core.count.call(null,c__24059__auto__);
var G__34438 = (0);
seq__34419 = G__34435;
chunk__34420 = G__34436;
count__34421 = G__34437;
i__34422 = G__34438;
continue;
} else {
var i = cljs.core.first.call(null,seq__34419__$1);
if((i > datalength)){
(input[i] = (0));
} else {
}

var G__34439 = cljs.core.next.call(null,seq__34419__$1);
var G__34440 = null;
var G__34441 = (0);
var G__34442 = (0);
seq__34419 = G__34439;
chunk__34420 = G__34440;
count__34421 = G__34441;
i__34422 = G__34442;
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__34427_34443 = cljs.core.seq.call(null,cljs.core.range.call(null,cljs.core.count.call(null,value)));
var chunk__34428_34444 = null;
var count__34429_34445 = (0);
var i__34430_34446 = (0);
while(true){
if((i__34430_34446 < count__34429_34445)){
var i_34447 = cljs.core._nth.call(null,chunk__34428_34444,i__34430_34446);
(view[i_34447] = value.charCodeAt(i_34447));

var G__34448 = seq__34427_34443;
var G__34449 = chunk__34428_34444;
var G__34450 = count__34429_34445;
var G__34451 = (i__34430_34446 + (1));
seq__34427_34443 = G__34448;
chunk__34428_34444 = G__34449;
count__34429_34445 = G__34450;
i__34430_34446 = G__34451;
continue;
} else {
var temp__5278__auto___34452 = cljs.core.seq.call(null,seq__34427_34443);
if(temp__5278__auto___34452){
var seq__34427_34453__$1 = temp__5278__auto___34452;
if(cljs.core.chunked_seq_QMARK_.call(null,seq__34427_34453__$1)){
var c__24059__auto___34454 = cljs.core.chunk_first.call(null,seq__34427_34453__$1);
var G__34455 = cljs.core.chunk_rest.call(null,seq__34427_34453__$1);
var G__34456 = c__24059__auto___34454;
var G__34457 = cljs.core.count.call(null,c__24059__auto___34454);
var G__34458 = (0);
seq__34427_34443 = G__34455;
chunk__34428_34444 = G__34456;
count__34429_34445 = G__34457;
i__34430_34446 = G__34458;
continue;
} else {
var i_34459 = cljs.core.first.call(null,seq__34427_34453__$1);
(view[i_34459] = value.charCodeAt(i_34459));

var G__34460 = cljs.core.next.call(null,seq__34427_34453__$1);
var G__34461 = null;
var G__34462 = (0);
var G__34463 = (0);
seq__34427_34443 = G__34460;
chunk__34428_34444 = G__34461;
count__34429_34445 = G__34462;
i__34430_34446 = G__34463;
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$string34467 !== '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$string34467 = (function (size,meta34468){
this.size = size;
this.meta34468 = meta34468;
this.cljs$lang$protocol_mask$partition0$ = 393216;
this.cljs$lang$protocol_mask$partition1$ = 0;
})
octet.spec.string.t_octet$spec$string34467.prototype.cljs$core$IWithMeta$_with_meta$arity$2 = (function (_34469,meta34468__$1){
var self__ = this;
var _34469__$1 = this;
return (new octet.spec.string.t_octet$spec$string34467(self__.size,meta34468__$1));
});

octet.spec.string.t_octet$spec$string34467.prototype.cljs$core$IMeta$_meta$arity$1 = (function (_34469){
var self__ = this;
var _34469__$1 = this;
return self__.meta34468;
});

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

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

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

octet.spec.string.t_octet$spec$string34467.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$string34467.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$string34467.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,"meta34468","meta34468",-1105104770,null)], null);
});

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

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

octet.spec.string.t_octet$spec$string34467.cljs$lang$ctorPrWriter = (function (this__23805__auto__,writer__23806__auto__,opt__23807__auto__){
return cljs.core._write.call(null,writer__23806__auto__,"octet.spec.string/t_octet$spec$string34467");
});

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

}

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

octet.spec.string.t_octet$spec$string34470.prototype.cljs$core$IMeta$_meta$arity$1 = (function (_34472){
var self__ = this;
var _34472__$1 = this;
return self__.meta34471;
});

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

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

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

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

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

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

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

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

octet.spec.string.t_octet$spec$string34470.cljs$lang$ctorPrWriter = (function (this__23805__auto__,writer__23806__auto__,opt__23807__auto__){
return cljs.core._write.call(null,writer__23806__auto__,"octet.spec.string/t_octet$spec$string34470");
});

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

}

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