|
| 1 | +/* |
| 2 | + * Copyright 2017 Igor Maznitsa (http://www.igormaznitsa.com). |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.igormaznitsa.jcp.expression.functions; |
| 17 | + |
| 18 | +import javax.annotation.Nonnull; |
| 19 | + |
| 20 | +import com.igormaznitsa.jcp.context.PreprocessorContext; |
| 21 | +import com.igormaznitsa.jcp.expression.Value; |
| 22 | +import com.igormaznitsa.jcp.expression.ValueType; |
| 23 | +import com.igormaznitsa.jcp.utils.PreprocessorUtils; |
| 24 | + |
| 25 | +import com.igormaznitsa.meta.annotation.MustNotContainNull; |
| 26 | + |
| 27 | +/** |
| 28 | + * The class implements escape function handler to escape strings to be used in Go. |
| 29 | + * |
| 30 | + * @author Igor Maznitsa (igor.maznitsa@igormaznitsa.com) |
| 31 | + */ |
| 32 | +public final class FunctionSTR2GO extends AbstractFunction { |
| 33 | + |
| 34 | + private static final ValueType[][] ARG_TYPES = new ValueType[][]{{ValueType.STRING, ValueType.BOOLEAN}}; |
| 35 | + |
| 36 | + @Override |
| 37 | + @Nonnull |
| 38 | + public String getName() { |
| 39 | + return "str2go"; |
| 40 | + } |
| 41 | + |
| 42 | + @Nonnull |
| 43 | + public Value executeStrBool(@Nonnull final PreprocessorContext context, @Nonnull final Value source, @Nonnull final Value splitAndQuoteLines) { |
| 44 | + if (splitAndQuoteLines.asBoolean()){ |
| 45 | + final boolean endsWithNextLine = source.asString().endsWith("\n"); |
| 46 | + final String [] splitted = source.asString().split("\\n"); |
| 47 | + final StringBuilder result = new StringBuilder(source.asString().length()*2); |
| 48 | + final String nextLineChars = PreprocessorUtils.getNextLineCodes(); |
| 49 | + |
| 50 | + int index = 0; |
| 51 | + for(final String s : splitted){ |
| 52 | + final boolean last = ++index == splitted.length; |
| 53 | + if (result.length()>0){ |
| 54 | + result.append(nextLineChars).append('+'); |
| 55 | + } |
| 56 | + result.append('\"').append(escapeGo(s)); |
| 57 | + if (last ){ |
| 58 | + result.append(endsWithNextLine ? "\\n\"":"\""); |
| 59 | + }else{ |
| 60 | + result.append("\\n\""); |
| 61 | + } |
| 62 | + } |
| 63 | + return Value.valueOf(result.toString()); |
| 64 | + }else{ |
| 65 | + return Value.valueOf(escapeGo(source.asString())); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static String toUnicode(final char c) { |
| 70 | + final StringBuilder result = new StringBuilder(4); |
| 71 | + final String hex = Integer.toHexString(c); |
| 72 | + |
| 73 | + for(int i=0;i<4-hex.length();i++){ |
| 74 | + result.append('0'); |
| 75 | + } |
| 76 | + |
| 77 | + result.append(hex); |
| 78 | + |
| 79 | + return result.toString(); |
| 80 | + } |
| 81 | + |
| 82 | + @Nonnull |
| 83 | + private static String escapeGo(@Nonnull final String value) { |
| 84 | + final StringBuilder result = new StringBuilder(); |
| 85 | + |
| 86 | + for(final char c : value.toCharArray()) { |
| 87 | + switch(c) { |
| 88 | + case '\u0007' : result.append("\\a");break; |
| 89 | + case '\u000b' : result.append("\\v");break; |
| 90 | + case '\b' : result.append("\\b");break; |
| 91 | + case '\f' : result.append("\\f");break; |
| 92 | + case '\n' : result.append("\\n");break; |
| 93 | + case '\r' : result.append("\\r");break; |
| 94 | + case '\t' : result.append("\\t");break; |
| 95 | + case '\\' : result.append("\\\\");break; |
| 96 | + case '\'' : result.append("\\\'");break; |
| 97 | + case '\"' : result.append("\\\"");break; |
| 98 | + case ' ' : result.append(" ");break; |
| 99 | + default: { |
| 100 | + if (Character.isISOControl(c) || Character.isWhitespace(c) || c > 0xFF){ |
| 101 | + result.append("\\u").append(toUnicode(c)); |
| 102 | + } else { |
| 103 | + result.append(c); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return result.toString(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public int getArity() { |
| 114 | + return 2; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + @Nonnull |
| 119 | + @MustNotContainNull |
| 120 | + public ValueType[][] getAllowedArgumentTypes() { |
| 121 | + return ARG_TYPES; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + @Nonnull |
| 126 | + public String getReference() { |
| 127 | + return "escapes a string to be compatible with GoLang"; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + @Nonnull |
| 132 | + public ValueType getResultType() { |
| 133 | + return ValueType.STRING; |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments