Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bd063a4

Browse files
committed
refactoring
1 parent 1563972 commit bd063a4

File tree

9 files changed

+192
-113
lines changed

9 files changed

+192
-113
lines changed

‎src/main/java/com/igormaznitsa/prologparser/terms/PrologTerm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public String toString() {
203203
if (this.quotation == NONE) {
204204
result = this.text;
205205
} else {
206-
result = this.quotation.quoteString(this.text);
206+
result = this.quotation.formatString(this.text);
207207
}
208208
return result;
209209
}

‎src/main/java/com/igormaznitsa/prologparser/terms/Quotation.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222
package com.igormaznitsa.prologparser.terms;
2323

24-
import static com.igormaznitsa.prologparser.utils.StringUtils.escapeString;
25-
24+
import com.igormaznitsa.prologparser.utils.StringUtils;
2625
import java.util.List;
2726

2827
/**
@@ -32,49 +31,62 @@ public enum Quotation {
3231
/**
3332
* Term doesn't have any quotation.
3433
*/
35-
NONE(""),
34+
NONE("%s", true),
3635
/**
3736
* Term is single quotation
3837
* example: 'hello'
3938
*/
40-
SINGLE("'"),
39+
SINGLE("'%s'", true),
4140
/**
4241
* Term is double quotation
4342
* example: "hello"
4443
*/
45-
DOUBLE("\""),
44+
DOUBLE("\"%s\"", true),
4645
/**
4746
* Term is back tick quotation
4847
* example: `hello`
4948
*/
50-
BACK_TICK("`"),
49+
BACK_TICK("`%s`", true),
5150
/**
5251
* Special variant shows that content is line comment
5352
*
5453
* @since 2.2.0
5554
*/
56-
COMMENT_LINE("%"),
55+
COMMENT_LINE("%%%s", false),
5756
/**
5857
* Special variant shows that content is block comment
5958
*
6059
* @since 2.2.0
6160
*/
62-
COMMENT_BLOCK("/*");
61+
COMMENT_BLOCK("/*%s*/", false);
6362

64-
private final String quotationMark;
63+
private final String formatPattern;
64+
private final boolean escapeString;
6565
public static final List<Quotation> VALUES = List.of(Quotation.values());
6666

