26
5
Right hand brace is a style of code bracketing in which curly braces and semicolons are all aligned to a single point on the right side of a a file.

Generally, this is considered bad practice, for several reasons.
The Challenge
Take a multiline string through any method, and convert it's brace style to Right Hand Brace.
For this challenge, you only need it to work on Java code, however, it should theoretically work on any code that uses Braces and Semicolons.
You must grab all {}; characters in a row, with any amount of whitespace between them. EG. }}, ; } }\n\t\t}, and line them up on the right side of the file through the use of whitespace.
for example:
a {
b;
{c
should become
a {
b ;{
c
Or, more abstractly, push any and all whitespace from the left of all {}; characters, to the right.
Indentation of lines should be otherwise preserved. Lines only containing whitespace after the movement of the {}; characters may optionally be removed.
For example:
a{
b{
c;
}
}
d;
May become either
a {
b {
c;}}
d ;
or
a {
b {
c;}}
d ;
Pushed to the right refers to all the {}; characters being aligned to a point no shorter than the longest line. Any amount of space after that is acceptable.
So all the below is acceptable:
a {
bc;
a {
bc ;
a {
bc ;
etc...
Lines in any code may contain {}; characters between other non-whitspace characters, the handling of this case isn't necessary, although if you're inclined, you should leave them in place. Lines may also not contain any {}; characters at all, and this should be handled correctly. As is shown below.
a {
b ;
c
d }
Because we don't want Code Review to see the horrible things we're doing, you need to make your code as small as possible.
Examples / Testcases
Generic Java
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello, World!");
}
}
becomes...
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!") ;}}
The image itself
public class Permuter{
private static void permute(int n, char[] a){
if (n == 0){
System.out.println(String.valueOf(a));
}else{
for (int i=0; i<= n; i++){
permute(n-1, a);
swap(a, n % 2 == 0 ? i : 0, n);
}
}
}
private static void swap(char[] a, int i, int j){
char saved = a[i];
a[i] = a[j];
a[j] = saved;
}
}
becomes...
public class Permuter {
private static void permute(int n, char[] a) {
if (n == 0) {
System.out.println(String.valueOf(a)) ;}
else {
for (int i=0; i<= n; i++) {
permute(n-1, a) ;
swap(a, n % 2 == 0 ? i : 0, n) ;}}}
private static void swap(char[] a, int i, int j) {
char saved = a[i] ;
a[i] = a[j] ;
a[j] = saved ;}}
Not so Perfectly Generic Python
For contrast
def Main():
print("Hello, World!");
Main();
becomes...
def Main():
print("Hello, World!") ;
Main() ;
Notes
- Standard Loopholes apply
- Standard IO applies
- This is code-golf, so shortest program in bytes wins!
- I am not Liable for damages related to programming in Right Hand Brace style
- Have Fun!
Edit Notes
I reworded the challenge details, Hopefully I didn't break anyone's view of the rules, I assure you it was unintentional. This should be a much more clear and less self-conflicting spec.
What's the verdict on lines with multiple semicolons? Something like
int a=0;System.out.println(a);– Value Ink – 2017-02-14T01:26:18.023You only need to handle the last semicolon, although you may optionally insert a newline after each. You can assume such a case won't happen – ATaco – 2017-02-14T01:27:51.447
Oh I forgot to ask - can we assume no white space exists to the right of any trailing
{};s? – Jonathan Allan – 2017-02-14T02:26:01.193@JonathanAllan For the case of this, you can assume to whitespace after the
{};s, however, you do need to combine{};s with whitespace between them, including across lines. – ATaco – 2017-02-14T02:28:06.913Ah "and across lines". I read "You may optionally: ... Preserve blank lines" to mean we can keep them. – Jonathan Allan – 2017-02-14T02:31:13.750
If a
{is on its own line, and the line before it has no braces or semicolons, such asif (condition), does the brace need to go up to the previous line? – Value Ink – 2017-02-14T03:27:07.443Yes, otherwise it leaves a basically blank line left over – ATaco – 2017-02-14T03:28:25.810
2That might no be the best image for the challenge if we don't need to handle for loops like in the sample image? – Dennis – 2017-02-14T04:56:19.317
The sample image was the inspiration for this challenge. Plus, the actual contents of the code shouldn't matter that much. – ATaco – 2017-02-14T05:09:29.307
1
Looks like the image in the question came from this example, which was followed up by this followup, which has more complex examples
– Ray Toal – 2017-02-14T05:43:01.8132It could be made clearer that you want the
;{}characters gathered up if they're on separate lines (it's only clear from the example, not the rules, and in fact if a line consists of\t}preserving the indentation would mean not moving}up to the end of the previous line) – Chris H – 2017-02-14T10:44:08.883Can we assume there is always a semicolon or brace in a line with code? – Titus – 2017-02-14T12:17:16.663
You say Yes, otherwise it leaves a basically blank line left over here in the comments, but You may optionally: [...] Preserve blank lines in the challenge. Those seem to contradict each other. – Dennis – 2017-02-14T14:37:26.067
In the second example, on the second line, there are two spaces between the closing bracket and the semicolon. Is this on purpose? if so, does this mean that A space between the longest line and this point is optional is in reality some spaces... ? – Dada – 2017-02-14T20:30:40.483
The 'a space' refers simply to any amount of white space. In regards to @Dennis, he's right. The preserving or removal of those lines Which only contain
{};should be optional. As for @Titus, there may not always be a brace or a semicolon on the line. I may reword the rules to make this more obvious when I get to a computer. – ATaco – 2017-02-14T20:33:54.643Some more test cases would be nice... – Conor O'Brien – 2017-02-15T04:25:04.063
You edit changes the challenge a lot. In particular, the removal of you can assume these characters will not occur in the middle of a line. might invalidate a few answers. – Dada – 2017-02-15T10:34:46.270
@Dada It invalidates mine, so I have to change it now – user41805 – 2017-02-15T10:36:18.473
Also in your Permuter example, you forgot to move the brace behind the
}else– user41805 – 2017-02-15T10:38:45.5832Good god, please tell me nobody actually does this in practice for a verbose language like Java ._. – Magic Octopus Urn – 2017-02-15T17:16:30.507
I think this style is for people who like whitespace-delineated languages (accuracy of term?) (cough, Python, cough) or like the game of "find the missing brace!" – Draco18s no longer trusts SE – 2017-06-07T13:43:57.283