001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019/**
020 * Implements the CFML Function jsstringformat
021 */
022package lucee.runtime.functions.string;
023
024import lucee.runtime.PageContext;
025import lucee.runtime.ext.function.Function;
026
027public final class JSStringFormat implements Function {
028
029        private static final long serialVersionUID = -4188516789835855021L;
030
031
032        public static String call(PageContext pc , String str) {
033                return invoke(str);
034        }
035        public static String invoke(String str) {
036            int len=str.length();
037            StringBuilder rtn=new StringBuilder(len+10);
038                char c;
039                for(int i=0;i<len;i++) {
040                    c=str.charAt(i);
041                        switch(c) {
042                                case '\\': rtn.append("\\\\"); break;
043                                case '\n': rtn.append("\\n"); break;
044                                case '\r': rtn.append("\\r"); break;
045                                case '\f': rtn.append("\\f"); break;
046                                case '\b': rtn.append("\\b"); break;
047                                case '\t': rtn.append("\\t"); break;
048                                case '"' : rtn.append("\\\""); break;
049                                case '\'': rtn.append("\\\'"); break;
050                                default : rtn.append(c); break;
051                        }
052                }
053                return rtn.toString();
054        }
055        
056        
057        public static String callx(PageContext pc , String jsString) {// MUST ????
058                int len=jsString.length();
059                int plus=0;
060                
061                for(int pos=0;pos<len;pos++) {
062            char chr = jsString.charAt(pos);
063            
064            switch(chr){
065                                case '\\': 
066                                case '\n': 
067                                case '\r': 
068                                case '\f': 
069                                case '\b': 
070                                case '\t': 
071                                case '"' : 
072                                case '\'': plus++; break;
073            }
074                }
075        if(plus==0) return jsString;
076        
077        char[] chars=new char[len+plus];
078        int count=0;
079        
080                for(int pos=0;pos<len;pos++) {
081            char chr = jsString.charAt(pos);
082            switch(chr){
083                        case '\\': 
084                            chars[count++]='\\';
085                            chars[count++]='\\';
086                        break;
087                        case '\'': 
088                            chars[count++]='\\';
089                            chars[count++]='\'';
090                        break;
091                        case '"': 
092                            chars[count++]='\\';
093                            chars[count++]='"';
094                        break;
095                case '\n': 
096                    chars[count++]='\\';
097                    chars[count++]='n';
098                break;
099                case '\r': 
100                    chars[count++]='\\';
101                    chars[count++]='r';
102                break;
103                case '\f': 
104                    chars[count++]='\\';
105                    chars[count++]='f';
106                break;
107                case '\b': 
108                    chars[count++]='\\';
109                    chars[count++]='b';
110                break;
111                case '\t': 
112                    chars[count++]='\\';
113                    chars[count++]='t';
114                break;
115                default: 
116                    chars[count++]=chr;
117                break;
118            }
119                }
120        return new String(chars);
121        }
122}