<commit_before>package aerys.qark
{
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;

	public class Qark
	{
		private static const FLAG_OBJECT		: int		= 0;
		private static const FLAG_ARRAY			: int		= 1;
		private static const FLAG_INTEGER		: int		= 2;
		private static const FLAG_FLOAT			: int		= 3;
		private static const FLAG_STRING		: int		= 4;
		private static const FLAG_UTF_STRING	: int		= 5;
		private static const FLAG_BYTES			: int		= 6;
		private static const FLAG_BOOLEAN		: int		= 7;
		
		private static const ENCODERS		: Array		= [encodeObject,
														   encodeArray,
														   encodeInteger,
														   encodeFloat,
														   encodeString,
														   encodeUTFString,
														   encodeBytes,
														   encodeBoolean];
		
		private static const DECODERS		: Array		= [decodeObject,
														   decodeArray,
														   decodeInteger,
														   decodeFloat,
														   decodeString,
														   decodeUTFString,
														   decodeBytes,
														   decodeBoolean];
		
		private static function getTypeFlag(source : *) : int
		{
			if (source is int)
				return FLAG_INTEGER;
			if (source is Number)
				return FLAG_FLOAT;
			if (source is String)
				return FLAG_UTF_STRING;
			if (source is Array)
				return FLAG_ARRAY;
			if (source is ByteArray)
				return FLAG_BYTES;
			if (source is Boolean)
				return FLAG_BOOLEAN;
			
			return FLAG_OBJECT;
		}
		
		public static function encode(...sources) : ByteArray
		{
			var data 	: ByteArray = new ByteArray();
			
			for each (var source : * in sources)
				encodeRecursive(source, data);
			
			data.position = 0;
			
			return data;
		}
		
		public static function decode(source : ByteArray) : Array
		{
			var result : Array = new Array();
			
			while (source.bytesAvailable)
				result.push(decodeRecursive(source));
			
			return result;
		}
		
		public static function encodeRecursive(source : *, target : ByteArray) : void
		{
			var flag : int = getTypeFlag(source);
			
			target.writeByte(flag);
			ENCODERS[flag].call(null, source, target);
		}
		
		public static function decodeRecursive(source : ByteArray) : *
		{
			var flag : int = source.readByte();
			
			return DECODERS[flag].call(null, source);
		}
		
		private static function encodeObject(source : Object, target : ByteArray) : void
		{
			var start	: int = target.position;
			var length 	: int = 0;
			
			target.position += 2;
			
			for (var propertyName : String in source)
			{
				encodeString(propertyName, target);
				encodeRecursive(source[propertyName], target);
				
				++length;
			}<commit_msg>M decode returns an array only if multiple objects where encoded<commit_after>package aerys.qark
{
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;

	public class Qark
	{
		private static const FLAG_OBJECT		: int		= 0;
		private static const FLAG_ARRAY			: int		= 1;
		private static const FLAG_INTEGER		: int		= 2;
		private static const FLAG_FLOAT			: int		= 3;
		private static const FLAG_STRING		: int		= 4;
		private static const FLAG_UTF_STRING	: int		= 5;
		private static const FLAG_BYTES			: int		= 6;
		private static const FLAG_BOOLEAN		: int		= 7;
		
		private static const ENCODERS		: Array		= [encodeObject,
														   encodeArray,
														   encodeInteger,
														   encodeFloat,
														   encodeString,
														   encodeUTFString,
														   encodeBytes,
														   encodeBoolean];
		
		private static const DECODERS		: Array		= [decodeObject,
														   decodeArray,
														   decodeInteger,
														   decodeFloat,
														   decodeString,
														   decodeUTFString,
														   decodeBytes,
														   decodeBoolean];
		
		private static function getTypeFlag(source : *) : int
		{
			if (source is int)
				return FLAG_INTEGER;
			if (source is Number)
				return FLAG_FLOAT;
			if (source is String)
				return FLAG_UTF_STRING;
			if (source is Array)
				return FLAG_ARRAY;
			if (source is ByteArray)
				return FLAG_BYTES;
			if (source is Boolean)
				return FLAG_BOOLEAN;
			
			return FLAG_OBJECT;
		}
		
		public static function encode(...sources) : ByteArray
		{
			var data 	: ByteArray = new ByteArray();
			
			for each (var source : * in sources)
				encodeRecursive(source, data);
			
			data.position = 0;
			
			return data;
		}
		
		public static function decode(source : ByteArray) : *
		{
			var result : Array = new Array();
			
			while (source.bytesAvailable)
				result.push(decodeRecursive(source));
			
			return result.length == 1 ? result[0] : result;
		}
		
		public static function encodeRecursive(source : *, target : ByteArray) : void
		{
			var flag : int = getTypeFlag(source);
			
			target.writeByte(flag);
			ENCODERS[flag].call(null, source, target);
		}
		
		public static function decodeRecursive(source : ByteArray) : *
		{
			var flag : int = source.readByte();
			
			return DECODERS[flag].call(null, source);
		}
		
		private static function encodeObject(source : Object, target : ByteArray) : void
		{
			var start	: int = target.position;
			var length 	: int = 0;
			
			target.position += 2;
			
			for (var propertyName : String in source)
			{
				encodeString(propertyName, target);
				encodeRecursive(source[propertyName], target);
				
				++length;
			}
{'fraction_non_alphanumeric': 0.08125, 'fraction_numerical': 0.004411764705882353, 'mean_word_length': 2.2896009673518742, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2061896', 'n_tokens_mistral': 2379, 'n_tokens_neox': 1614, 'n_words': 456}