Skip to content

Commit

Permalink
Simplify string formatting following a suggestion Alexander Weigl
Browse files Browse the repository at this point in the history
  • Loading branch information
unp1 committed Oct 22, 2023
1 parent 3f4ebee commit e19bc2a
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public final String proofToString() {
new StringBuilder((sort() == Sort.FORMULA ? "" : sort().toString()) + " ");
s.append(name());
if (arity() > 0) {
s.append(Strings.formatAsList(argSorts().iterator(), "(", ",", ")"));
s.append(Strings.formatAsList(argSorts(), "(", ",", ")"));
}
s.append(";\n");
return s.toString();
Expand Down
52 changes: 5 additions & 47 deletions key.util/src/main/java/org/key_project/util/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
package org.key_project.util;


import java.util.Iterator;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
* Helper functions for {@link String}s
Expand Down Expand Up @@ -57,51 +58,6 @@ public static boolean isJMLComment(String comment) {
}
}

/**
* outputs the collection represented by the iterator in the format
* <code> open element1 sep element2 sep element3 close</code>
*
* @param it the Iterator over the collection to be printed
* @param open the String used to open the list
* @param sep the String separating the different elements
* @param close the String used to close the list
* @param mapper a Function that maps the elements of type S to their String representation
* @return the CharSequence in the described format
* @param <S> the type of the elements of the iterated collection
*/
public static <S, T> String formatAsList(Iterator<S> it,
CharSequence open, CharSequence sep, CharSequence close,
Function<S, T> mapper) {
final StringBuilder str = new StringBuilder();
str.append(open);
var hasNext = it.hasNext();
while (hasNext) {
str.append(mapper.apply(it.next()));
hasNext = it.hasNext();
if (hasNext) {
str.append(sep);
}
}
str.append(close);
return str.toString();
}

/**
* outputs the collection represented by the iterator in the format
* <code> open element1 sep element2 sep element3 close</code>
*
* @param it the Iterator over the collection to be printed
* @param open the String used to open the list
* @param sep the String separating the different elements
* @param close the String used to close the list
* @return the CharSequence in the described format
* @param <S> the type of the elements of the iterated collection
*/
public static <S> String formatAsList(Iterator<S> it,
CharSequence open, CharSequence sep, CharSequence close) {
return formatAsList(it, open, sep, close, Function.identity());
}

/**
* outputs the collection represented by the iterator in the format
* <code> open element1 sep element2 sep element3 close</code>
Expand All @@ -117,7 +73,9 @@ public static <S> String formatAsList(Iterator<S> it,
public static <S, T> String formatAsList(Iterable<S> it,
CharSequence open, CharSequence sep, CharSequence close,
Function<S, T> mapper) {
return formatAsList(it.iterator(), open, sep, close, mapper);
return StreamSupport.stream(it.spliterator(), false)
.map(a -> mapper.apply(a).toString())
.collect(Collectors.joining(sep, open, close));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public Iterator<ImmutableMapEntry<S, T>> iterator() {
}

public String toString() {
return Strings.formatAsList(iterator(), "[", ",", "]");
return Strings.formatAsList(this, "[", ",", "]");
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public static <T> ImmutableSet<T> fromCollection(@Nullable Collection<T> seq) {

@Override
public String toString() {
return Strings.formatAsList(iterator(), "{", ",", "}");
return Strings.formatAsList(this, "{", ",", "}");
}

/** represents the empty set for elements of type <T> */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public boolean equals(Object o) {

@Override
public String toString() {
return Strings.formatAsList(iterator(), "[", ",", "]");
return Strings.formatAsList(this, "[", ",", "]");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public Iterator<T> sortedIterator() {


public String toString() {
return Strings.formatAsList(iterator(), "[", ",", "]");
return Strings.formatAsList(this, "[", ",", "]");
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public boolean equals(Object o) {

@Override
public String toString() {
return Strings.formatAsList(this.iterator(), "[", ",", "]");
return Strings.formatAsList(this, "[", ",", "]");
}
}

Expand Down

0 comments on commit e19bc2a

Please sign in to comment.