About the CharMatcher.WHITESPACE implementation

前端 未结 1 1768
独厮守ぢ
独厮守ぢ 2021-01-27 08:09

When i looked up the implementation of CharMatcher and notice a field WHITESPACE_MULTIPLIER=1682554634 , then i set this value to 1582554634

相关标签:
1条回答
  • 2021-01-27 08:25

    I'm not sure if the generator still exists somewhere, but it can be recreated easily. The class Result contains the data used in the implementation of CharMatcher.WHITESPACE:

    static class Result {
        private int shift;
        private int multiplier;
        private String table;
    }
    
    // No duplicates allowed.
    private final String allMatchingString = "\u2002\r\u0085\u200A\u2005\u2000"
            + "\u2029\u000B\u2008\u2003\u205F\u1680"
            + "\u0009\u0020\u2006\u2001\u202F\u00A0\u000C\u2009"
            + "\u2004\u2028\n\u2007\u3000";
    
    public Result generate(String allMatchingString) {
        final char[] allMatching = allMatchingString.toCharArray();
        final char filler = allMatching[allMatching.length - 1];
        final int shift = Integer.numberOfLeadingZeros(allMatching.length);
        final char[] table = new char[1 << (32 - shift)];
        OUTER: for (int i=0; i>=0; ++i) {
            final int multiplier = 123456789 * i; // Jumping a bit makes the search faster.
            Arrays.fill(table, filler);
            for (final char c : allMatching) {
                final int index = (multiplier * c) >>> shift;
                if (table[index] != filler) continue OUTER; // Conflict found.
                table[index] = c;
            }
            return new Result(shift, multiplier, new String(table));
        }
        return null; // No solution exists.
    }
    

    It generates a different multiplier, but this doesn't matter.

    In case no solution for a given allMatchingString exists, you can decrement shift and try again.

    0 讨论(0)
提交回复
热议问题