Skip to content

Commit

Permalink
objectionary#3238 resolved other violations
Browse files Browse the repository at this point in the history
  • Loading branch information
c71n93 committed Aug 6, 2024
1 parent 0308ff0 commit 83e7568
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 36 deletions.
13 changes: 8 additions & 5 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOfs/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
*/
package EOorg.EOeolang.EOfs; // NOPMD

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import org.eolang.ExFailure;
Expand Down Expand Up @@ -67,12 +68,13 @@ private Files() {
* @param name Name of the file
* @throws FileNotFoundException If can't open file
*/
void open(final String name) throws FileNotFoundException {
void open(final String name) throws IOException {
final Path path = Paths.get(name);
this.streams.putIfAbsent(
name,
new Object[] {
new FileInputStream(name),
new FileOutputStream(name, true),
java.nio.file.Files.newInputStream(path),
java.nio.file.Files.newOutputStream(path, StandardOpenOption.APPEND),
}
);
}
Expand All @@ -84,6 +86,7 @@ void open(final String name) throws FileNotFoundException {
* @return Read bytes
* @throws IOException If fails to read
*/
@SuppressWarnings({"PMD.AssignmentInOperand", "PMD.CloseResource"})
byte[] read(final String name, final int size) throws IOException {
synchronized (this.streams) {
if (!this.streams.containsKey(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ public final class EOstring$EOslice extends PhDefault implements Atom {
@Override
public Phi lambda() {
final int start = new Dataized(this.take("start")).asNumber().intValue();
final int length = new Dataized(this.take("len")).asNumber().intValue();
if (start < 0) {
throw new ExFailure(
"Start index must be greater than 0 but was %d",
start
);
}
final int length = new Dataized(this.take("len")).asNumber().intValue();
final int end = length + start;
if (start > end) {
throw new ExFailure(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public final class EOregex$EOcompiled extends PhDefault implements Atom {
public Phi lambda() throws Exception {
final Phi regex = this.take(Attr.RHO);
final String expression = new Dataized(regex.take("expression")).asString();
final StringBuilder builder = new StringBuilder();
if (!expression.startsWith("/")) {
throw new ExFailure("Wrong regex syntax: \"/\" is missing");
}
final int last = expression.lastIndexOf("/");
final int last = expression.lastIndexOf('/');
final StringBuilder builder = new StringBuilder();
if (!expression.endsWith("/")) {
builder.append("(?").append(expression.substring(last + 1)).append(")");
builder.append("(?").append(expression.substring(last + 1)).append(')');
}
builder.append(expression, 1, last);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,30 @@
@SuppressWarnings("PMD.AvoidDollarSigns")
public final class EOregex$EOpattern$EOmatch$EOmatched_from_index extends PhDefault
implements Atom {

/**
* Start.
*/
public static final String START = "start";

/**
* Position.
*/
public static final String POSITION = "position";

/**
* Ctor.
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
public EOregex$EOpattern$EOmatch$EOmatched_from_index() {
this.add("position", new AtVoid("position"));
this.add("start", new AtVoid("start"));
this.add(
EOregex$EOpattern$EOmatch$EOmatched_from_index.POSITION,
new AtVoid(EOregex$EOpattern$EOmatch$EOmatched_from_index.POSITION)
);
this.add(
EOregex$EOpattern$EOmatch$EOmatched_from_index.START,
new AtVoid(EOregex$EOpattern$EOmatch$EOmatched_from_index.START)
);
}

@Override
Expand All @@ -69,14 +86,19 @@ public Phi lambda() throws Exception {
final Matcher matcher = ((Pattern) new ObjectInputStream(bais).readObject()).matcher(
new Dataized(match.take("txt")).asString()
);
final Phi start = this.take("start");
final Double from = new Dataized(this.take("start")).asNumber();
final Phi start = this.take(EOregex$EOpattern$EOmatch$EOmatched_from_index.START);
final Double from = new Dataized(
this.take(EOregex$EOpattern$EOmatch$EOmatched_from_index.START)
).asNumber();
final boolean found = matcher.find(from.intValue());
final Phi result;
if (found) {
result = match.take("matched");
result.put("position", this.take("position"));
result.put("start", start);
result.put(
EOregex$EOpattern$EOmatch$EOmatched_from_index.POSITION,
this.take(EOregex$EOpattern$EOmatch$EOmatched_from_index.POSITION)
);
result.put(EOregex$EOpattern$EOmatch$EOmatched_from_index.START, start);
result.put("from", new Data.ToPhi(matcher.start()));
result.put("to", new Data.ToPhi(matcher.end()));
final Phi[] groups;
Expand All @@ -91,7 +113,10 @@ public Phi lambda() throws Exception {
result.put("groups", new Data.ToPhi(groups));
} else {
result = match.take("not-matched");
result.put("position", this.take("position"));
result.put(
EOregex$EOpattern$EOmatch$EOmatched_from_index.POSITION,
this.take(EOregex$EOpattern$EOmatch$EOmatched_from_index.POSITION)
);
}
return result;
}
Expand Down
24 changes: 12 additions & 12 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOtxt/EOsprintf.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@
public final class EOsprintf extends PhDefault implements Atom {
/**
* Character conversion.
* @checkstyle IndentationCheck (15 lines)
*/
private static final Map<Character, Function<Dataized, Object>> CONVERSION =
new HashMap<Character, Function<Dataized, Object>>(5) {{
put('s', Dataized::asString);
put('d', element -> element.asNumber().longValue());
put('f', Dataized::asNumber);
put('x', element -> EOsprintf.bytesToHex(element.take()));
put('b', Dataized::asBool);
}};
private static final Map<Character, Function<Dataized, Object>> CONVERSION = new HashMap<>();

static {
EOsprintf.CONVERSION.put('s', Dataized::asString);
EOsprintf.CONVERSION.put('d', element -> element.asNumber().longValue());
EOsprintf.CONVERSION.put('f', Dataized::asNumber);
EOsprintf.CONVERSION.put('x', element -> EOsprintf.bytesToHex(element.take()));
EOsprintf.CONVERSION.put('b', Dataized::asBool);
}

/**
* Percent sign.
Expand Down Expand Up @@ -103,14 +103,14 @@ public Phi lambda() throws Exception {
);
}
final char sym = pattern.charAt(idx + 1);
if (sym != EOsprintf.PERCENT) {
if (sym == EOsprintf.PERCENT) {
pattern = pattern.substring(idx + 1);
} else {
final Phi taken = retriever.copy();
taken.put(0, new Data.ToPhi(index));
arguments.add(EOsprintf.formatted(sym, new Dataized(taken)));
++index;
pattern = pattern.substring(idx + 2);
} else {
pattern = pattern.substring(idx + 1);
}
}
return new ToPhi(
Expand Down
17 changes: 9 additions & 8 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOtxt/EOsscanf.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@
public final class EOsscanf extends PhDefault implements Atom {
/**
* Character conversion.
* @checkstyle IndentationCheck (10 lines)
*/
private static final Map<Character, Function<String, Phi>> CONVERSION =
new HashMap<Character, Function<String, Phi>>(3) {{
put('s', ToPhi::new);
put('d', str -> new Data.ToPhi(Long.parseLong(str)));
put('f', str -> new Data.ToPhi(Double.parseDouble(str)));
}};
private static final Map<Character, Function<String, Phi>> CONVERSION = new HashMap<>();

static {
EOsscanf.CONVERSION.put('s', ToPhi::new);
EOsscanf.CONVERSION.put('d', str -> new Data.ToPhi(Long.parseLong(str)));
EOsscanf.CONVERSION.put('f', str -> new Data.ToPhi(Double.parseDouble(str)));
}

/**
* Percent sign.
Expand All @@ -80,9 +80,9 @@ public EOsscanf() {
}

@Override
@SuppressWarnings("PMD.CognitiveComplexity")
public Phi lambda() throws Exception {
final String format = new Dataized(this.take("format")).asString();
final List<Phi> output = new ArrayList<>(0);
final StringBuilder regex = new StringBuilder();
boolean literal = false;
for (int idx = 0; idx < format.length(); ++idx) {
Expand Down Expand Up @@ -119,6 +119,7 @@ public Phi lambda() throws Exception {
new Dataized(this.take("read")).asString()
);
String frmt = format;
final List<Phi> output = new ArrayList<>(0);
if (matcher.find()) {
int index = 1;
while (true) {
Expand Down

0 comments on commit 83e7568

Please sign in to comment.