light refactoring

This commit is contained in:
kifir 2024-05-09 22:45:23 +03:00 committed by rusefillc
parent 697c4da88d
commit 11dd4c081a
2 changed files with 8 additions and 6 deletions

View File

@ -34,8 +34,7 @@ public class IniFileReader {
boolean hadQuote = false;
int openedBraceCount = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
for (final char c: str.toCharArray()) {
if (c == '\"' || (isTokenSeparator(c) && !inQuote && (openedBraceCount == 0))) {
if (c == '\"') {
inQuote = !inQuote;

View File

@ -4,12 +4,12 @@ import java.util.ArrayList;
import java.util.List;
public class TokenUtils {
public static String[] tokenizeWithBraces(String stringToTokenize) {
public static String[] tokenizeWithBraces(final String stringToTokenize) {
final List<String> tokens = new ArrayList<>();
if (stringToTokenize != null) {
StringBuilder currentToken = new StringBuilder();
int openedBracesCount = 0;
for (char ch : stringToTokenize.toCharArray()) {
for (final char ch : stringToTokenize.toCharArray()) {
// we finish token parsing only if it contains no unclosed braces
if ((ch == ',') && (openedBracesCount == 0)) {
tokens.add(currentToken.toString());
@ -25,7 +25,10 @@ public class TokenUtils {
if (0 < openedBracesCount) {
openedBracesCount--;
} else {
throw new IllegalStateException("Unexpected closing brace");
throw new IllegalArgumentException(String.format(
"Unexpected closing brace: %s",
stringToTokenize
));
}
break;
}
@ -33,7 +36,7 @@ public class TokenUtils {
}
}
if (openedBracesCount != 0) {
throw new IllegalStateException("Unclosed opening brace");
throw new IllegalArgumentException(String.format("Unclosed opening brace: %s", stringToTokenize));
}
tokens.add(currentToken.toString());
}