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

Revision 318, 3.5 KB checked in by pablo, 4 years ago (diff)

Overwriting the playlist and associated tests

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                                return filename.substring(filename.lastIndexOf(".")+1, filename.length).toLowerCase();
128                        } else {
129                                return "";
130                        }
131                }
132
133        }
134
135}
Note: See TracBrowser for help on using the repository browser.