Category Archives: strings

split() hacks & RegExp

Actionscript:
  1. var path:String = "section/home.swf";
  2.  
  3. var deepLinkValue:String = path.split("/")[1].split(".swf")[0];
  4.  
  5. trace(deepLinkValue);
  6. /*outputs:
  7. home
  8. */

As a primarily self taught programmer there was a time when I didn't know what a regular expression was. There was also a time when ActionScript didn't have them (you had to use a library). Sometimes when I'm in a rush I still do weird things like the above instead of regular expressions... pretty nasty I know.

Here are two different regular expressions that do the same thing:

Actionscript:
  1. var path = "section/home.swf"
  2.  
  3. trace(path.match(/\w+(?=\.)/))
  4. /*outputs:
  5. home
  6. */
  7.  
  8. trace(path.replace(/.+\/|\..+/g, ""))
  9. /* also outputs:
  10. home
  11. */

Actually, I'd be curious to see other regular expressions that only return one value (not an array) that achieve this same thing... feel free to post of a comment if you can think of one...

Also posted in string manipulation | Tagged , | Leave a comment

substr() Remove First Character

Actionscript:
  1. var someString:String = "_file";
  2. trace(someString.substr(1));
  3. /*
  4. ouptuts:
  5. file
  6. */

Turns out the second argument of substr() is optional..... not sure how I never noticed that before.

Also posted in string manipulation | Tagged , | Leave a comment

Crackable String Encoding

Actionscript:
  1. var encode:Object = new Object();
  2. var decode:Object = new Object();
  3. var a:Array = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".split("");
  4.  
  5. // change this for different encoded results
  6. var offset:int = 10;
  7.  
  8. for (var i:int = 0; i<a.length; i++){
  9.     var index:int = (a.length - i - offset) % a.length;
  10.     encode[a[i]] = a[index];
  11.     decode[a[index]] = a[i];
  12. }
  13.  
  14. function encodeString(str:String):String{
  15.     return map(str, encode);
  16. }
  17.  
  18. function decodeString(str:String):String{
  19.     return map(str, decode);
  20. }
  21.  
  22. function map(str:String, smode:Object):String{
  23.     var n:String = "";
  24.     for (var i:int = 0; i<str.length; i++){
  25.         var char:String = str.charAt(i);
  26.         var en:String = smode[char];
  27.         if (en){
  28.             n += en;
  29.         }else{
  30.             n += char;
  31.         }
  32.     }
  33.     return n;
  34. }
  35.  
  36. // test out the functions
  37.  
  38. var input:String = "This is a regular string";
  39.  
  40. trace(input);
  41.  
  42. var encoded:String = encodeString(input);
  43.  
  44. trace("encoded: ", encoded);
  45.  
  46. trace("decoded: ",decodeString(encoded));
  47.  
  48. /*
  49. outputs:
  50.  
  51. This is a regular string
  52. encoded:  gSRH1RH1Z1IVTFOZI1HGIRMT
  53. decoded:  This is a regular string
  54.  
  55. */

The above demos an intentionally simple string encoding technique.

This is a technique I use if I need to encode strings but don't care if someone figures out what the string value actually is. For me, this is more common than needing hard/impossible to crack string encoding algorithms. A good example is an e-card... a url for an ecard could look like this:

www.birthdaycardthing.com/?name=joe&age=32

or it could look like this:

www.birthdaycardthing.com/?i=brx&x=5p

I wrapped this snippet up into a class and made a few minor tweaks. The class is called SimpleCipher it has two static methods and one static property...

Actionscript:
  1. import com.actionsnippet.utils.SimpleCipher;
  2. // same as offset above, but can be set at any time
  3. // new encode and decode Objects will be calculated
  4. SimpleCipher.offset = 1;
  5. var input:String = "SimpleCipher encoding and decoding";
  6.              
  7. trace("input: ", input);
  8.              
  9. var encoded:String = SimpleCipher.encode(input);
  10.              
  11. trace("encoded: ", encoded);
  12. trace("decoded: ", SimpleCipher.decode(encoded));
  13. /*
  14. outputs:
  15. input:  SimpleCipher encoding and decoding
  16. encoded:  RhlokdBhogdq0dmbnchmf0 mc0cdbnchmf
  17. decoded:  SimpleCipher encoding and decoding
  18. */

Download SimpleCipher Class
(this class uses a static initializer... see yesterdays post)

Also posted in OOP | Tagged , | Leave a comment

Graphics Class Methods in XML

Actionscript:
  1. // for simplicity I left this XML inline, this will work exactly the same if it were external
  2. var program:XML=<body>
  3.                 <draw>
  4.                   <![CDATA[
  5.                    beginFill(0xFF0000);
  6.                    drawCircle(100,100,50);
  7.                    endFill();
  8.                    lineStyle(0, 0x666666);
  9.                    moveTo(100, 100);
  10.                    lineTo(200, 200);
  11.                    moveTo(300, 200);
  12.                    curveTo(350, 300, 400, 200);
  13.                    lineStyle(0, 0x0000FF);
  14.                    drawRect(200, 50,100,100) ;
  15.                     ]]>
  16.                 </draw>
  17. </body>;
  18.  
  19. // parse and run the Graphics class commands from the XML
  20. render(parseFunctions(program.draw.toString()));
  21.  
  22. function parseFunctions(dat:String):Array{
  23.     var a:Array = dat.split(";") ;
  24.     for (var i:int = 0; i<a.length-1; i++){
  25.         a[i] = a[i].split(/\(\)|\(|\)/g);
  26.         var f:String = a[i][0] = a[i][0].replace(/\s/g,"");
  27.         a[i] = a[i].splice(0, a[i].length - 1);
  28.         if (a[i].length> 1){
  29.          a[i] = a[i][1].split(",");
  30.          a[i].unshift(f);
  31.         }
  32.     }
  33.     return a.splice(0,a.length - 1);
  34. }
  35. function render(p:Array):void {
  36.     for (var i:int = 0; i<p.length; i++) {
  37.         graphics[p[i][0]].apply(graphics,p[i].splice(1));
  38.     }
  39. }

The above code builds on yesterdays post by showing how one could potentially store graphics class method calls in XML using a few regular expressions and Function.apply().

The parseFunctions() function reads through the CDATA string and formats it in a 2D array that looks like this:

Actionscript:
  1. [[beginFill, 0xFF0000], [drawCircle, 100, 100, 50], etc...]

The render() function reads through this 2D array, using the first value of each nested array as the function and the remaining values as arguments...

As is this won't really work with most of the new fp10 graphics methods...

Also posted in Graphics, XML, dynamic, external data, functions, string manipulation | Tagged , | Leave a comment