ByteArray walk, RegExp function to check Error.

Professeur Pirson Nicolas Pirson :
Go to my website : tatactic.be [FR] (Website about hepatitis)
nicolas.pirson.me [FR - EN] (current website)
business card - blog [FR - EN] (just for fun)
Nicolas Pirson CV page [FR] on tatactic.be
Back to AS3 - FLASH index ... [EN]

Source :

// IMPORTS
import flash.utils.ByteArray;
// VARIABLES DECLARATIONS :
// Array of some stuffs

var listOfAsholes:Array = ["Bart de Wever", 599.99, "Yves Leterme", 75.88, "EDDY WALLY", 30.6, "CENSORED" , 150.99];
// ByteArray (var bytes)
var bytes:ByteArray = new ByteArray();
// iterator for uint loops
var i:uint = 0;
// define possible values Array variable : Array
var acceptedValues:Array;
// define an Array with possible Matches suggestions. Will be filled by :
// checkPossibleMatch(string_value:String):Array

var possibleMatch:Array;
// create an array to check the possible arguments to pass to the function

// for each item in the selected Array
// WRITE THE BYTEARRAY OBJECT


function writeByteArray(byteArrayObj:ByteArray,array_to_read:Array):void{
for (var i:int = 0; i < listOfAsholes.length; i++) {
byteArrayObj.writeUTFBytes(listOfAsholes[i++]);
// writeString and pos for next item
byteArrayObj.writeFloat(listOfAsholes[i]);
// writeFloat
trace("bytes.position is: " + bytes.position);
// display pos in ByteArray
}
}
// END WRITE THE BYTEARRAY OBJECT

// READ THROUGH THE BYTEARRAY OBJECT

function readBytesArray(byte_array:ByteArray,readType:String="UTF"){
var dataType:String = readType.toUpperCase();
// case insensitive argument (all in Caps)
ByteArray(byte_array).position = 0
acceptedValues = new Array("UTF","BYTE","DOUBLE","INT","FLOAT");
// Fill the Array with cases here under :
switch(dataType){
case "UTF":
trace("\n *** ByteArray.read " + dataType);
for (i = 1 ; i <= byte_array.bytesAvailable ; i++){
trace ("byte_array.readUTFBytes = " + (ByteArray(byte_array).readUTFBytes(i)) + " , pos = " + + byte_array.position);
}
break;
case "BYTE":
trace("\n *** ByteArray.read " + dataType);
for (i = 1 ; i <= byte_array.bytesAvailable ; i++){
trace ("byte_array.readUTFBytes = " + (ByteArray(byte_array).readByte()) + " , pos = " + + byte_array.position);
}
break;
case "DOUBLE":
trace("\n *** ByteArray.read " + dataType);
for (i = 1 ; i <= byte_array.bytesAvailable ; i++){
trace ("byte_array.readUTFBytes = " + (ByteArray(byte_array).readDouble()) + " , pos = " + + byte_array.position);
}
break;
case "INT":
trace("\n *** ByteArray.read " + dataType);
for (i = 1 ; i <= byte_array.bytesAvailable ; i++){
trace ("byte_array.readUTFBytes = " + (ByteArray(byte_array).readInt()) + " , pos = " + + byte_array.position);
}
break;
case "FLOAT":
trace("\n *** ByteArray.read " + dataType);
for (i = 1 ; i <= byte_array.bytesAvailable ; i++){
trace ("byte_array.readUTFBytes = " + (ByteArray(byte_array).readFloat()) + " , pos = " + + byte_array.position);
}
break;
default:
var err:Error;
var isError:Boolean = false;

try{
var regexpReturn:Array = checkPossibleMatch(dataType);
var typeErrorMessage:String = new String("");

typeErrorMessage = "!!!!!!!!! -------------------------------------------------------------\n";
typeErrorMessage += (" -> The value passed as argument (\"" + dataType + "\") is unexpected.\n");
typeErrorMessage += "!!!!!!!!! -------------------------------------------------------------\n";
typeErrorMessage += getRegExpSuggestions(regexpReturn);
typeErrorMessage += ("\nvalues are :\n");
typeErrorMessage += (" - UTF - > readUTFBytes();\n");
typeErrorMessage += (" - BYTE - > readUTFBytes();\n");
typeErrorMessage += (" - DOUBLE - > readUTFBytes();\n");
typeErrorMessage += (" - INT - > readUTFBytes();\n");
typeErrorMessage += (" - FLOAT - > readUTFBytes();\n");
typeErrorMessage += ("All types are case insensitive ('Utf' = 'utf' = 'UTF')\n");
var errorToThrow:Error = new Error(typeErrorMessage,990010);
throw errorToThrow;
}catch(err:*){
trace(" *********** ERROR ***********");
trace (" -> ERROR NAME = " + err.name);
trace(" -> ERROR MESSAGE = \n" + err.message);
trace(" -> ERROR ID = " + err.errorID);
trace(" ***********************************");

isError = true;
}finally{
if(isError){
trace("ERROR " + errorToThrow.errorID + " OCCURED.");
trace("Read output above for POSSIBLE MISTAKES and SOLUTIONS.");
trace(" ***********************************");

}
isError = false;
trace("END SWITCH FUNCTION FOR :\nreadBytesArray(byte_array:ByteArray,readType:String=\"UTF\")\n");
trace("bytes length is: " + bytes.length);

}
}
}
// END READ THROUGH THE BYTEARRAY OBJECT WITH ERROR MESSAGES AND SUGGESTIONS
// CHECK VIA A RegExp in the getRegExpSuggestions(arr:Array):String

function checkPossibleMatch(string_value:String):Array{
/*
// A FUNCTION TO WALK IN ARGUMENTS VIA RegExp in acceptedValues

OPTIONNAL ARGS FOR A RegExp
\f matches form-feed.
\r matches carriage return.
\n matches linefeed.
\t matches horizontal tab.
\v matches vertical tab.
\0 matches NUL character.
[\b] matches backspace.
\s matches whitespace (short for [\f\n\r\t\v\u00A0\u2028\u2029]).
\S matches anything but a whitespace (short for [^\f\n\r\t\v\u00A0\u2028\u2029]).
\w matches any alphanumerical character (word characters) including underscore (short for [a-zA-Z0-9_]).
\W matches any non-word characters (short for [^a-zA-Z0-9_]).
\d matches any digit (short for [0-9]).
\D matches any non-digit (short for [^0-9]).
\b matches a word boundary (the position between a word and a space).
\B matches a non-word boundary (short for [^\b]).
\cX matches a control character. E.g: \cm matches control-M.
\xhh matches the character with two characters of hexadecimal code hh.
\uhhhh matches the Unicode character with four characters of hexadecimal code hhhh.

*/

possibleMatch = new Array();
var str:String;
str = string_value;
var pattern:RegExp;
pattern = new RegExp(".*?" + str + ".*?",i)
//pattern = /.*str.*?/i;
var resultFound:String;
for(i = 0 ; i<acceptedValues.length;i++){
resultFound = null;
resultFound = pattern.exec(acceptedValues[i]);
if(!(resultFound == null)){
possibleMatch.push(acceptedValues[i]);
}
}
return(possibleMatch);
}

// A FUNCTION TO FIND CLOSE MATCHES IN ARGUMENTS VIA THE acceptedValues:Array

function getRegExpSuggestions(arr:Array):String{
// A FUNCTION TO FIND CLOSE MATCHES IN ARGUMENTS VIA THE acceptedValues:Array
var returnRegexpString:String = new String("");
returnRegexpString += "--------------------------------------------------------------";
returnRegexpString += "\nPOSSIBLE MISTAKES : DID YOU MEAN ";
for (i = 0; i< arr.length ; i++){
returnRegexpString += '"' + arr[i]+'"';
if(i<arr.length-2){
returnRegexpString += ", ";
}else if(i == arr.length-2){
returnRegexpString += " or ";
}else if(i == arr.length-1){
returnRegexpString += " ? Possible match(es).";
returnRegexpString += "\n--------------------------------------------------------------";
}
}
return returnRegexpString;
}

/*
UNCOMMENT TO CHANGE THE DEFAULT ARRAY (A LITTLE SUBJECTIVE PERHAPS) :)
listOfAsholes = [];
*/

writeByteArray(bytes,listOfAsholes);
readBytesArray(bytes,"utf");
// readBytesArray(bytes,["UTF","BYTE","DOUBLE","INT","FLOAT"])
 

Output :

bytes.position is: 17
bytes.position is: 33
bytes.position is: 47
bytes.position is: 59

*** ByteArray.read UTF
byte_array.readUTFBytes = B , pos = 1
byte_array.readUTFBytes = ar , pos = 3
byte_array.readUTFBytes = t d , pos = 6
byte_array.readUTFBytes = e We , pos = 10
byte_array.readUTFBytes = verD , pos = 15
byte_array.readUTFBytes = ÿ\Yves , pos = 21
byte_array.readUTFBytes = Leterm , pos = 28
byte_array.readUTFBytes = eB—EDD , pos = 36
byte_array.readUTFBytes = Y WALLYAô , pos = 45
byte_array.readUTFBytes = ÌÍCENSORED , pos = 55

AND readBytesArray(bytes,"u");

Output :

bytes.position is: 17
bytes.position is: 33
bytes.position is: 47
bytes.position is: 59
*********** ERROR ***********
-> ERROR NAME = Error
-> ERROR MESSAGE =
!!!!!!!!! -------------------------------------------------------------
-> The value passed as argument ("U") is unexpected.
!!!!!!!!! -------------------------------------------------------------
--------------------------------------------------------------
POSSIBLE MISTAKES : DID YOU MEAN "UTF" or "DOUBLE" ? Possible match(es).
--------------------------------------------------------------
values are :
- UTF - > readUTFBytes();
- BYTE - > readUTFBytes();
- DOUBLE - > readUTFBytes();
- INT - > readUTFBytes();
- FLOAT - > readUTFBytes();
All types are case insensitive ('Utf' = 'utf' = 'UTF')

-> ERROR ID = 990010
***********************************
ERROR 990010 OCCURED.
Read output above for POSSIBLE MISTAKES and SOLUTIONS.
***********************************
END SWITCH FUNCTION FOR :
readBytesArray(byte_array:ByteArray,readType:String="UTF")

bytes length is: 59


XHTML 1.0 STRICT. Compatible W3C     CSS 2.0  Compatible W3C