source: trunk/fl5/src/com/longtailvideo/jwplayer/utils/Strings.as @ 828

Revision 828, 5.1 KB checked in by pablo, 3 years ago (diff)
Line 
1package com.longtailvideo.jwplayer.utils {
2
3        /**
4         * This class groups a couple of commonly used string operations.
5         * @author Jeroen Wijering
6         **/
7        public class Strings {
8
9                /**
10                 * Unescape a string and filter "asfunction" occurences ( can be used for XSS exploits).
11                 *
12                 * @param str   The string to decode.
13                 * @return              The decoded string.
14                 **/
15                public static function decode(str:String):String {
16                        if (str.indexOf('asfunction') == -1) {
17                                return unescape(str);
18                        } else {
19                                return '';
20                        }
21                }
22
23                /**
24                 * Convert a number to a digital-clock like string.
25                 *
26                 * @param nbr   The number of seconds.
27                 * @return              A MN:SS string.
28                 **/
29                public static function digits(nbr:Number):String {
30                        var min:Number = Math.floor(nbr / 60);
31                        var sec:Number = Math.floor(nbr % 60);
32                        var str:String = Strings.zero(min) + ':' + Strings.zero(sec);
33                        return str;
34                }
35
36                /**
37                 * Convert a time-representing string to a number.
38                 *
39                 * @param str   The input string. Supported are 00:03:00.1 / 03:00.1 / 180.1s / 3.2m / 3.2h
40                 * @return              The number of seconds.
41                 **/
42                public static function seconds(str:String):Number {
43                        str = str.replace(',', '.');
44                        var arr:Array = str.split(':');
45                        var sec:Number = 0;
46                        if (str.substr(-1) == 's') {
47                                sec = Number(str.substr(0, str.length - 1));
48                        } else if (str.substr(-1) == 'm') {
49                                sec = Number(str.substr(0, str.length - 1)) * 60;
50                        } else if (str.substr(-1) == 'h') {
51                                sec = Number(str.substr(0, str.length - 1)) * 3600;
52                        } else if (arr.length > 1) {
53                                sec = Number(arr[arr.length - 1]);
54                                sec += Number(arr[arr.length - 2]) * 60;
55                                if (arr.length == 3) {
56                                        sec += Number(arr[arr.length - 3]) * 3600;
57                                }
58                        } else {
59                                sec = Number(str);
60                        }
61                        return sec;
62                }
63
64                /**
65                 * Basic serialization: string representations of booleans and numbers are returned typed;
66                 * strings are returned urldecoded.
67                 *
68                 * @param val   String value to serialize.
69                 * @return              The original value in the correct primitive type.
70                 **/
71                public static function serialize(val:String):Object {
72                        if (val == null) {
73                                return null;
74                        } else if (val == 'true') {
75                                return true;
76                        } else if (val == 'false') {
77                                return false;
78                        } else if (isNaN(Number(val)) || val.length > 5) {
79                                return val;
80                        } else {
81                                return Number(val);
82                        }
83                }
84
85                /**
86                 * Strip HTML tags and linebreaks off a string.
87                 *
88                 * @param str   The string to clean up.
89                 * @return              The clean string.
90                 **/
91                public static function strip(str:String):String {
92                        var tmp:Array = str.split("\n");
93                        str = tmp.join("");
94                        tmp = str.split("\r");
95                        str = tmp.join("");
96                        var idx:Number = str.indexOf("<");
97                        while (idx != -1) {
98                                var end:Number = str.indexOf(">", idx + 1);
99                                end == -1 ? end = str.length - 1 : null;
100                                str = str.substr(0, idx) + " " + str.substr(end + 1, str.length);
101                                idx = str.indexOf("<", idx);
102                        }
103                        return str;
104                }
105
106                /**
107                 * Add a leading zero to a number.
108                 *
109                 * @param nbr   The number to convert. Can be 0 to 99.
110                 * @ return             A string representation with possible leading 0.
111                 **/
112                public static function zero(nbr:Number):String {
113                        if (nbr < 10) {
114                                return '0' + nbr;
115                        } else {
116                                return '' + nbr;
117                        }
118                }
119                /**
120                 * Finds the extension of a filename or URL
121                 * @param filename      The string on which to search
122                 * @return                      Everything trailing the final '.' character
123                 *
124                 */
125                public static function extension(filename:String):String {
126                        if (filename.lastIndexOf(".") > 0) {
127                                //https://mail.google.com/a/longtailvideo.com/#inbox/125e06cbfca4149f
128                                if (filename.lastIndexOf("?") > 0){
129                                        Logger.log(filename.substring(filename.lastIndexOf(".")+1, filename.lastIndexOf("?")).toLowerCase());
130                                        return filename.substring(filename.lastIndexOf(".")+1, filename.lastIndexOf("?")).toLowerCase();       
131                                } else {
132                                        return filename.substring(filename.lastIndexOf(".")+1, filename.length).toLowerCase();
133                                }
134                        } else {
135                                return "";
136                        }
137                }
138               
139                /**
140                 * Recursively creates a string representation of an object and its properties.
141                 *
142                 * @param object The object to be converted to a string.
143                 */
144                public static function print_r(object:Object):String {
145                        var result:String = "";
146                        if (typeof(object) == "object") {
147                                result += "{";
148                        }
149                        for (var property:Object in object) {
150                                if (typeof(object[property]) == "object") {
151                                        result += property + ": ";
152                                } else {
153                                        result += property + ": " + object[property];
154                                }
155                                result += print_r(object[property]) +  ", ";
156                        }
157                       
158                        if (result != "{"){
159                                result = result.substr(0, result.length - 2);
160                        }
161                       
162                        if (typeof(object) == "object") {
163                                result += "}";
164                        }
165
166                        return result;
167                }
168               
169                /** Remove white space from before and after a string. **/
170                public static function trim(s:String):String {
171                        return s.replace(/^\s+/, '').replace(/\s+$/, '');
172                }
173               
174                /** Get the value of a case-insensitive attribute in an XML node **/
175                public static function xmlAttribute(xml:XML, attribute:String):String {
176                        for each (var attrib:XML in xml.attributes()) {
177                                if (attrib.name().toString().toLowerCase() == attribute.toLowerCase())
178                                        return attrib.toString();
179                        }
180                        return "";
181                }
182
183        }
184
185}
Note: See TracBrowser for help on using the repository browser.