Skip to content

Commit b3b6e55

Browse files
committed
added STR2GO function
1 parent 6824e4b commit b3b6e55

File tree

6 files changed

+224
-5
lines changed

6 files changed

+224
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- MAVEN: added `ignoreMissingSources` boolean parameter, allows to skip preprocessing if source folders not found or not provided [#12](https://github.com/raydac/java-comment-preprocessor/issues/12)
1515
- MAVEN: added `skip` boolean parameter, it allows to skip execution, also it is possible to use `-Djcp.preprocess.skip=true` [#13](https://github.com/raydac/java-comment-preprocessor/issues/13)
1616
- CORE: added function `BOOL is(STR,ANY)` to check existence of variable for its name and compare its value with etalon (through string conversion, it will ensure true for `true` and `"true"` case) [#10](https://github.com/raydac/java-comment-preprocessor/issues/10)
17+
- CORE: added `STR str2go(STR)` function to escape strings to be represented in Golang sources
1718
- CORE: improved the BINFILE function, it allows `base64|byte[]|uint8[]|int8` and modifiers `s|d|ds|sd` where s - means splitting to lines and d - means deflate compression
1819

1920
- **6.1.0**

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- MAVEN: added 'ignoreMissingSources' boolean parameter, allows to skip preprocessing if source folders not found or not provided #12
55
- MAVEN: added 'skip'boolean parameter, it allows to skip execution, also it is possible to use `-Djcp.preprocess.skip=true` #13
66
- CORE: added function `BOOL is(STR,ANY)` to check existence of variable for its name and compare its value with etalon (through string conversion, it will ensure true for `true` and `"true"` case), #10
7+
- CORE: added `STR str2go(STR)` function to escape strings to be represented in Golang sources
78
- CORE: improved the BINFILE function, it allows `base64|byte[]|uint8[]|int8` and modifiers `s|d|ds|sd` where s - means splitting to lines and d - means deflate compression
89

910
6.1.0 (03 jul 2016)

src/main/java/com/igormaznitsa/jcp/expression/functions/AbstractFunction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public abstract class AbstractFunction implements ExpressionItem {
6464
new FunctionSTR2JSON(),
6565
new FunctionSTR2XML(),
6666
new FunctionSTR2JAVA(),
67+
new FunctionSTR2GO(),
6768
new FunctionSTRLEN(),
6869
new FunctionISSUBSTR(),
6970
new FunctionIS(),
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2014 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 com.igormaznitsa.jcp.expression.Value;
19+
import com.igormaznitsa.jcp.expression.ValueType;
20+
import com.igormaznitsa.jcp.utils.PreprocessorUtils;
21+
import org.junit.Test;
22+
import static org.junit.Assert.*;
23+
24+
public class FunctionSTR2GOTest extends AbstractFunctionTest {
25+
26+
private static final FunctionSTR2GO HANDLER = new FunctionSTR2GO();
27+
28+
@Test
29+
public void testExecution_NoSplit() throws Exception {
30+
assertFunction("str2go(\"\",false)", Value.valueOf(""));
31+
assertFunction("str2go(\"hello\nworld\",false)", Value.valueOf("hello\\nworld"));
32+
assertDestinationFolderEmpty();
33+
}
34+
35+
@Test
36+
public void testExecution_Split() throws Exception {
37+
assertFunction("str2go(\"\",true)", Value.valueOf("\"\""));
38+
assertFunction("str2go(\"hello\nworld\",true)", Value.valueOf("\"hello\\n\""+PreprocessorUtils.getNextLineCodes()+"+\"world\""));
39+
assertFunction("str2go(\"hello\nworld\n\",true)", Value.valueOf("\"hello\\n\""+PreprocessorUtils.getNextLineCodes()+"+\"world\\n\""));
40+
assertFunction("str2go(\"\u000bhello\u0007\nworld\n\",true)", Value.valueOf("\"\\vhello\\a\\n\""+PreprocessorUtils.getNextLineCodes()+"+\"world\\n\""));
41+
assertFunction("str2go(\"Здравствуй\nМир\n\",true)", Value.valueOf("\"\\u0417\\u0434\\u0440\\u0430\\u0432\\u0441\\u0442\\u0432\\u0443\\u0439\\n\""+PreprocessorUtils.getNextLineCodes()+"+\"\\u041c\\u0438\\u0440\\n\""));
42+
assertDestinationFolderEmpty();
43+
}
44+
45+
@Test
46+
public void testExecution_wrongCases() throws Exception {
47+
assertFunctionException("str2go()");
48+
assertFunctionException("str2go(1,2)");
49+
assertFunctionException("str2go(true)");
50+
assertFunctionException("str2go(true,\"ss\")");
51+
assertFunctionException("str2go(\"ss\",3)");
52+
assertDestinationFolderEmpty();
53+
54+
}
55+
56+
@Override
57+
public void testName() {
58+
assertEquals("str2go", HANDLER.getName());
59+
}
60+
61+
@Override
62+
public void testReference() {
63+
assertReference(HANDLER);
64+
}
65+
66+
@Override
67+
public void testArity() {
68+
assertEquals(2, HANDLER.getArity());
69+
}
70+
71+
@Override
72+
public void testAllowedArgumentTypes() {
73+
assertAllowedArguments(HANDLER, new ValueType[][]{{ValueType.STRING, ValueType.BOOLEAN}});
74+
}
75+
76+
@Override
77+
public void testResultType() {
78+
assertEquals(ValueType.STRING, HANDLER.getResultType());
79+
}
80+
}

src/test/java/com/igormaznitsa/jcp/expression/functions/FunctionSTR2JAVATest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ public void testExecution_Split() throws Exception {
4242

4343
@Test
4444
public void testExecution_wrongCases() throws Exception {
45-
assertFunctionException("str2web()");
46-
assertFunctionException("str2web(1,2)");
47-
assertFunctionException("str2web(true)");
48-
assertFunctionException("str2web(true,\"ss\")");
49-
assertFunctionException("str2web(\"ss\",3)");
45+
assertFunctionException("str2java()");
46+
assertFunctionException("str2java(1,2)");
47+
assertFunctionException("str2java(true)");
48+
assertFunctionException("str2java(true,\"ss\")");
49+
assertFunctionException("str2java(\"ss\",3)");
5050
assertDestinationFolderEmpty();
5151

5252
}

0 commit comments

Comments
 (0)