mirror of
https://github.com/objectionary/eo.git
synced 2024-09-11 12:45:42 +03:00
feat(#3251): checkstyle
This commit is contained in:
parent
24da1ceb81
commit
498b25c780
@ -57,6 +57,44 @@ public final class EOsprintf extends PhDefault implements Atom {
|
||||
this.add("args", new AtVoid("args"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Phi lambda() throws Exception {
|
||||
final String format = new Param(this, "format").strong(String.class);
|
||||
final Phi args = this.take("args");
|
||||
final Phi retriever = args.take("at");
|
||||
final Long length = new Param(args, "length").strong(Long.class);
|
||||
final List<Object> arguments = new ArrayList<>(0);
|
||||
String pattern = format;
|
||||
long index = 0;
|
||||
while (true) {
|
||||
final int idx = pattern.indexOf('%');
|
||||
if (idx == -1) {
|
||||
break;
|
||||
}
|
||||
if (index == length) {
|
||||
throw new ExFailure(
|
||||
String.format(
|
||||
"The amount of arguments %d does not match the amount of format occurrences %d",
|
||||
length,
|
||||
EOsprintf.percents(format)
|
||||
)
|
||||
);
|
||||
}
|
||||
final char sym = pattern.charAt(idx + 1);
|
||||
final Phi taken = retriever.copy();
|
||||
taken.put(0, new Data.ToPhi(++index));
|
||||
arguments.add(EOsprintf.formatted(sym, new Dataized(taken)));
|
||||
pattern = pattern.substring(idx + 1);
|
||||
}
|
||||
return new ToPhi(
|
||||
String.format(
|
||||
Locale.US,
|
||||
format.replaceAll("%x", "%s"),
|
||||
arguments.toArray()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert byte array to hex string.
|
||||
* @param bytes Byte array
|
||||
@ -113,49 +151,11 @@ public final class EOsprintf extends PhDefault implements Atom {
|
||||
*/
|
||||
private static int percents(final String str) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < str.length(); ++i) {
|
||||
if (str.charAt(i) == '%') {
|
||||
count++;
|
||||
for (int idx = 0; idx < str.length(); ++idx) {
|
||||
if (str.charAt(idx) == '%') {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Phi lambda() throws Exception {
|
||||
final String format = new Param(this, "format").strong(String.class);
|
||||
final Phi args = this.take("args");
|
||||
final Phi retriever = args.take("at");
|
||||
final Long length = new Param(args, "length").strong(Long.class);
|
||||
final List<Object> arguments = new ArrayList<>(0);
|
||||
String pattern = format;
|
||||
long index = 0;
|
||||
while (true) {
|
||||
final int idx = pattern.indexOf('%');
|
||||
if (idx == -1) {
|
||||
break;
|
||||
}
|
||||
if (index == length) {
|
||||
throw new ExFailure(
|
||||
String.format(
|
||||
"The amount of arguments %d does not match the amount of format occurrences %d",
|
||||
length,
|
||||
EOsprintf.percents(format)
|
||||
)
|
||||
);
|
||||
}
|
||||
final char sym = pattern.charAt(idx + 1);
|
||||
final Phi taken = retriever.copy();
|
||||
taken.put(0, new Data.ToPhi(index++));
|
||||
arguments.add(EOsprintf.formatted(sym, new Dataized(taken)));
|
||||
pattern = pattern.substring(idx + 1);
|
||||
}
|
||||
return new ToPhi(
|
||||
String.format(
|
||||
Locale.US,
|
||||
format.replaceAll("%x", "%s"),
|
||||
arguments.toArray()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016-2024 Objectionary.com
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* EO runtime, TXT.
|
||||
*
|
||||
* @since 0.39
|
||||
* @checkstyle PackageNameCheck (4 lines)
|
||||
*/
|
||||
package EOorg.EOeolang.EOtxt;
|
@ -142,6 +142,7 @@ public interface Data {
|
||||
* @param obj Object to convert
|
||||
* @return Constructed Phi
|
||||
*/
|
||||
@SuppressWarnings("PMD.CognitiveComplexity")
|
||||
private static Phi toPhi(final Object obj) {
|
||||
final Phi phi;
|
||||
final Phi eolang = Phi.Φ.take("org").take("eolang");
|
||||
@ -219,7 +220,6 @@ public interface Data {
|
||||
"PMD.NPathComplexity"
|
||||
})
|
||||
private static String unescapeJavaString(final String str) {
|
||||
|
||||
final StringBuilder unescaped = new StringBuilder(str.length());
|
||||
for (int idx = 0; idx < str.length(); ++idx) {
|
||||
char chr = str.charAt(idx);
|
||||
|
@ -27,7 +27,7 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* EO runtime, tests.
|
||||
* EO-io, tests.
|
||||
*
|
||||
* @since 0.1
|
||||
*/
|
||||
|
@ -48,13 +48,18 @@ final class EOsprintfTest {
|
||||
void returnsValidFormattedString() {
|
||||
final Phi sprintf = new EOsprintf();
|
||||
sprintf.put(0, new Data.ToPhi("string %s, bytes %x, float %f, int %d, bool %b"));
|
||||
sprintf.put(1, new Data.ToPhi(new Phi[] {
|
||||
new Data.ToPhi("Jeff"),
|
||||
new Data.ToPhi("Jeff".getBytes(StandardCharsets.UTF_8)),
|
||||
new Data.ToPhi(42.3),
|
||||
new Data.ToPhi(14L),
|
||||
new Data.ToPhi(false)
|
||||
}));
|
||||
sprintf.put(
|
||||
1,
|
||||
new Data.ToPhi(
|
||||
new Phi[] {
|
||||
new Data.ToPhi("Jeff"),
|
||||
new Data.ToPhi("Jeff".getBytes(StandardCharsets.UTF_8)),
|
||||
new Data.ToPhi(42.3),
|
||||
new Data.ToPhi(14L),
|
||||
new Data.ToPhi(false),
|
||||
}
|
||||
)
|
||||
);
|
||||
MatcherAssert.assertThat(
|
||||
"All types should be formatted successfully",
|
||||
new Dataized(sprintf).take(String.class),
|
||||
@ -68,9 +73,7 @@ final class EOsprintfTest {
|
||||
void failsIfAmountOfArgumentsDoesNotMatch() {
|
||||
final Phi sprintf = new EOsprintf();
|
||||
sprintf.put(0, new Data.ToPhi("%s%s"));
|
||||
sprintf.put(1, new Data.ToPhi(new Phi[] {
|
||||
new Data.ToPhi("Hey")
|
||||
}));
|
||||
sprintf.put(1, new Data.ToPhi(new Phi[] {new Data.ToPhi("Hey")}));
|
||||
final Throwable exception = Assertions.assertThrows(
|
||||
EOerror.ExError.class,
|
||||
() -> new Dataized(sprintf).take(),
|
||||
@ -89,10 +92,15 @@ final class EOsprintfTest {
|
||||
void doesNotFailIfArgumentsMoreThanFormats() {
|
||||
final Phi sprintf = new EOsprintf();
|
||||
sprintf.put(0, new Data.ToPhi("%s"));
|
||||
sprintf.put(1, new Data.ToPhi(new Phi[] {
|
||||
new Data.ToPhi("Hey"),
|
||||
new Data.ToPhi("Hey")
|
||||
}));
|
||||
sprintf.put(
|
||||
1,
|
||||
new Data.ToPhi(
|
||||
new Phi[] {
|
||||
new Data.ToPhi("Hey"),
|
||||
new Data.ToPhi("Hey"),
|
||||
}
|
||||
)
|
||||
);
|
||||
MatcherAssert.assertThat(
|
||||
"The second argument should be ignored",
|
||||
new Dataized(sprintf).take(String.class),
|
||||
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016-2024 Objectionary.com
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @checkstyle PackageNameCheck (20 lines)
|
||||
*/
|
||||
|
||||
/**
|
||||
* EO-txt, tests.
|
||||
*
|
||||
* @since 0.39
|
||||
*/
|
||||
package EOorg.EOeolang.EOtxt;
|
@ -41,7 +41,6 @@ import org.eolang.jucs.ClasspathSource;
|
||||
import org.hamcrest.MatcherAssert;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
Loading…
Reference in New Issue
Block a user