Introduction
I'm a real big fan of the SBU (Short But Unique) challenges that crop up on PPCG all of the time. The CUSRS is a system designed to refactor strings, a CUSRS function takes 2 parameters and outputs 1 String.
#Challenge
Produce a program, function, lambda or acceptable alternative to do the following:
Given String input and String refactor (as examples), refactor input using refactor as follows:
The refactor String will be in the format of ((\+|\-)\w* *)+ (regex), for example:
+Code -Golf -lf +al Each section is a refactoring action to perform on input. Each program also has a pointer.
+ Will insert it's suffix (without the plus) at the pointers current location in the String and then reset the pointer to 0.
Each operation should be applied to the input String and the result should be returned.
Example:
input: Golf +Code //pointer location: 0 output: CodeGolf //pointer location: 0 - Will move the pointer over to the right until it finds the suffix and will remove that part of the String. Leaving the pointer on the left hand side of the text that was just removed.
input: Golf -lf //pointer location 0 output: Go //pointer location 2 #Examples
input: Simple -impl +nip -e +er output: Sniper input: Function -F +Conj output: Conjunction input: Goal +Code -al +lf output: CodeGolf #Example Code
The example is Java, it's not golfed at all.
public static String refactor(String input, String swap) { int pointer = 0; String[] commands = swap.replace(" ", "").split("(?=[-+])"); for (String s : commands) { if (s.startsWith("+")) { input = input.substring(0, pointer) + s.substring(1) + input.substring(pointer, input.length()); pointer = 0; } else { if (s.startsWith("-")) { String remove = s.substring(1); for (int i = pointer; i < input.length(); i++) { if (input.substring(i, i + remove.length() > input.length() ? input.length() : i + remove.length()).equals(remove)) { pointer = i; input = input.substring(0, i) + input.substring(i + remove.length(), input.length()); break; } } } } } return input; } #Rules
- Standard Loopholes Apply
- Shortest code, in bytes, wins