Skip to content

Instantly share code, notes, and snippets.

@WizardlyBump17
Last active July 16, 2024 02:05
Show Gist options
  • Save WizardlyBump17/91205533778f5fb27d66655fb4fb8c86 to your computer and use it in GitHub Desktop.
Save WizardlyBump17/91205533778f5fb27d66655fb4fb8c86 to your computer and use it in GitHub Desktop.
Some bombastic code to apply placeholders, parse strings (input: "test 123" a b, output: [test 123, a, b] and trim strings (input: a b, output: a b)
package com.wizardlybump17.tests.generic;
import lombok.NonNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Bomba1e2 {
public static final char QUOTE = '"';
public static final char ESCAPE = '\\';
public static final char DELIMITER = ' ';
public static final char SPACE = ' ';
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Trim strings? (true/false)");
boolean trim = scanner.nextBoolean();
while (scanner.hasNextLine()) {
try {
String input = scanner.nextLine();
if (input.equalsIgnoreCase("stop"))
break;
if (input.equalsIgnoreCase("exit"))
return;
System.out.println(getStrings(input, QUOTE, ESCAPE, DELIMITER, trim));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static @NonNull List<String> getStrings(@NonNull String input, char quote, char escape, char delimiter, boolean trim) {
List<String> strings = getStrings(trim ? trim(input) : input, quote, escape, delimiter);
if (trim)
strings.replaceAll(Bomba1e2::trim);
return strings;
}
public static @NonNull List<String> getStrings(@NonNull String input, char quote, char escape, char delimiter) {
List<String> strings = new ArrayList<>();
char[] chars = input.toCharArray();
StringBuilder builder = new StringBuilder();
StringBuilder quoted = new StringBuilder();
boolean escaped = false;
boolean delimited = true;
boolean hadQuote = false;
for (char current : chars) {
if (current == escape && !escaped) { // start of an escaped char
escaped = true;
continue;
}
if (escaped) { // end of the escaped char
(quoted.isEmpty() ? builder : quoted).append(current);
escaped = false;
continue;
}
if (current == quote) {
if (!delimited) // the previous char was not the delimiter. Example case: string"quoted"
throw new IllegalArgumentException("Can not have quoted strings without the delimiter");
if (quoted.isEmpty()) { // begin of quoted string
quoted.append(quote);
continue;
}
// end of quoted string
strings.add(quoted.substring(1));
delimited = false;
hadQuote = true;
quoted.setLength(0);
continue;
}
if (current == delimiter && quoted.isEmpty()) { // delimiter (space)
if (!builder.isEmpty()) {
strings.add(builder.toString());
builder.setLength(0);
}
delimited = true;
hadQuote = false;
continue;
}
if (hadQuote) // the previous char was a quote. Example case: "quoted"string
throw new IllegalArgumentException("Can not have non-quoted strings after quoted strings");
(quoted.isEmpty() ? builder : quoted).append(current); // any char
if (quoted.isEmpty())
delimited = false;
}
if (escaped)
throw new IllegalArgumentException("Invalid escape sequence");
if (!quoted.isEmpty())
throw new IllegalArgumentException("Unclosed quotes");
if (!builder.isEmpty())
strings.add(builder.toString());
return strings;
}
public static @NonNull String trim(@NonNull String string) {
if (string.isEmpty())
return string;
StringBuilder builder = new StringBuilder(string.length());
char[] chars = string.toCharArray();
for (int i = 0; i < chars.length; i++) {
char current = chars[i];
if (current != SPACE || (i != 0 && chars[i - 1] != SPACE))
builder.append(current);
}
int length = builder.length();
if (length > 0 && builder.charAt(length - 1) == SPACE)
builder.deleteCharAt(length - 1);
return builder.toString();
}
}
package com.wizardlybump17.tests.generic;
import lombok.NonNull;
import java.util.Map;
import java.util.Objects;
import java.util.Scanner;
public class Bomba3 {
public static final char PLACEHOLDER_BEGIN = '{';
public static final char PLACEHOLDER_END = '}';
public static final char ESCAPE = '\\';
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
try {
System.out.println(applyPlaceholders(scanner.nextLine(), Map.of("test", "{test}", "test1", "TEST1"), PLACEHOLDER_BEGIN, PLACEHOLDER_END));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static @NonNull String applyPlaceholders(@NonNull String string, @NonNull Map<Object, Object> map, char begin, char end) {
char[] chars = string.toCharArray();
StringBuilder builder = new StringBuilder(chars.length);
StringBuilder placeholder = new StringBuilder();
boolean escaped = false;
for (char current : chars) {
if (current == ESCAPE && !escaped) {
escaped = true;
continue;
}
if (current == begin && !escaped) {
if (placeholder.isEmpty()) {
placeholder.append(current);
continue;
}
throw new IllegalArgumentException("Duplicated placeholder begin");
}
if (current == end && !escaped) {
if (placeholder.isEmpty())
throw new IllegalArgumentException("Unexpected placeholder end");
Object value = map.get(placeholder.substring(1));
builder.append(value);
placeholder.setLength(0);
continue;
}
(placeholder.isEmpty() ? builder : placeholder).append(current);
escaped = false;
}
if (escaped)
throw new IllegalArgumentException("Invalid escape sequence");
if (!placeholder.isEmpty())
throw new IllegalArgumentException("Unclosed placeholder");
return builder.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment