source: tags/mediaplayer-4.5/com/jeroenwijering/utils/Strings.as @ 221

Revision 221, 2.9 KB checked in by jeroen, 4 years ago (diff)

tagged the 4.5 player

  • Property svn:executable set to *
Line 
1package com.jeroenwijering.utils {
2
3
4/**
5* This class groups a couple of commonly used string operations.
6**/
7public class Strings {
8
9
10        /**
11        * Unescape a string and filter "asfunction" occurences ( can be used for XSS exploits).
12        *
13        * @param str    The string to decode.
14        * @return               The decoded string.
15        **/
16        public static function decode(str:String):String {
17                if(str.indexOf('asfunction') == -1) {
18                        return unescape(str);
19                } else {
20                        return '';
21                }
22        };
23
24
25        /**
26        * Convert a number to a digital-clock like string.
27        *
28        * @param nbr    The number of seconds.
29        * @return               A MN:SS string.
30        **/
31        public static function digits(nbr:Number):String {
32                var min:Number = Math.floor(nbr/60);
33                var sec:Number = Math.floor(nbr%60);
34                var str:String = Strings.zero(min)+':'+Strings.zero(sec);
35                return str;
36        };
37
38
39        /**
40        * Convert a time-representing string to a number.
41        *
42        * @param str    The input string. Supported are 00:03:00.1 / 03:00.1 / 180.1s / 3.2m / 3.2h
43        * @return               The number of seconds.
44        **/
45        public static function seconds(str:String):Number {
46                str = str.replace(',','.');
47                var arr:Array = str.split(':');
48                var sec:Number = 0;
49                if (str.substr(-1) == 's') {
50                        sec = Number(str.substr(0,str.length-1));
51                } else if (str.substr(-1) == 'm') {
52                        sec = Number(str.substr(0,str.length-1)) * 60;
53                } else if(str.substr(-1) == 'h') {
54                        sec = Number(str.substr(0,str.length-1)) *3600;
55                } else if(arr.length > 1) {
56                        sec = Number(arr[arr.length-1]);
57                        sec += Number(arr[arr.length-2]) * 60;
58                        if(arr.length == 3) {
59                                sec += Number(arr[arr.length-3]) *3600;
60                        }
61                } else {
62                        sec = Number(str);
63                }
64                return sec;
65        };
66
67
68        /**
69        * Basic serialization: string representations of booleans and numbers are returned typed;
70        * strings are returned urldecoded.
71        *
72        * @param val    String value to serialize.
73        * @return               The original value in the correct primitive type.
74        **/
75        public static function serialize(val:String):Object {
76                if(val == null) {
77                        return null;
78                } else if (val == 'true') {
79                        return true;
80                } else if (val == 'false') {
81                        return false;
82                } else if (isNaN(Number(val)) || val.length > 5) {
83                        return Strings.decode(val);
84                } else {
85                        return Number(val);
86                }
87        };
88
89
90        /**
91        * Strip HTML tags and linebreaks off a string.
92        *
93        * @param str    The string to clean up.
94        * @return               The clean string.
95        **/
96        public static function strip(str:String):String {
97                var tmp:Array = str.split("\n");
98                str = tmp.join("");
99                tmp = str.split("\r");
100                str = tmp.join("");
101                var idx:Number = str.indexOf("<");
102                while(idx != -1) {
103                        var end:Number = str.indexOf(">",idx+1);
104                        end == -1 ? end = str.length-1: null;
105                        str = str.substr(0,idx)+" "+str.substr(end+1,str.length);
106                        idx = str.indexOf("<",idx);
107                }
108                return str;
109        };
110
111
112        /**
113        * Add a leading zero to a number.
114        *
115        * @param nbr    The number to convert. Can be 0 to 99.
116        * @ return              A string representation with possible leading 0.
117        **/
118        public static function zero(nbr:Number):String {
119                if(nbr < 10) {
120                        return '0'+nbr;
121                } else {
122                        return ''+nbr;
123                }
124        };
125
126
127}
128
129
130}
Note: See TracBrowser for help on using the repository browser.