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

var G__34251 = seq__34239;
var G__34252 = chunk__34240;
var G__34253 = count__34241;
var G__34254 = (i__34242 + (1));
seq__34239 = G__34251;
chunk__34240 = G__34252;
count__34241 = G__34253;
i__34242 = G__34254;
continue;
} else {
var temp__5278__auto__ = cljs.core.seq.call(null,seq__34239);
if(temp__5278__auto__){
var seq__34239__$1 = temp__5278__auto__;
if(cljs.core.chunked_seq_QMARK_.call(null,seq__34239__$1)){
var c__24059__auto__ = cljs.core.chunk_first.call(null,seq__34239__$1);
var G__34255 = cljs.core.chunk_rest.call(null,seq__34239__$1);
var G__34256 = c__24059__auto__;
var G__34257 = cljs.core.count.call(null,c__24059__auto__);
var G__34258 = (0);
seq__34239 = G__34255;
chunk__34240 = G__34256;
count__34241 = G__34257;
i__34242 = G__34258;
continue;
} else {
var i = cljs.core.first.call(null,seq__34239__$1);
if((i > datalength)){
(input[i] = (0));
} else {
}

var G__34259 = cljs.core.next.call(null,seq__34239__$1);
var G__34260 = null;
var G__34261 = (0);
var G__34262 = (0);
seq__34239 = G__34259;
chunk__34240 = G__34260;
count__34241 = G__34261;
i__34242 = G__34262;
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__34247_34263 = cljs.core.seq.call(null,cljs.core.range.call(null,cljs.core.count.call(null,value)));
var chunk__34248_34264 = null;
var count__34249_34265 = (0);
var i__34250_34266 = (0);
while(true){
if((i__34250_34266 < count__34249_34265)){
var i_34267 = cljs.core._nth.call(null,chunk__34248_34264,i__34250_34266);
(view[i_34267] = value.charCodeAt(i_34267));

var G__34268 = seq__34247_34263;
var G__34269 = chunk__34248_34264;
var G__34270 = count__34249_34265;
var G__34271 = (i__34250_34266 + (1));
seq__34247_34263 = G__34268;
chunk__34248_34264 = G__34269;
count__34249_34265 = G__34270;
i__34250_34266 = G__34271;
continue;
} else {
var temp__5278__auto___34272 = cljs.core.seq.call(null,seq__34247_34263);
if(temp__5278__auto___34272){
var seq__34247_34273__$1 = temp__5278__auto___34272;
if(cljs.core.chunked_seq_QMARK_.call(null,seq__34247_34273__$1)){
var c__24059__auto___34274 = cljs.core.chunk_first.call(null,seq__34247_34273__$1);
var G__34275 = cljs.core.chunk_rest.call(null,seq__34247_34273__$1);
var G__34276 = c__24059__auto___34274;
var G__34277 = cljs.core.count.call(null,c__24059__auto___34274);
var G__34278 = (0);
seq__34247_34263 = G__34275;
chunk__34248_34264 = G__34276;
count__34249_34265 = G__34277;
i__34250_34266 = G__34278;
continue;
} else {
var i_34279 = cljs.core.first.call(null,seq__34247_34273__$1);
(view[i_34279] = value.charCodeAt(i_34279));

var G__34280 = cljs.core.next.call(null,seq__34247_34273__$1);
var G__34281 = null;
var G__34282 = (0);
var G__34283 = (0);
seq__34247_34263 = G__34280;
chunk__34248_34264 = G__34281;
count__34249_34265 = G__34282;
i__34250_34266 = G__34283;
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$string34287 !== '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$string34287 = (function (size,meta34288){
this.size = size;
this.meta34288 = meta34288;
this.cljs$lang$protocol_mask$partition0$ = 393216;
this.cljs$lang$protocol_mask$partition1$ = 0;
})
octet.spec.string.t_octet$spec$string34287.prototype.cljs$core$IWithMeta$_with_meta$arity$2 = (function (_34289,meta34288__$1){
var self__ = this;
var _34289__$1 = this;
return (new octet.spec.string.t_octet$spec$string34287(self__.size,meta34288__$1));
});

octet.spec.string.t_octet$spec$string34287.prototype.cljs$core$IMeta$_meta$arity$1 = (function (_34289){
var self__ = this;
var _34289__$1 = this;
return self__.meta34288;
});

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

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

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

octet.spec.string.t_octet$spec$string34287.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$string34287.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$string34287.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,"meta34288","meta34288",-948190399,null)], null);
});

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

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

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

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

}

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

octet.spec.string.t_octet$spec$string34290.prototype.cljs$core$IMeta$_meta$arity$1 = (function (_34292){
var self__ = this;
var _34292__$1 = this;
return self__.meta34291;
});

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

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

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

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

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

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

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

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

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

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

}

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