blob: 7dc9aea6b3c70974a9b56d7248bd15ad048f45c8 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.grouping.vespa;
import java.util.Arrays;
/**
* @author Simon Thoresen Hult
*/
class IntegerDecoder {
private static final int CHAR_MIN = IntegerEncoder.CHARS[0];
private static final int CHAR_MAX = IntegerEncoder.CHARS[IntegerEncoder.CHARS.length - 1];
private final String input;
private int pos = 0;
public IntegerDecoder(String input) {
this.input = input;
}
public boolean hasNext() {
return pos < input.length();
}
public int next() {
int val = 0;
int len = decodeChar(input.charAt(pos++));
for (int i = 0; i < len; i++) {
val = (val << 4) | decodeChar(input.charAt(pos + i));
}
pos += len;
return (val >>> 1) ^ (-(val & 0x1));
}
private static int decodeChar(char c) {
if (c >= CHAR_MIN && c <= CHAR_MAX) {
return (0xF & (c - CHAR_MIN));
} else {
throw new NumberFormatException("Expected a char in " + Arrays.toString(IntegerEncoder.CHARS) +
" but was '" + c + "'");
}
}
}
|