forked from real-logic/agrona
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add natural, int and long parse and put methods for the buffer hierarchy
- Loading branch information
1 parent
19ac040
commit b422867
Showing
7 changed files
with
870 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2014-2017 Real Logic Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.agrona; | ||
|
||
import static java.nio.charset.StandardCharsets.US_ASCII; | ||
|
||
/** | ||
* Single source of truth for ascii encoding data | ||
*/ | ||
public class AsciiEncodingHelper | ||
{ | ||
public static final byte NEGATIVE = '-'; | ||
public static final byte ZERO = '0'; | ||
private static final int[] INT_ROUNDS = | ||
{ | ||
9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE | ||
}; | ||
private static final long[] LONG_ROUNDS = | ||
{ | ||
9L, 99L, 999L, 9999L, 99999L, 999999L, 9999999L, 99999999L, 999999999L, | ||
9_999999999L, 99_999999999L, 999_999999999L, 9999_999999999L, | ||
99999_999999999L, 999999_999999999L, 9999999_999999999L, 99999999_999999999L, | ||
999999999_999999999L, Long.MAX_VALUE | ||
}; | ||
public static final byte[] MIN_INTEGER_VALUE = String.valueOf(Integer.MIN_VALUE).getBytes(US_ASCII); | ||
public static final byte[] MIN_LONG_VALUE = String.valueOf(Long.MIN_VALUE).getBytes(US_ASCII); | ||
public static final byte MINUS_SIGN = (byte)'-'; | ||
|
||
public static int endOffset(final int value) | ||
{ | ||
for (int i = 0; true; i++) | ||
{ | ||
if (value <= INT_ROUNDS[i]) | ||
{ | ||
return i; | ||
} | ||
} | ||
} | ||
|
||
public static int endOffset(final long value) | ||
{ | ||
for (int i = 0; true; i++) | ||
{ | ||
if (value <= LONG_ROUNDS[i]) | ||
{ | ||
return i; | ||
} | ||
} | ||
} | ||
|
||
public static int getDigit(final int index, final byte value) | ||
{ | ||
if (value < 0x30 || value > 0x39) | ||
{ | ||
throw new NumberFormatException("'" + ((char)value) + "' isn't a valid digit @ " + index); | ||
} | ||
|
||
return value - 0x30; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.