1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package io.netty.util.internal;
16
17
18
19
20
21 public final class SWARUtil {
22
23
24
25
26 public static long compilePattern(byte byteToFind) {
27 return (byteToFind & 0xFFL) * 0x101010101010101L;
28 }
29
30
31
32
33
34
35
36
37
38 public static long applyPattern(final long word, final long pattern) {
39 long input = word ^ pattern;
40 long tmp = (input & 0x7F7F7F7F7F7F7F7FL) + 0x7F7F7F7F7F7F7F7FL;
41 return ~(tmp | input | 0x7F7F7F7F7F7F7F7FL);
42 }
43
44
45
46
47
48
49
50
51
52
53
54 public static int getIndex(final long word, final boolean isBigEndian) {
55 final int zeros = isBigEndian? Long.numberOfLeadingZeros(word) : Long.numberOfTrailingZeros(word);
56 return zeros >>> 3;
57 }
58
59
60
61
62 private static long applyUpperCasePattern(final long word) {
63
64 long rotated = word & 0x7F7F7F7F7F7F7F7FL;
65 rotated += 0x2525252525252525L;
66 rotated &= 0x7F7F7F7F7F7F7F7FL;
67 rotated += 0x1A1A1A1A1A1A1A1AL;
68 rotated &= ~word;
69 rotated &= 0x8080808080808080L;
70 return rotated;
71 }
72
73
74
75
76 private static int applyUpperCasePattern(final int word) {
77 int rotated = word & 0x7F7F7F7F;
78 rotated += 0x25252525;
79 rotated &= 0x7F7F7F7F;
80 rotated += 0x1A1A1A1A;
81 rotated &= ~word;
82 rotated &= 0x80808080;
83 return rotated;
84 }
85
86
87
88
89 private static long applyLowerCasePattern(final long word) {
90 long rotated = word & 0x7F7F7F7F7F7F7F7FL;
91 rotated += 0x0505050505050505L;
92 rotated &= 0x7F7F7F7F7F7F7F7FL;
93 rotated += 0x1A1A1A1A1A1A1A1AL;
94 rotated &= ~word;
95 rotated &= 0x8080808080808080L;
96 return rotated;
97 }
98
99
100
101
102 private static int applyLowerCasePattern(final int word) {
103 int rotated = word & 0x7F7F7F7F;
104 rotated += 0x05050505;
105 rotated &= 0x7F7F7F7F;
106 rotated += 0x1A1A1A1A;
107 rotated &= ~word;
108 rotated &= 0x80808080;
109 return rotated;
110 }
111
112
113
114
115 public static boolean containsUpperCase(final long word) {
116 return applyUpperCasePattern(word) != 0;
117 }
118
119
120
121
122 public static boolean containsUpperCase(final int word) {
123 return applyUpperCasePattern(word) != 0;
124 }
125
126
127
128
129 public static boolean containsLowerCase(final long word) {
130 return applyLowerCasePattern(word) != 0;
131 }
132
133
134
135
136 public static boolean containsLowerCase(final int word) {
137 return applyLowerCasePattern(word) != 0;
138 }
139
140
141
142
143 public static long toLowerCase(final long word) {
144 final long mask = applyUpperCasePattern(word) >>> 2;
145 return word | mask;
146 }
147
148
149
150
151 public static int toLowerCase(final int word) {
152 final int mask = applyUpperCasePattern(word) >>> 2;
153 return word | mask;
154 }
155
156
157
158
159 public static long toUpperCase(final long word) {
160 final long mask = applyLowerCasePattern(word) >>> 2;
161 return word & ~mask;
162 }
163
164
165
166
167 public static int toUpperCase(final int word) {
168 final int mask = applyLowerCasePattern(word) >>> 2;
169 return word & ~mask;
170 }
171
172 private SWARUtil() {
173
174 }
175
176 }