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; boolean hadQuote = false;
int openedBraceCount = 0; int openedBraceCount = 0;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) { for (final char c: str.toCharArray()) {
char c = str.charAt(i);
if (c == '\"' || (isTokenSeparator(c) && !inQuote && (openedBraceCount == 0))) { if (c == '\"' || (isTokenSeparator(c) && !inQuote && (openedBraceCount == 0))) {
if (c == '\"') { if (c == '\"') {
inQuote = !inQuote; inQuote = !inQuote;

View File

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