Skip to content

Commit dc89b3d

Browse files
committed
minor refactoring and added STR TRIMLINES(STR) function
1 parent 0605289 commit dc89b3d

File tree

9 files changed

+150
-3
lines changed

9 files changed

+150
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# Changelog
1111

1212
- **6.1.2-SNAPSHOT (under development)**
13+
- CORE: added STR TRIMLINES(STR) function to trim lines represented as string and removing empty lines
1314
- CORE: added `/A` command line option (`copyFileAttributes` in Maven and ANT) to copy file attributes
1415
- CORE: added `/ED:` command line option to exclude sub-folders from preprocessing (`excludedFolders` in Maven and ANT) with ANT pattern support.
1516
- CORE: added `/PI` command line option (`preserveIndent` in Maven and ANT), turn on mode to preserve indent when removing `//$` and `//$$`, thanks to [jamuir](https://github.com/jamuir)

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
6.1.2-SNAPSHOT (under development)
2+
- CORE: added STR TRIMLINES(STR) function to trim lines represented as string and removing empty lines
23
- CORE: added `/A` command line option (`copyFileAttributes` in Maven and ANT) to copy file attributes
34
- CORE: added `/ED:` command line option to exclude sub-folders from preprocessing (`excludedFolders` in Maven and ANT) with ANT pattern support.
45
- CORE: added `/PI` command line flag (`preserveIndent` in Maven and ANT), turn on mode to preserve indent when removing `//$` and `//$$`, thanks to @jamuir

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public abstract class AbstractFunction implements ExpressionItem {
6565
new FunctionSTR2XML(),
6666
new FunctionSTR2JAVA(),
6767
new FunctionSTR2GO(),
68+
new FunctionTRIMLINES(),
6869
new FunctionSTRLEN(),
6970
new FunctionISSUBSTR(),
7071
new FunctionIS(),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public String getName() {
4343
public Value executeStrBool(@Nonnull final PreprocessorContext context, @Nonnull final Value source, @Nonnull final Value splitAndQuoteLines) {
4444
if (splitAndQuoteLines.asBoolean()){
4545
final boolean endsWithNextLine = source.asString().endsWith("\n");
46-
final String [] splitted = source.asString().split("\\n");
46+
final String [] splitted = PreprocessorUtils.splitForCharAndHoldEmptyLine(source.asString(),'\n');
4747
final StringBuilder result = new StringBuilder(source.asString().length()*2);
4848
final String nextLineChars = PreprocessorUtils.getNextLineCodes();
4949

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public String getName() {
4545
public Value executeStrBool(@Nonnull final PreprocessorContext context, @Nonnull final Value source, @Nonnull final Value splitAndQuoteLines) {
4646
if (splitAndQuoteLines.asBoolean()){
4747
final boolean endsWithNextLine = source.asString().endsWith("\n");
48-
final String [] splitted = source.asString().split("\\n");
48+
final String [] splitted = PreprocessorUtils.splitForCharAndHoldEmptyLine(source.asString(),'\n');
4949
final StringBuilder result = new StringBuilder(source.asString().length()*2);
5050
final String nextLineChars = PreprocessorUtils.getNextLineCodes();
5151

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
24+
import com.igormaznitsa.jcp.utils.PreprocessorUtils;
25+
26+
/**
27+
* The class implements the TRIMLINES function handler
28+
*
29+
* @author Igor Maznitsa ([email protected])
30+
*/
31+
public final class FunctionTRIMLINES extends AbstractStrConverter {
32+
33+
@Override
34+
@Nonnull
35+
public String getName() {
36+
return "trimlines";
37+
}
38+
39+
@Override
40+
@Nonnull
41+
public Value executeStr(@Nonnull final PreprocessorContext context, @Nonnull final Value value) {
42+
final String text = value.asString();
43+
final StringBuilder result = new StringBuilder(text.length());
44+
45+
for(final String s : PreprocessorUtils.splitForChar(text, '\n')){
46+
final String trimmed = s.trim();
47+
if (!trimmed.isEmpty()) {
48+
if (result.length()>0) result.append(PreprocessorUtils.getNextLineCodes());
49+
result.append(trimmed);
50+
}
51+
}
52+
53+
return Value.valueOf(result.toString());
54+
}
55+
56+
@Override
57+
@Nonnull
58+
public String getReference() {
59+
return "split string to lines and trim ecah line, empty lines will be removed";
60+
}
61+
62+
@Override
63+
@Nonnull
64+
public ValueType getResultType() {
65+
return ValueType.STRING;
66+
}
67+
}

src/main/java/com/igormaznitsa/jcp/utils/PreprocessorUtils.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,13 @@ public static String[] splitForEqualChar(@Nonnull final String string) {
382382
return result;
383383
}
384384

385+
@Nonnull
386+
@MustNotContainNull
387+
public static String[] splitForCharAndHoldEmptyLine(@Nonnull final String string, final char delimiter) {
388+
String [] result = splitForChar(string, delimiter);
389+
return result.length == 0 ? new String[]{""} : result;
390+
}
391+
385392
@Nonnull
386393
@MustNotContainNull
387394
public static String[] splitForChar(@Nonnull final String string, final char delimiter) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public void testExecution_wrongCases() throws Exception {
4848
assertFunctionException("str2java(true,\"ss\")");
4949
assertFunctionException("str2java(\"ss\",3)");
5050
assertDestinationFolderEmpty();
51-
5251
}
5352

5453
@Override
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 FunctionTRIMLINESTest extends AbstractFunctionTest {
25+
26+
private static final FunctionTRIMLINES HANDLER = new FunctionTRIMLINES();
27+
28+
@Test
29+
public void testExecution_WorkingCases() throws Exception {
30+
assertFunction("trimlines(\"\")", Value.valueOf(""));
31+
assertFunction("trimlines(\"hello world\")", Value.valueOf("hello world"));
32+
assertFunction("trimlines(\" hello \n \n world\n\")", Value.valueOf("hello"+PreprocessorUtils.getNextLineCodes()+"world"));
33+
assertDestinationFolderEmpty();
34+
}
35+
36+
@Test
37+
public void testExecution_wrongCases() throws Exception {
38+
assertFunctionException("trimlines()");
39+
assertFunctionException("trimlines(1)");
40+
assertFunctionException("trimlines(true)");
41+
assertFunctionException("trimlines(true,\"ss\")");
42+
assertFunctionException("trimlines(\"ss\",3)");
43+
assertDestinationFolderEmpty();
44+
45+
}
46+
47+
@Override
48+
public void testName() {
49+
assertEquals("trimlines", HANDLER.getName());
50+
}
51+
52+
@Override
53+
public void testReference() {
54+
assertReference(HANDLER);
55+
}
56+
57+
@Override
58+
public void testArity() {
59+
assertEquals(1, HANDLER.getArity());
60+
}
61+
62+
@Override
63+
public void testAllowedArgumentTypes() {
64+
assertAllowedArguments(HANDLER, new ValueType[][]{{ValueType.STRING}});
65+
}
66+
67+
@Override
68+
public void testResultType() {
69+
assertEquals(ValueType.STRING, HANDLER.getResultType());
70+
}
71+
}

0 commit comments

Comments
 (0)