Browse Source

Simplify some parser logic relating to prefix modes.

Change-Id: I1764d2e737e9b4f67d7422f6b90e5d11380393a3
Reviewed-on: http://gerrit.dmdirc.com/3960
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
changes/60/3960/2
Chris Smith 9 years ago
parent
commit
90164d89bf

+ 1
- 1
src/com/dmdirc/parser/interfaces/Parser.java View File

370
     String getUserModes();
370
     String getUserModes();
371
 
371
 
372
     /**
372
     /**
373
-     * Retrieves a list of channel user modes, in descending priority order.
373
+     * Retrieves a list of channel user modes, in ascending priority order.
374
      *
374
      *
375
      * @return A string containing a list of channel user mode characters
375
      * @return A string containing a list of channel user mode characters
376
      */
376
      */

+ 15
- 26
src/com/dmdirc/parser/irc/IRCParser.java View File

1569
 
1569
 
1570
     @Override
1570
     @Override
1571
     public String getChannelUserModes() {
1571
     public String getChannelUserModes() {
1572
-        if (h005Info.containsKey("PREFIXSTRING")) {
1573
-            return h005Info.get("PREFIXSTRING");
1574
-        } else {
1575
-            return "";
1576
-        }
1572
+        return prefixModes.getModes();
1577
     }
1573
     }
1578
 
1574
 
1579
     @Override
1575
     @Override
1690
      */
1686
      */
1691
     public void parsePrefixModes() {
1687
     public void parsePrefixModes() {
1692
         final String sDefaultModes = "(ohv)@%+";
1688
         final String sDefaultModes = "(ohv)@%+";
1693
-        String[] bits;
1694
         String modeStr;
1689
         String modeStr;
1695
         if (h005Info.containsKey("PREFIX")) {
1690
         if (h005Info.containsKey("PREFIX")) {
1696
             modeStr = h005Info.get("PREFIX");
1691
             modeStr = h005Info.get("PREFIX");
1697
         } else {
1692
         } else {
1698
             modeStr = sDefaultModes;
1693
             modeStr = sDefaultModes;
1699
         }
1694
         }
1700
-        if (modeStr.substring(0, 1).equals("(")) {
1695
+        if ("(".equals(modeStr.substring(0, 1))) {
1701
             modeStr = modeStr.substring(1);
1696
             modeStr = modeStr.substring(1);
1702
         } else {
1697
         } else {
1703
             modeStr = sDefaultModes.substring(1);
1698
             modeStr = sDefaultModes.substring(1);
1704
             h005Info.put("PREFIX", sDefaultModes);
1699
             h005Info.put("PREFIX", sDefaultModes);
1705
         }
1700
         }
1706
 
1701
 
1707
-        bits = modeStr.split("\\)", 2);
1708
-        if (bits.length != 2 || bits[0].length() != bits[1].length()) {
1709
-            modeStr = sDefaultModes;
1710
-            callErrorInfo(new ParserError(ParserError.ERROR_ERROR, "PREFIX String not valid. Using default string of \"" + modeStr + "\"", getLastLine()));
1711
-            h005Info.put("PREFIX", modeStr);
1712
-            modeStr = modeStr.substring(1);
1713
-            bits = modeStr.split("\\)", 2);
1702
+        int closingIndex = modeStr.indexOf(')');
1703
+        if (closingIndex * 2 + 1 != modeStr.length()) {
1704
+            callErrorInfo(new ParserError(ParserError.ERROR_ERROR,
1705
+                    "PREFIX String not valid. Using default string of \"" + modeStr +
1706
+                    '"', getLastLine()));
1707
+            h005Info.put("PREFIX", sDefaultModes);
1708
+            modeStr = sDefaultModes.substring(1);
1709
+            closingIndex = modeStr.indexOf(')');
1714
         }
1710
         }
1715
 
1711
 
1716
-        // resetState
1717
-        prefixModes.clear();
1718
-
1719
-        for (int i = bits[0].length() - 1; i > -1; --i) {
1720
-            final Character cMode = bits[0].charAt(i);
1721
-            final Character cPrefix = bits[1].charAt(i);
1722
-            callDebugInfo(DEBUG_INFO, "Found Prefix Mode: %c => %c", cMode, cPrefix);
1723
-            if (!prefixModes.isPrefixMode(cMode)) {
1724
-                prefixModes.add(cMode, cPrefix);
1725
-            }
1726
-        }
1712
+        // The modes passed from the server are in descending order of importance, we want to
1713
+        // store them in ascending, so reverse them:
1714
+        final String reversedModes = new StringBuilder(modeStr).reverse().toString();
1727
 
1715
 
1728
-        h005Info.put("PREFIXSTRING", bits[0]);
1716
+        prefixModes.setModes(reversedModes.substring(closingIndex + 1),
1717
+                reversedModes.substring(0, closingIndex));
1729
     }
1718
     }
1730
 
1719
 
1731
     @Override
1720
     @Override

+ 14
- 3
src/com/dmdirc/parser/irc/PrefixModeManager.java View File

36
      * Resets the state of this manager, clearing all known modes.
36
      * Resets the state of this manager, clearing all known modes.
37
      */
37
      */
38
     public void clear() {
38
     public void clear() {
39
-        modes = "";
40
-        prefixes = "";
39
+        setModes("", "");
40
+    }
41
+
42
+    /**
43
+     * Replaces all existing modes with the specified ones.
44
+     *
45
+     * @param modes The new modes, in increasing order of importance.
46
+     * @param prefixes The corresponding new prefixes, in increasing order of importance.
47
+     */
48
+    public void setModes(final String modes, final String prefixes) {
49
+        this.modes = modes;
50
+        this.prefixes = prefixes;
41
     }
51
     }
42
 
52
 
43
     /**
53
     /**
100
      *
110
      *
101
      * @return Set of known modes, in increasing order of importance.
111
      * @return Set of known modes, in increasing order of importance.
102
      */
112
      */
103
-    public CharSequence getModes() {
113
+    public String getModes() {
104
         return modes;
114
         return modes;
105
     }
115
     }
106
 
116
 
175
 
185
 
176
         return result.toString();
186
         return result.toString();
177
     }
187
     }
188
+
178
 }
189
 }

Loading…
Cancel
Save