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

var G__34266 = seq__34254;
var G__34267 = chunk__34255;
var G__34268 = count__34256;
var G__34269 = (i__34257 + (1));
seq__34254 = G__34266;
chunk__34255 = G__34267;
count__34256 = G__34268;
i__34257 = G__34269;
continue;
} else {
var temp__5278__auto__ = cljs.core.seq.call(null,seq__34254);
if(temp__5278__auto__){
var seq__34254__$1 = temp__5278__auto__;
if(cljs.core.chunked_seq_QMARK_.call(null,seq__34254__$1)){
var c__24249__auto__ = cljs.core.chunk_first.call(null,seq__34254__$1);
var G__34270 = cljs.core.chunk_rest.call(null,seq__34254__$1);
var G__34271 = c__24249__auto__;
var G__34272 = cljs.core.count.call(null,c__24249__auto__);
var G__34273 = (0);
seq__34254 = G__34270;
chunk__34255 = G__34271;
count__34256 = G__34272;
i__34257 = G__34273;
continue;
} else {
var i = cljs.core.first.call(null,seq__34254__$1);
if((i > datalength)){
(input[i] = (0));
} else {
}

var G__34274 = cljs.core.next.call(null,seq__34254__$1);
var G__34275 = null;
var G__34276 = (0);
var G__34277 = (0);
seq__34254 = G__34274;
chunk__34255 = G__34275;
count__34256 = G__34276;
i__34257 = G__34277;
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__34262_34278 = cljs.core.seq.call(null,cljs.core.range.call(null,cljs.core.count.call(null,value)));
var chunk__34263_34279 = null;
var count__34264_34280 = (0);
var i__34265_34281 = (0);
while(true){
if((i__34265_34281 < count__34264_34280)){
var i_34282 = cljs.core._nth.call(null,chunk__34263_34279,i__34265_34281);
(view[i_34282] = value.charCodeAt(i_34282));

var G__34283 = seq__34262_34278;
var G__34284 = chunk__34263_34279;
var G__34285 = count__34264_34280;
var G__34286 = (i__34265_34281 + (1));
seq__34262_34278 = G__34283;
chunk__34263_34279 = G__34284;
count__34264_34280 = G__34285;
i__34265_34281 = G__34286;
continue;
} else {
var temp__5278__auto___34287 = cljs.core.seq.call(null,seq__34262_34278);
if(temp__5278__auto___34287){
var seq__34262_34288__$1 = temp__5278__auto___34287;
if(cljs.core.chunked_seq_QMARK_.call(null,seq__34262_34288__$1)){
var c__24249__auto___34289 = cljs.core.chunk_first.call(null,seq__34262_34288__$1);
var G__34290 = cljs.core.chunk_rest.call(null,seq__34262_34288__$1);
var G__34291 = c__24249__auto___34289;
var G__34292 = cljs.core.count.call(null,c__24249__auto___34289);
var G__34293 = (0);
seq__34262_34278 = G__34290;
chunk__34263_34279 = G__34291;
count__34264_34280 = G__34292;
i__34265_34281 = G__34293;
continue;
} else {
var i_34294 = cljs.core.first.call(null,seq__34262_34288__$1);
(view[i_34294] = value.charCodeAt(i_34294));

var G__34295 = cljs.core.next.call(null,seq__34262_34288__$1);
var G__34296 = null;
var G__34297 = (0);
var G__34298 = (0);
seq__34262_34278 = G__34295;
chunk__34263_34279 = G__34296;
count__34264_34280 = G__34297;
i__34265_34281 = G__34298;
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$string34302 !== '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$string34302 = (function (size,meta34303){
this.size = size;
this.meta34303 = meta34303;
this.cljs$lang$protocol_mask$partition0$ = 393216;
this.cljs$lang$protocol_mask$partition1$ = 0;
})
octet.spec.string.t_octet$spec$string34302.prototype.cljs$core$IWithMeta$_with_meta$arity$2 = (function (_34304,meta34303__$1){
var self__ = this;
var _34304__$1 = this;
return (new octet.spec.string.t_octet$spec$string34302(self__.size,meta34303__$1));
});

octet.spec.string.t_octet$spec$string34302.prototype.cljs$core$IMeta$_meta$arity$1 = (function (_34304){
var self__ = this;
var _34304__$1 = this;
return self__.meta34303;
});

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

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

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

octet.spec.string.t_octet$spec$string34302.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$string34302.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$string34302.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,"meta34303","meta34303",1869445249,null)], null);
});

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

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

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

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

}

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

octet.spec.string.t_octet$spec$string34305.prototype.cljs$core$IMeta$_meta$arity$1 = (function (_34307){
var self__ = this;
var _34307__$1 = this;
return self__.meta34306;
});

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

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

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

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

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

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

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

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

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

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

}

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