package com.github.nstdio.libpgn.core.parser;

import com.github.nstdio.libpgn.core.NAG;
import com.github.nstdio.libpgn.common.ArrayUtils;
import com.github.nstdio.libpgn.common.CollectionUtils;
import com.github.nstdio.libpgn.common.Pair;

import java.util.Set;
import java.util.TreeSet;

class InlineNag {

    Pair<byte[], short[]> split(byte[] moveBytes) {
        final String move = new String(moveBytes);
        Set<Short> nags = new TreeSet<>();

        int firstOccurrence = -1;

        for (int i = 2, n = move.length(); i < n; i++) {
            final char ch = move.charAt(i);

            final short nag = NAG.indexOf(ch);

            if (nag != -1 && firstOccurrence == -1) {
                firstOccurrence = i;
            }

            if (i + 1 < n) {
                final char nCh = move.charAt(i + 1);
                switch (ch) {
                    case '!':
                        switch (nCh) {
                            case '?':
                                i++;
                                nags.add((short) 5);
                                break;
                            case '!':
                                i++;
                                nags.add((short) 3);
                                break;
                            default:
                                nags.add(nag);
                                break;
                        }
                        break;
                    case '?':
                        switch (nCh) {
                            case '!':
                                i++;
                                nags.add((short) 6);
                                break;
                            case '?':
                                i++;
                                nags.add((short) 4);
                                break;
                            default:
                                nags.add(nag);
                                break;
                        }
                        break;
                    case '+':
                        switch (nCh) {
                            case '-':
                                if (firstOccurrence == -1) {
                                    firstOccurrence = i;
                                }
                                i++;
                                nags.add((short) 18);
                                break;
                        }
                        break;
                    case '-':
                        switch (nCh) {
                            case '+':
                                if (firstOccurrence == -1) {
                                    firstOccurrence = i;
                                }
                                i++;
                                nags.add((short) 19);
                                break;
                        }
                    case 'R':
                        switch (nCh) {
                            case 'R':
                                if (firstOccurrence == -1) {
                                    firstOccurrence = i;
                                }
                                i++;
                                nags.add((short) 145);
                                break;
                        }
                        break;
                    case 'N':
                        nags.add(nag);
                        break;
                    default:
                        if (nag != -1) {
                            nags.add(nag);
                        }
                        break;
                }
            } else if (nag != -1) {
                nags.add(nag);
            }
        }

        if (firstOccurrence == -1) {
            return Pair.of(move.getBytes(), ArrayUtils.EMPTY_SHORT_ARRAY);
        }

        return Pair.of(
                move.substring(0, firstOccurrence).getBytes(),
                CollectionUtils.toArray(nags)
        );
    }
}

@@ -2077,65 +2077,26 @@
     
-switch (nCh) %7B%0A                            case '-':%0A    
+if (nCh == '-') %7B%0A
     
@@ -2172,36 +2172,32 @@
                 
-    
 firstOccurrence 
@@ -2221,36 +2221,32 @@
                 
-    
 %7D%0A              
@@ -2255,37 +2255,29 @@
             
- 
-   
 i++;%0A
-    
             
@@ -2318,47 +2318,8 @@
 8);%0A
-                                break;%0A
     
@@ -2429,65 +2429,26 @@
     
-switch (nCh) %7B%0A                            case '+':%0A    
+if (nCh == '+') %7B%0A
     
@@ -2524,36 +2524,32 @@
                 
-  
-  
 firstOccurrence 
@@ -2577,34 +2577,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -2636,36 +2636,32 @@
                 
-    
 nags.add((short)
@@ -2670,47 +2670,8 @@
 9);%0A
-                                break;%0A
     
@@ -2750,65 +2750,26 @@
     
-switch (nCh) %7B%0A                            case 'R':%0A    
+if (nCh == 'R') %7B%0A
     
@@ -2845,36 +2845,32 @@
                 
-    
 firstOccurrence 
@@ -2898,34 +2898,26 @@
             
-  
-  %7D%0A    
+%7D%0A
             
@@ -2929,36 +2929,32 @@
            i++;%0A
-    
                 
@@ -2992,47 +2992,8 @@
 5);%0A
-                                break;%0A
     

Replace single case switch statements with if.