67-
Quotation(final String quotationMark) {
68-
this.quotationMark = quotationMark;
67+
Quotation(final String formatPattern, final boolean escapeString) {
68+
this.formatPattern = formatPattern;
69+
this.escapeString = escapeString;
70+
}
71+
72+
/**
73+
* Check that string should be escaped.
74+
*
75+
* @return true if escape string required, false otherwise
76+
* @since 2.2.0
77+
*/
78+
public boolean isEscapeString() {
79+
return this.escapeString;
6980
}
7081

7182
/**
7283
* Get quotation mark.
7384
*
7485
* @return the quotation mark as string
86+
* @since 2.2.0
7587
*/
76-
public String getQuotationMark() {
77-
return this.quotationMark;
88+
public String getFormatPattern() {
89+
return this.formatPattern;
7890
}
7991

8092
/**
@@ -83,14 +95,9 @@ public String getQuotationMark() {
8395
* @param str string to be quoted, can be null
8496
* @return quoted string
8597
*/
86-
public String quoteString(final String str) {
87-
switch (this) {
88-
case COMMENT_LINE:
89-
return COMMENT_LINE.quotationMark + str;
90-
case COMMENT_BLOCK:
91-
return COMMENT_BLOCK.quotationMark + str + "*/";
92-
default:
93-
return this.quotationMark + escapeString(str == null ? "" : str, this) + this.quotationMark;
94-
}
98+
public String formatString(String str) {
99+
final String nonNullStr = str == null ? "" : str;
100+
return String.format(this.formatPattern,
101+
this.escapeString ? StringUtils.escapeString(nonNullStr, this) : nonNullStr);
95102
}
96103
}

‎src/main/java/com/igormaznitsa/prologparser/utils/StringUtils.java

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -167,69 +167,80 @@ public static boolean isCharNotAppropriateForHexNum(final char chr) {
167167
return (chr < '0' || chr > '9') && (chr < 'a' || chr > 'f') && (chr < 'A' || chr > 'F');
168168
}
169169

170-
public static String escapeString(final String str, final Quotation quotingType) {
171-
final StringBuilder result = new StringBuilder(str.length() << 1);
170+
public static String escapeString(final String str, final Quotation quotation) {
171+
final StringBuilder buffer = new StringBuilder(str.length() << 1);
172172

173173
final int strLen = str.length();
174174
for (int i = 0; i < strLen; i++) {
175175
final char chr = str.charAt(i);
176176
switch (chr) {
177177
case 7:
178-
result.append("\\a");
178+
buffer.append("\\a");
179179
break;
180180
case 8:
181-
result.append("\\b");
181+
buffer.append("\\b");
182182
break;
183183
case '\f':
184-
result.append("\\f");
184+
buffer.append("\\f");
185+
break;
186+
case '\\':
187+
buffer.append("\\\\");
185188
break;
186189
case '\n':
187-
result.append("\\n");
190+
buffer.append("\\n");
188191
break;
189192
case '\r':
190-
result.append("\\r");
193+
buffer.append("\\r");
191194
break;
192195
case '`':
193-
if (quotingType == Quotation.BACK_TICK) {
194-
result.append("\\`");
196+
if (quotation == Quotation.BACK_TICK) {
197+
buffer.append("\\`");
195198
} else {
196-
result.append('`');
199+
buffer.append('`');
197200
}
198201
break;
199202
case 27:
200-
result.append("\\e");
203+
buffer.append("\\e");
201204
break;
202205
case '\t':
203-
result.append("\\t");
206+
buffer.append("\\t");
204207
break;
205208
case '\"':
206-
if (quotingType == Quotation.DOUBLE) {
207-
result.append("\\\"");
209+
if (quotation == Quotation.DOUBLE) {
210+
buffer.append("\\\"");
208211
} else {
209-
result.append('\"');
212+
buffer.append('\"');
210213
}
211214
break;
212215
case '\'':
213-
if (quotingType == Quotation.SINGLE) {
214-
result.append("\\'");
216+
if (quotation == Quotation.SINGLE) {
217+
buffer.append("\\'");
215218
} else {
216-
result.append('\'');
219+
buffer.append('\'');
217220
}
218221
break;
219222
case 11:
220-
result.append("\\v");
223+
buffer.append("\\v");
221224
break;
222225
default:
223-
if (Character.isISOControl(chr)) {
224-
result.append('\\').append(Integer.toOctalString(chr)).append('\\');
225-
} else {
226-
result.append(chr);
226+
switch (quotation) {
227+
case COMMENT_LINE:
228+
case COMMENT_BLOCK:
229+
buffer.append(chr);
230+
break;
231+
default: {
232+
if (Character.isISOControl(chr)) {
233+
buffer.append('\\').append(Integer.toOctalString(chr)).append('\\');
234+
} else {
235+
buffer.append(chr);
236+
}
237+
}
238+
break;
227239
}
228-
break;
229240
}
230241
}
231242

232-
return result.toString();
243+
return buffer.toString();
233244
}
234245

235246
/**

‎src/test/java/com/igormaznitsa/prologparser/BigDataTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,25 @@
2727
import com.igormaznitsa.prologparser.tokenizer.Op;
2828
import java.io.IOException;
2929
import java.io.InputStreamReader;
30+
import java.io.StringReader;
3031
import org.junit.jupiter.api.Test;
3132

3233
class BigDataTest extends AbstractIntegrationTest {
3334

3435
@Test
35-
void testBigSource_ClausesSplitted() {
36-
final int CLAUSES = 1000;
37-
assertEquals(CLAUSES,
38-
new GenericPrologParser(new InputStreamReader(new PrologSourceKoi7Generator(CLAUSES, true)),
36+
void testBigSource_ClausesSplitted() throws Exception {
37+
final int expectedClauses = 1000;
38+
final String text = new PrologSourceKoi7Generator(expectedClauses, true, false).asString();
39+
assertEquals(expectedClauses,
40+
new GenericPrologParser(new StringReader(text),
3941
DefaultParserContext.of(FLAG_NONE, Op.SWI)).stream().count());
4042
}
4143

4244
@Test
4345
void testBigSource_ClausesNotSplitted() {
44-
final int CLAUSES = 1000;
45-
assertEquals(CLAUSES, new GenericPrologParser(
46-
new InputStreamReader(new PrologSourceKoi7Generator(CLAUSES, false)),
46+
final int maxClauses = 1000;
47+
assertEquals(maxClauses, new GenericPrologParser(
48+
new InputStreamReader(new PrologSourceKoi7Generator(maxClauses, false, false)),
4749
DefaultParserContext.of(FLAG_NONE, Op.SWI)).stream().count());
4850
}
4951

‎src/test/java/com/igormaznitsa/prologparser/IntegrationTest.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,18 +1098,19 @@ void testAloneOperatorAsAtom() {
10981098
@Test
10991099
void testUnexpectedlyEndedReadStream() {
11001100
final Random rnd = new Random(12345);
1101-
11021101
final AtomicInteger completedClauseCounter = new AtomicInteger();
1102+
final int maxAttempts = 100;
11031103

1104-
final int ATTEMPTS = 100;
1105-
1106-
for (int i = 0; i < ATTEMPTS; i++) {
1107-
final int numChars = rnd.nextInt(5) + i * 3;
1104+
for (int i = 0; i < maxAttempts; i++) {
1105+
final int maxChars = rnd.nextInt(5) + i * 3;
11081106
assertThrows(PrologParserException.class, () -> {
11091107
final PrologParser parser = parseEd(
11101108
new InputStreamReader(
1111-
new PrologSourceKoi7Generator(rnd.nextBoolean(),
1112-
numChars,
1109+
new PrologSourceKoi7Generator(
1110+
rnd.nextBoolean(),
1111+
maxChars,
1112+
Integer.MAX_VALUE,
1113+
false,
11131114
false), StandardCharsets.UTF_8), new DefaultParserContext(FLAG_BLOCK_COMMENTS));
11141115

11151116
while (parser.hasNext()) {
@@ -1120,7 +1121,7 @@ void testUnexpectedlyEndedReadStream() {
11201121
});
11211122
}
11221123

1123-
assertTrue(completedClauseCounter.get() < Math.round(0.1 * ATTEMPTS));
1124+
assertTrue(completedClauseCounter.get() < Math.round(0.1 * maxAttempts));
11241125
}
11251126

11261127
@Test
@@ -1290,7 +1291,8 @@ void testConformity() {
12901291
assertEquals("writeq('\\n')", parseEd("writeq('\\\n'). % \"\\\\\\n\" ").next().toString());
12911292
assertEquals("* = *", parseEd("* = * .").next().toString());
12921293
assertEquals("[:- - c] = [(:- - c)]", parseEd("[:- -c] = [(:- -c)].").next().toString());
1293-
assertEquals("X = '\\'", parseEd("X = '\\\\' .").next().toString());
1294+
assertEquals("X = \\", parseEd("X = \\.").next().toString());
1295+
assertEquals("X = '\\\\'", parseEd("X = '\\\\' .").next().toString());
12941296
assertEquals("X = `a`", parseEd("X = `a`.").next().toString());
12951297
assertEquals("writeq(- (a * b))", parseEd("writeq(- (a*b)).").next().toString());
12961298
assertEquals("writeq(\\ (a * b))", parseEd("writeq(\\ (a*b)).").next().toString());

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /