Skip to content

Commit c6a2f71

Browse files
committed
Добавлена функция std::stripMargin
1 parent ed16408 commit c6a2f71

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,11 @@ sonarqube {
133133
property "sonar.projectKey", "aNNiMON_Own-Programming-Language-Tutorial"
134134
property "sonar.host.url", "https://sonarcloud.io"
135135
}
136+
}
137+
138+
test {
139+
testLogging {
140+
events "passed", "skipped", "failed"
141+
exceptionFormat "full"
142+
}
136143
}

src/main/java/com/annimon/ownlang/modules/std/StringFunctions.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.annimon.ownlang.lib.Arguments;
44
import com.annimon.ownlang.lib.ArrayValue;
55
import com.annimon.ownlang.lib.NumberValue;
6+
import com.annimon.ownlang.lib.StringValue;
67
import com.annimon.ownlang.lib.Value;
78
import java.io.UnsupportedEncodingException;
89

@@ -31,4 +32,54 @@ static Value parseLong(Value[] args) {
3132
final int radix = (args.length == 2) ? args[1].asInt() : 10;
3233
return NumberValue.of(Long.parseLong(args[0].asString(), radix));
3334
}
35+
36+
static Value stripMargin(Value[] args) {
37+
Arguments.checkOrOr(1, 2, args.length);
38+
final String input = args[0].asString();
39+
final String marginPrefix = (args.length == 2) ? args[1].asString() : "|";
40+
if (!input.contains(marginPrefix)) {
41+
return args[0];
42+
}
43+
final String[] lines = input.split("\\r?\\n");
44+
45+
// First blank line is omitted
46+
final StringBuilder sb = new StringBuilder();
47+
final int firstLineIndex = (isBlank(lines[0])) ? 1 : 0;
48+
final int lastLineIndex = lines.length - 1;
49+
int index = firstLineIndex;
50+
while (true) {
51+
sb.append(strip(lines[index], marginPrefix));
52+
if (++index >= lastLineIndex) break;
53+
sb.append('\n');
54+
}
55+
// Process last line
56+
if (lastLineIndex >= (firstLineIndex + 1) && !isBlank(lines[lastLineIndex])) {
57+
sb.append('\n').append(strip(lines[lastLineIndex], marginPrefix));
58+
}
59+
return new StringValue(sb.toString());
60+
}
61+
62+
private static String strip(String str, String marginPrefix) {
63+
final int nonBlankIndex = firstNonBlankIndex(str);
64+
if (str.startsWith(marginPrefix, nonBlankIndex)) {
65+
return str.substring(nonBlankIndex + marginPrefix.length());
66+
} else {
67+
return str;
68+
}
69+
}
70+
71+
private static boolean isBlank(String str) {
72+
return firstNonBlankIndex(str) == str.length();
73+
}
74+
75+
private static int firstNonBlankIndex(String str) {
76+
final int length = str.length();
77+
for (int index = 0; index < length; index++) {
78+
final char ch = str.charAt(index);
79+
if (ch != ' ' && ch != '\t' && !Character.isWhitespace(ch)) {
80+
return index;
81+
}
82+
}
83+
return length;
84+
}
3485
}

src/main/java/com/annimon/ownlang/modules/std/std.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public void init() {
5555
Functions.set("replaceFirst", new std_replacefirst());
5656
Functions.set("parseInt", StringFunctions::parseInt);
5757
Functions.set("parseLong", StringFunctions::parseLong);
58+
Functions.set("stripMargin", StringFunctions::stripMargin);
5859

5960
// Arrays and maps
6061
Functions.set("newarray", new std_newarray());
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use "std"
2+
3+
def testStripMargin() {
4+
testCases = [
5+
"|abc".stripMargin(),
6+
" |abc".stripMargin(),
7+
"
8+
|abc".stripMargin(),
9+
"|abc
10+
".stripMargin(),
11+
"
12+
|abc
13+
".stripMargin(),
14+
"abc".stripMargin(""),
15+
"abc".stripMargin(),
16+
"#abc".stripMargin("#"),
17+
"| abc".stripMargin("| "),
18+
"
19+
| abc
20+
".stripMargin("| "),
21+
"xxxabc".stripMargin("xxx"),
22+
"
23+
xxxabc".stripMargin("xxx"),
24+
"
25+
xxxabc
26+
".stripMargin("xxx")
27+
]
28+
for actual : testCases {
29+
assertEquals("abc", actual)
30+
}
31+
}
32+
33+
def testStripMargin2() {
34+
assertEquals("123\n456\n789", "123
35+
|456
36+
|789".stripMargin())
37+
assertEquals("123\n456\n789", "|123
38+
|456
39+
|789".stripMargin())
40+
assertEquals("123\n456\n789", "
41+
|123
42+
|456
43+
|789".stripMargin())
44+
assertEquals("123\n456\n789", "
45+
|123
46+
|456
47+
|789
48+
".stripMargin())
49+
assertEquals("123\n456\n789", "
50+
//123
51+
//456
52+
//789
53+
".stripMargin("//"))
54+
}

0 commit comments

Comments
 (0)